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
 Trg prevent deletion if....
 New Topic  Reply to Topic
 Printer Friendly
Previous Page
Author Previous Topic Topic Next Topic
Page: of 2

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 04/02/2013 :  09:56:46  Show Profile  Reply with Quote
try

DELETE FROM product WHERE name = 'tosti'

is it deleting?

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

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 04/03/2013 :  06:44:17  Show Profile  Reply with Quote
Still unable to understand the requirements ..but guessing this might be something you're looking for

ALTER TRIGGER articleMustHaveTwoIngridients
ON product
INSTEAD OF DELETE
AS
BEGIN
IF EXISTS ( SELECT 1 FROM product p
JOIN deleted d
ON d.name = p.name
GROUP BY p.name
HAVING COUNT(*) > 2)
BEGIN
DELETE p
FROM Product p
INNER JOIN deleted d
ON d.name = p.name
AND d.ingridient = p.ingridient
END
ELSE IF EXISTS ( SELECT 1 FROM product p
JOIN deleted d
ON d.name = p.name
GROUP BY p.name
HAVING COUNT(*) < 3)
Begin
SELECT 'Cannot Delete products'
Rollback
END
END


Cheers
MIK
Go to Top of Page

chihwahli
Starting Member

11 Posts

Posted - 04/03/2013 :  07:35:56  Show Profile  Reply with Quote
@visakh16: if I use your trigger with: DELETE FROM product WHERE name = 'tosti'
it works with your delete statement, but not when I try:

DELETE FROM product WHERE ingridient = 'ham'
DELETE FROM product WHERE ingridient = 'bread'
DELETE FROM product WHERE ingridient = 'cheese'

Then 2 rows with tosti stay in database. But that is ok. You gave a working query!
Thank you Visakh.


@MIK_2008: Your query works both with single DELETE statement:
DELETE FROM product WHERE name = 'tosti'

And it works with multiple deletestatements:
DELETE FROM product WHERE ingridient = 'ham'
DELETE FROM product WHERE ingridient = 'bread'
DELETE FROM product WHERE ingridient = 'cheese'

Exellent!

My requirements don't reflect any real life thing. I just came across some old SQL test question. Tried it, did not know how.
Basicly i just wrote the requirements as what must happen when I do A and what happens when I do B, nothing more, nothing less.


Now I have figure out why your query's (@visakh16 and MIK_2008) work... did not work with indead delete triggers yet....

Thumbs up you both! @(@visakh16 and MIK_2008


Edited by - chihwahli on 04/03/2013 07:45:00
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 04/03/2013 :  07:40:54  Show Profile  Reply with Quote
Well my query was just an extention to Visakh solution. You may tune it further as per your requirements. And have a good time

Cheers
MIK
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Previous Page
 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