Thursday, August 19, 2010

Magic Tables

While using triggers Inserted & Deleted tables
(called as magic tables) will be created automatically.

We can not see these tables in the data base. But we can access these
tables from the "TRIGGER" only.

update () and columns_updated() functions can
be used to determine the changes being caused by the DML
statements.

Note that the Magic Table does not contain the information
about the columns of the data-type text, ntext, or image.
Attempting to access these columns will cause an error.

When we insert any record then that record will be added
into this Inserted table initially, similarly while
updating a record a new entry will be inserted into
Inserted table & old value will be inserted into Deleted
table.

In the case of deletion of a record then it will insert
that record in the Deleted table

Example ( How use Magic Tables in Trigger)

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

No comments:

Followers

Link