1.

What are Magic tables in the SQL server?

Answer»

The magic tables are an integral PART of Triggers. They facilitate Triggers and make the job easier.  There are two magic tables that exist, one is inserted one and the other one is called deleted one. These are built by SQL server itself to hold the data while PROCESSING gets triggered. They hold the inserted, deleted and UPDATED records while doing DML operations on a table. If we have triggers for INSERT operation then the newly inserted values are stored in the INSERTED magic table. If triggers get fired for UPDATE /DELETE operation then updated and deleted values of records are stored in DELETED magic tables.

  • INSERTED Magic table

Let me explain the INSERTED magic table by looking at the below example. In the below screenshot we have triggers for INSERT operation and we can see two tables INSERTED/DELETED which we can use in the trigger for performing any manipulation.

  • DELETED Magic table

The below example illustrates the use case of a DELETED magic table which COMES into the picture when we have UPDATE/DELETE trigger. This table holds the old record which was either updated/deleted.



Discussion

No Comment Found