Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Hw i will see Contents of inserted tbl of trigger

Author  Topic 

ranjeetsingh_6
Posting Yak Master

125 Posts

Posted - 2006-08-04 : 03:09:21
Hi

I have some trigger on a table.Since Trigger create two special table inserted and deleted i want to see structure and contents of inserted and deleted table .How i will do.

Ranjeet Kumar Singh

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-04 : 03:15:13
the inserted and deleted table only existed within the trigger. The structure is the same as the table of the trigger.


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-04 : 03:17:00
Here is a snippet from Books On Line

quote:
Using the inserted and deleted Tables
Two special tables are used in trigger statements: the deleted table and the inserted table. Microsoft® SQL Server™ 2000 automatically creates and manages these tables. You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for trigger actions; however, you cannot alter the data in the tables directly.

The inserted and deleted tables are used primarily in triggers to:

Extend referential integrity between tables.


Insert or update data in base tables underlying a view.


Check for errors and take action based on the error.


Find the difference between the state of a table before and after a data modification and take action(s) based on that difference.
The deleted table stores copies of the affected rows during DELETE and UPDATE statements. During the execution of a DELETE or UPDATE statement, rows are deleted from the trigger table and transferred to the deleted table. The deleted table and the trigger table ordinarily have no rows in common.

The inserted table stores copies of the affected rows during INSERT and UPDATE statements. During an insert or update transaction, new rows are added simultaneously to both the inserted table and the trigger table. The rows in the inserted table are copies of the new rows in the trigger table.

An update transaction is similar to a delete operation followed by an insert operation; the old rows are copied to the deleted table first, and then the new rows are copied to the trigger table and to the inserted table.

When you set trigger conditions, use the inserted and deleted tables appropriately for the action that fired the trigger. Although referencing the deleted table while testing an INSERT, or the inserted table while testing a DELETE does not cause any errors, these trigger test tables do not contain any rows in these cases.



Note If trigger actions depend on the number of rows a data modification effects, use tests (such as an examination of @@ROWCOUNT) for multirow data modifications (an INSERT, DELETE, or UPDATE based on a SELECT statement), and take appropriate actions.



Refer to Books On Line for more information


KH

Go to Top of Page

ranjeetsingh_6
Posting Yak Master

125 Posts

Posted - 2006-08-04 : 04:10:41
Hi
Thanks for sending detail of inserted and deleted table.

Ranjeet Kumar Singh
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-08-04 : 04:57:51
If you want to see the contents of the inserted or deleted tables you can do like this:
CREATE TRIGGER myTrigger
ON dbo.myTable
AFTER INSERT, UPDATE, DELETE
AS

SELECT * FROM inserted
SELECT * FROM deleted
The run an insert or delete-statement from query analyzer against dbo.myTable and see what happens...

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page
   

- Advertisement -