SQL Server 中的触发器

发布于 2024-10-03 01:54:37 字数 629 浏览 0 评论 0原文

有两个表Table_TriggerTable_Trigger1。我在Table_Trigger1上创建了一个触发器。它工作完美。下面是我的触发器脚本 -

CREATE TRIGGER Test_Trigger
ON Table_Trigger1

INSTEAD OF INSERT

AS
SET NOCOUNT ON

INSERT INTO Table_Trigger1(FirstName)
SELECT FirstName from INSERTED I
WHERE I.firstName in (SELECT FirstName from Table_Trigger)

当我的 where 条件为 true 时,我收到一条消息(1 行受影响)并将值保存在表中。但是当我的 where 条件为 false 时,当时我还收到一条消息(1 行受影响)并且值未保存在表中。

我的问题是 - 如果 where 条件为 false 并且值未保存在表中,那么为什么我收到 (1 row(s)受影响) 消息。此消息的含义是什么。

提前致谢。

There are two Table Table_Trigger and Table_Trigger1.I have created a trigger on Table_Trigger1. It works perfectly.Below is my Trigger script -

CREATE TRIGGER Test_Trigger
ON Table_Trigger1

INSTEAD OF INSERT

AS
SET NOCOUNT ON

INSERT INTO Table_Trigger1(FirstName)
SELECT FirstName from INSERTED I
WHERE I.firstName in (SELECT FirstName from Table_Trigger)

when my where condition is true, I get a message (1 row(s) affected) and value save in Table.But when my where condition is false, that time I also get a message (1 row(s) affected) and value not save in Table.

My question is - If where condition is false and value don't save in table so why I got a (1 row(s) affected) message. what is meaning of this message.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-10-10 01:54:37

简而言之,原始插入的“受影响的行”不知道触发器中幕后发生的情况。它通过一行并认为该行“受影响”。

我试图找到一些文档来支持这一点。我能得到的最接近的是 SqlCommand 上的文档。 ExecuteNonQuery,其中指出:

用于更新、插入和删除
语句,返回值是
受影响的行数
命令。当触发器存在于
正在插入或更新表时,
返回值包括数量
受插入或影响的行
更新操作和数量
受触发器影响的行或
触发器。

当然,触发器中的 SET NOCOUNT ON 会抑制触发器内受影响的行数的返回,因此您只能看到原始插入语句的结果。

In a nutshell, the "rows affected" for your original insert is ignorant of what's going on behind the scenes in your trigger. It passes one row through and considers that row "affected."

I tried to find some documentation to back this up. The closest I could come was the documentation on SqlCommand.ExecuteNonQuery, which states:

For UPDATE, INSERT, and DELETE
statements, the return value is the
number of rows affected by the
command. When a trigger exists on a
table being inserted or updated, the
return value includes the number of
rows affected by both the insert or
update operation and the number of
rows affected by the trigger or
triggers.

Of course, the SET NOCOUNT ON in your trigger is suppressing the return of the number of rows affected within the trigger, so you're only seeing the result of the original insert statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文