The tables INSERTED and DELETED are called magic tables of the SQL Server. We can not see these tables in the database. But we can access these tables from the "TRIGGER"

When we insert the record into the table, the magic table INSERTED will be created In that table the current inserted row will be available. We can access this record in the "TRIGGER".

When we update a record on the table where the trigger is created, the magic tables INSERTED and DELETED both will be created and the Old data of the updating record will be available in DELETED table and, the new data will be available in "INSERTED" table while accessing them inside the trigger.

When we delete the record from the table, the magic table DELETED will be created In that table the current deleted row will be available. We can access this record in the "TRIGGER".

Example:

Following code Explains the magic table INSERTED:

CREATE TRIGGER LogMessage
ON EMP
FOR INSERT
AS
   DECLARE @EMPNAME varchar(50)
   SELECT @EMPNAME = ( SELECT EMPNAME FROM INSERTED )
   INSERT INTO LOGTABLE ( UserId, Message )
   VALUES ( @EMPNAME, 'Record Added' )
GO

Following code Explain the magic table DELETED:

CREATE TRIGGER LogMessage
ON EMP
FOR DELETE
AS
   DECLARE @EMPNAME varchar(50)
   SELECT @EMPNAME = ( SELECT EMPNAME FROM DELETED )
   INSERT INTO LOGTABLE ( UserId, Message )
   VALUES ( @EMPNAME, 'Record Removed' )
GO
Copyright © 2025 delaney. All rights reserved.