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 2005 Forums
 Other SQL Server Topics (2005)
 Delete Trigger
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

taniarto
Starting Member

Indonesia
27 Posts

Posted - 10/12/2013 :  00:32:12  Show Profile  Reply with Quote
I Have 2 Tables :
Table one is master and table 2 is stock as follow :
Master :
ID Desc unit
002 Ball Pcs
001 Book pcs

Stock
ID Qty
002 0
001 0

I want to make delete trigger, which is if the master where delete the stock will be deleted too. I try but still not working..please help..

thanks


visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/12/2013 :  04:52:17  Show Profile  Reply with Quote
you mean deleting a record in Master causing delete of the corresponding records in Stock

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 10/14/2013 :  05:28:17  Show Profile  Reply with Quote
Yes...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/14/2013 :  07:05:50  Show Profile  Reply with Quote
You can do this in two ways

1 Making FOREIGN KEY in Stock table with ON DELETE CASCADE option on ID column
2 Creating a trigger like below

CREATE TRIGGER Trg_TodayUpStock
ON Master
FOR DELETE
AS
BEGIN 

DELETE s
FROM Stock s
WHERE EXISTS (SELECT 1
FROM DELETED
WHERE ID = s.ID)

END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.05 seconds. Powered By: Snitz Forums 2000