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.
| Author |
Topic |
|
deadfish
Starting Member
38 Posts |
Posted - 2003-05-18 : 07:00:11
|
| I have create some stored procedure as follows:create procedure sp111asbegin...end create procedure sp11asbegin....esec sp111endcreate procedure sp12asbegin...endcreate procedure sp1asbegin.....exec sp11exec sp12endif there is an error raised in sp12, I have to rollback all transaction in all store procedure. How should I put the 'begin tran' and 'commit tran' in order to achievel this?Thanks a lot! |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2003-05-18 : 11:56:40
|
| You must commit or rollback a transaction in its procedure. Why can't you put all statements into one procedure or batch? Maybe post some code here and you'll get some viable solutions.Sarah Berger MCSD |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-05-18 : 12:01:34
|
| USE NorthwindGODROP TABLE TestTransGODROP PROCEDURE spOneGODROP PROCEDURE spTwoGODROP PROCEDURE spThreeGODROP PROCEDURE spMainGOCREATE TABLE TestTrans ([ID] INT)GOCREATE PROCEDURE spOne AS SET NOCOUNT ONINSERT INTO TestTrans([ID]) VALUES(1)GOCREATE PROCEDURE spTwo AS SET NOCOUNT ONINSERT INTO TestTrans([ID]) VALUES(2)GOCREATE PROCEDURE spThree AS SET NOCOUNT ON--This will fail because of datatypeINSERT INTO TestTrans([ID]) VALUES('a')GOCREATE PROCEDURE spMain ASSET NOCOUNT ONDECLARE @ErrorID INTBEGIN TRANSACTION EXEC spOneIF @@ERROR <> 0 ROLLBACK TRANSACTION EXEC spTwoIF @@ERROR <> 0 ROLLBACK TRANSACTION EXEC spThreeIF @@ERROR <> 0 ROLLBACK TRANSACTION COMMIT GOEXEC spMainGOSELECT * FROM TestTrans |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-05-18 : 12:33:05
|
| More likelyCREATE PROCEDURE spMain AS SET NOCOUNT ON DECLARE @ErrorID INT BEGIN TRANSACTION EXEC spOne IF @@ERROR <> 0 beginROLLBACK TRANSACTION raiserror('failed',16,-1)returnendEXEC spTwo IF @@ERROR <> 0 beginROLLBACK TRANSACTION raiserror('failed',16,-1)returnendEXEC spThree IF @@ERROR <> 0 beginROLLBACK TRANSACTION raiserror('failed',16,-1)returnendCOMMIT GO EXEC spMain GO And put the same error processing (without the rollback maybe - depends if they also have transaction control) after each sql statement in the subordinate SPs.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
deadfish
Starting Member
38 Posts |
Posted - 2003-05-19 : 10:53:24
|
| I've got the idea, thanks everyone! ^____^ |
 |
|
|
|
|
|