帮助了解插入和删除的表SQL Server

时间:2021-02-09 23:38:18

If a run an INSERT INTO statement that triggers an INSERT AFTER trigger. What table/row(s) does the inserted and deleted objects represent in the trigger? For example,

如果运行INSERT INTO语句,则触发INSERT AFTER触发器。插入和删除的对象在触发器中表示哪些表/行?例如,

INSERT INTO Person.person (personID, name) VALUES (1, 'robert')

CREATE TRIGGER myTrigger  
ON Person.person  
AFTER INSERT AS  
BEGIN  
 insert into Person.PersonMovies (personID, movieID)  
 select inserted.personID  from inserted   _--i know the code is incomplete, but i'm curious only about the inserted part._  
END  

I'm confused as to what records are held by the inserted table. Would it be the records in the insert statement that triggered the trigger or the Person.PersonMovies table?

我对插入的表所保存的记录感到困惑。它是插入语句中触发触发器或Person.PersonMovies表的记录吗?

4 个解决方案

#1


2  

Yes, the inserted/deleted tables gives your trigger code access to the records changed by the statement that caused (triggered) the trigger. Also remember that your statements could have effected multiple rows, so your deleted or inserted tables could have multiple records.

是的,插入/删除的表使您的触发器代码可以访问由引发(触发)触发器的语句更改的记录。还要记住,您的语句可能会影响多行,因此您删除或插入的表可能有多个记录。

#2


1  

The rows in the pseudotables inserted and deleted are based on the table defined in you ON clause.

插入和删除的伪表中的行基于您在ON子句中定义的表。

#3


0  

The Inserted table holds the rows from the original Insert statement that caused the trigger to fire.

Inserted表保存原始Insert语句中导致触发器触发的行。

See also this MSDN article.

另请参阅此MSDN文章。

#4


0  

The inserted and deleted pseudotables hold one record for each row in the table that was created or deleted by the statement that fired the trigger. For an update, the deleted table holds the old version of the records, and the inserted table holds the new. The schema of the pseudotables matches the actual table.

插入和删除的伪表为表中的每一行保存一条记录,该记录是由触发触发器的语句创建或删除的。对于更新,已删除的表包含旧版本的记录,而插入的表包含新的记录。伪表的模式与实际表匹配。

#1


2  

Yes, the inserted/deleted tables gives your trigger code access to the records changed by the statement that caused (triggered) the trigger. Also remember that your statements could have effected multiple rows, so your deleted or inserted tables could have multiple records.

是的,插入/删除的表使您的触发器代码可以访问由引发(触发)触发器的语句更改的记录。还要记住,您的语句可能会影响多行,因此您删除或插入的表可能有多个记录。

#2


1  

The rows in the pseudotables inserted and deleted are based on the table defined in you ON clause.

插入和删除的伪表中的行基于您在ON子句中定义的表。

#3


0  

The Inserted table holds the rows from the original Insert statement that caused the trigger to fire.

Inserted表保存原始Insert语句中导致触发器触发的行。

See also this MSDN article.

另请参阅此MSDN文章。

#4


0  

The inserted and deleted pseudotables hold one record for each row in the table that was created or deleted by the statement that fired the trigger. For an update, the deleted table holds the old version of the records, and the inserted table holds the new. The schema of the pseudotables matches the actual table.

插入和删除的伪表为表中的每一行保存一条记录,该记录是由触发触发器的语句创建或删除的。对于更新,已删除的表包含旧版本的记录,而插入的表包含新的记录。伪表的模式与实际表匹配。