| Author |
Topic |
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-17 : 21:58:32
|
Hi.I have a sp that will do delete in 3 steps.I was wondering if there is a better solution because if a step after the first one fails then (if i'm not mistaken) all data one the previous step would have been deleted and the other data will be left hanging.Connections are: operations.animalid FK on animal.iddrugsoperations.operationsid fk on operations.idThe queries are as follows,without xtra if's etc.I was thinking to do a while but it seemed more risky.Anyway.Step 1delete from drugsoperations where drugsoperations.operationsidin(select id from operations where animalid in (select id from animalswhere animals.flag =99)) Step2delete from operations where operations.id in(SELECT operations.id FROM drugsoperations RIGHT JOIN operations ON drugsoperations.operationsid = operations.idwhere drugsoperations.id is null) Step 3delete from animals where animals.id in(SELECT animals.idFROM Animals left JOIN operations ON Animals.id = operations.animalidwhere operations.id is null and animals.flag = 99) |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-03-17 : 22:07:46
|
| using begin trancommit tran or rollback tranThat guarantees either ALL delete actions are committed or NONE |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-17 : 22:21:32
|
| Hi.Should i but it above the first query(begin tran T;)and at the end of the last query(Commit tran t;)There are many begin end and if's and returns inside the sp, will i need to change anything on them?Thanks |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-17 : 22:35:19
|
| Also i'm using a statement right after deleteset @vlaueX=@@rowcountIf after that i do :IF @@ERROR <> 0 BEGIN ROLLBACK .....Will i get the @@error or i first must pass it to a variable?Thanks. |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-17 : 23:16:16
|
| I solved it another way but i will be good to know.Thanks. |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-03-17 : 23:26:19
|
| There are many begin end and if's and returns inside the sp, will i need to change anything on them?May notI prefer to use with try-catch. But I think you can do with @@ERROR tooex:begin tran -- delete 1 -- delete 2 -- delete 3commit tranif @@ERROR <> 0 rollback tranif you can just post the code here, it is easier for people to present a proper solution |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-17 : 23:39:51
|
Hi.I'm doing the exact same thing except i use the rollback, first.Is this ok?IF @@ERROR <>0 BEGIN PRINT 'Error Occured'Rollback tran t1;return 99 ENDCommit tran t1; |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-03-18 : 00:18:47
|
quote: Originally posted by sapator Hi.I'm doing the exact same thing except i use the rollback, first.Is this ok?IF @@ERROR <>0 BEGIN PRINT 'Error Occured'Rollback tran t1;return 99 ENDCommit tran t1;
It's fine. Most code I've seen does it in the reverse though.I do this type of thing in a TRY/CATCH so that I can gracefully catch the errors. Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog"Let's begin with the premise that everything you've done up until this point is wrong." |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-18 : 00:35:47
|
| For try catch do you need to set your SQL so it supports .net statements? |
 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-18 : 00:37:35
|
| I've found a useful link.I suppose is right on the T-SQL to use after all?http://msdn.microsoft.com/en-us/library/ms175976.aspx |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2010-03-18 : 06:12:58
|
| Thanks i'll have an extensive look ! |
 |
|
|
|