| 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 ContactIdDELETE 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.ENDGO[/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 ContactIdDELETE 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.ENDGO Shimmy
Can you elaborate what exactly you are trying to do?your post is not clear. |
 |
|
|
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 intSELECT @ContactId = ContactId FROM DELETEDDELETE FROM Phone Where Contact = @ContactIdDELETE FROM Contact Where ContactId = @ContactId --If this line fails to execute it should rollback previous line's transaction as wellEND Shimmy |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-11 : 13:32:29
|
| use BEGIN TRAN tranname....your codeIF @@ERROR >0ROLLBACK TRAN transnameELSECOMMIT TRAN tranname |
 |
|
|
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 CATCHEND Shimmy |
 |
|
|
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 enoughCREATE TRIGGER dbo.trgContact_DELETEON dbo.ContactAFTER DELETEAS SET NOCOUNT ONDELETE pFROM Phone AS pINNER JOIN inserted AS i ON i.ContactID = p.ContactID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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" |
 |
|
|
weitzhandler
Yak Posting Veteran
64 Posts |
Posted - 2009-03-12 : 07:16:48
|
| Will check what it is.ThanksShimmy |
 |
|
|
|