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
 General SQL Server Forums
 New to SQL Server Programming
 Need help with trigger

Author  Topic 

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-09 : 21:01:06
[code]CREATE TRIGGER [dbo].Contact_DELETE
ON Contact
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Phone WHERE ContactId = ContactId --Should refer to the desired delete-row's ContactId
DELETE FROM Contact WHERE ContactId = ContactId --Same as above
--I want that if the contact deletion fails, it should revert the deletion of the phones too and print an error.
END
GO[/code]

Shimmy

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-09 : 21:22:59
quote:
Originally posted by weitzhandler

CREATE TRIGGER [dbo].Contact_DELETE
ON Contact
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Phone WHERE ContactId = ContactId --Should refer to the desired delete-row's ContactId
DELETE FROM Contact WHERE ContactId = ContactId --Same as above
--I want that if the contact deletion fails, it should revert the deletion of the phones too and print an error.
END
GO


Shimmy



Can you elaborate what exactly you are trying to do?your post is not clear.
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-09 : 21:35:39
I have 2 tables: Contact and phone.
Phone is made to allow 1 contact to have many phones by having (in the phone table) a contactID col referring to its owner contact.

now I want that on contact delete it shouldn't raise an error that phones for this contact exist, I want it should first delete all the phones than the contact itself.

but I want also that if the contact deletion fails due to some other issue (for example if this contact is referenced in another table etc.), it should roll back the whole transaction including the phone deletion.

and sorry about my code from before, please accept this update:
CREATE TRIGGER [dbo].[Contact_DELETE]
ON [dbo].[Contact]
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ContactId int
SELECT @ContactId = ContactId FROM DELETED
DELETE FROM Phone Where Contact = @ContactId
DELETE FROM Contact Where ContactId = @ContactId --If this line fails to execute it should rollback previous line's transaction as well
END


Shimmy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 02:32:06
put them in a transaction and rollback if @@ERROR >0
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-10 : 20:46:40
quote:
Originally posted by visakh16

put them in a transaction and rollback if @@ERROR >0




And how do I do that?

Shimmy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-11 : 13:32:29
use BEGIN TRAN tranname
....your code

IF @@ERROR >0
ROLLBACK TRAN transname
ELSE
COMMIT TRAN tranname
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-12 : 07:05:58
I did it like this so I can get information about the error:


ALTER TRIGGER [dbo].[Contact_DELETE]
ON [dbo].[Contact]
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;

DECLARE @ContactId int
SELECT @ContactId = ContactId FROM deleted

BEGIN TRY
BEGIN TRANSACTION
DELETE FROM Phone Where Contact = @ContactId
DELETE FROM Contact Where ContactId = @ContactId
COMMIT TRAN
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN
DECLARE @ERROR_MESSAGE nvarchar(2048), @ERROR_SEVERITY int
SELECT @ERROR_MESSAGE = ERROR_MESSAGE(), @ERROR_SEVERITY = ERROR_SEVERITY()
RAISERROR(@ERROR_MESSAGE, @ERROR_SEVERITY, 1)
END CATCH
END


Shimmy
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-12 : 07:08:41
Why? An after trigger runs in same scope as original query.

This should be enough
CREATE TRIGGER dbo.trgContact_DELETE
ON dbo.Contact
AFTER DELETE
AS

SET NOCOUNT ON

DELETE p
FROM Phone AS p
INNER JOIN inserted AS i ON i.ContactID = p.ContactID



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-12 : 07:09:09
And you also can make use of CASCADING DELETE.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-03-12 : 07:16:48
Will check what it is.
Thanks

Shimmy
Go to Top of Page
   

- Advertisement -