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 2008 Forums
 SQL Server Administration (2008)
 Trigger AFTER DELETE

Author  Topic 

mikeSQLmike
Starting Member

2 Posts

Posted - 2012-04-09 : 05:40:00
hi to all,
I need a trigger that deletes records from a table when they are deleted from another table.
The records to be deleted were inserted into user table by copying the table where I want to control lines erased.
In summary:
I have a table of detailed records and when a registration is deleted need to be deleted the same record in another table that contains the same records but summarized.
Already grateful.

Mike Teixeira

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-09 : 07:38:24
You can create a trigger on the detailed records table. In the trigger code, you will have access to the rows that were deleted via the DELETED virtual table. You can examine that table and decide how to update/delete rows from the summary table.

One thing to keep in mind is that the DELETED virtual table can have more than one row. This comes about because the trigger gets called once for each delete operation regardless of the number of rows affected by the delete action.

http://msdn.microsoft.com/en-us/library/ms189799(v=sql.105).aspx
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-04-09 : 11:40:53
I've said it a million times and I'll say it again: this is a bad idea!

It is far better to use stored procedures to to all of the deletes.

Triggers that delete data and cascading keys cause unexpected data loss and difficult to track down bugs.
Go to Top of Page
   

- Advertisement -