Tuesday 18 March 2014

What are Inserted and Deleted Tables in Trigger? or Do you know about magic tables in SQL Server?

These two tables also known as magic tables of SQL Server.
When a DML trigger is executing, it has access to two memory-resident tables that allow access to the data that was modified: Inserted and Deleted. These tables are available only within the body of a trigger for read-only access, and they are automatically created and managed by SQL Server.
The structures of the inserted and deleted tables are the same as the structure of the table on which the trigger is defined.
For insert operations, all inserted rows are available in the inserted table.
For delete operations, all deleted rows are available in the deleted table.

For update operations, an update can be thought of as a delete followed by an insert. Both the inserted and deleted tables are available within an UPDATE trigger. The deleted table stores the records before modification (old values), and the inserted table stores records after they were updated (new values).

No comments:

Post a Comment