Help Understanding the inserted and deleted Table SQL Server
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 Person.person (personID, name) VALUES (1, 'robert')
CREATE TRIGGER myTrigger
ON Person.person
AFTER INSERT AS
BEGIN
insert into Person.Perso开发者_JS百科nMovies (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?
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.
The rows in the pseudotables inserted
and deleted
are based on the table defined in you ON
clause.
The Inserted table holds the rows from the original Insert statement that caused the trigger to fire.
See also this MSDN article.
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.
精彩评论