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
 General SQL Server Forums
 New to SQL Server Programming
 How to delete this in a trigger?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 04/03/2013 :  23:01:53  Show Profile  Reply with Quote
Hi there,

I have table1 with column Code which is related to table2 as PK-FK relations. So if I need delete a row in table1 I need delete all FK-ed rows in table2 first. How can I do this in a trigger? If I use

Create Trigger Code_Delete
On table1
After Delete


but this is supposed to start work after items in table1 is successfully removed, right? But I need remove all the related rows intab2 first. Plz guide me out of this. Thanks in advance.

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/04/2013 :  00:20:19  Show Profile  Reply with Quote
Create Trigger Code_Delete
On table1
INSTEAD OF DELTE
AS
BEGIN
--Your sql code to delete data from child tables and then from parent
END
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 04/04/2013 :  00:29:30  Show Profile  Reply with Quote
Thanks bandi. But I am no sure how to write the code here.
If I want to delete 'ABC' from table1 and I need know ID of 'ABC' and then remove all rows in table2 with same ID. But how can I get ID here? Plz show me more details. Thanks again.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 04/04/2013 :  00:36:38  Show Profile  Reply with Quote
Hi Bandi, I got it got ID from DELETED! Ha, Thank you!
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/04/2013 :  00:39:57  Show Profile  Reply with Quote
quote:
Originally posted by allan8964

Hi Bandi, I got it got ID from DELETED! Ha, Thank you!


Welcome

Note: Refer the link to get knowledge on TRIGGERS
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server

--
Chandu

Edited by - bandi on 04/04/2013 00:42:40
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/04/2013 :  00:44:36  Show Profile  Reply with Quote
Alternative solution is to use ON DELETE CASCADE option for the integrity maintenance of data
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.08 seconds. Powered By: Snitz Forums 2000