SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Trigger AFTER DELETE
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mikeSQLmike
Starting Member

Portugal
2 Posts

Posted - 04/09/2012 :  05:40:00  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 04/09/2012 :  07:38:24  Show Profile  Reply with Quote
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

USA
5072 Posts

Posted - 04/09/2012 :  11:40:53  Show Profile  Visit russell's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000