Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to use begin try and transaction with in a loop which call other stored procs. Expand / Collapse
Author
Message
Posted Monday, March 3, 2014 6:05 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 2:27 AM
Points: 52, Visits: 157
Dear ALL
I have to call few strored precedure like sp1 sp2 sp3 from spmain.

Spmain will be scheduled and it will run on time basis.

I want to trap each kind of error for loging so that i can check latter.

We need to roll back only when error comes inside the outer loop for @mid ,
so that all canges made by sub stored proces for that @mid can be rolled back,
but we want to continue with next @mid .


--------------

CREATE PROC SPMAIN
AS
BEGIN

SET XACT_ABORT ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT

SET NOCOUNT ON

--outer loop
begin LOOP (@mid is not null) -- loops through all id of a table one by one.

BEGIN TRY

BEGIN TRAN


if (@i= 1)
begin
begin loop
EXEC SP1 @mid
end loop
end

if (@i= 2)
begin

EXEC SP1 @mid

begin loop
EXEC SP2 @mid
end loop
end




COMMIT TRANSACTION;

END TRY
BEGIN CATCH

IF @@trancount > 0 ROLLBACK TRANSACTION;

EXEC LOGERROR


END CATCH;


end loop

SET XACT_ABORT OFF;

END

GO

yours sincerely
Post #1546879
Posted Wednesday, March 12, 2014 9:13 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 5:46 AM
Points: 3,998, Visits: 7,172
Man I don't know if that's real code of pseudo code (which I hope it is) because I've never seen BEGIN LOOP

Something like this might work (if you give actual code, I can provide a better example)
CREATE PROC SPMAIN AS
BEGIN
SET XACT_ABORT ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
SET NOCOUNT ON

begin LOOP (@mid is not null) -- loops through all id of a table one by one.
if (@i= 1)
begin
begin loop
BEGIN TRY
BEGIN TRANSACTION
EXEC SP1 @mid
IF @@ERROR = 0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
EXEC LOGERROR
ROLLBACK TRANSACTION;
END CATCH;
end loop
end

if (@i= 2)
begin
BEGIN TRY
BEGIN TRANSACTION
EXEC SP1 @mid
IF @@ERROR = 0
BEGIN
/* If the EXEC above succeeded, continue into the next loop */
COMMIT TRANSACTION

begin loop
BEGIN TRY
/* Start next TRY, if all is good commit the transaction and move on,
otherwise, roll back and exit the loop */
BEGIN TRANSACTION
EXEC SP2 @mid
IF @@ERROR = 0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
EXEC LOGERROR
ROLLBACK TRANSACTION;
/* There was an error in the INNER loop, don't continue to process
so exit the loop */
BREAK
END CATCH;
end loop
END
END TRY
BEGIN CATCH
EXEC LOGERROR
ROLLBACK TRANSACTION;
END CATCH;
end
end loop

SET XACT_ABORT OFF;
END
GO



______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1550522
Posted Wednesday, March 12, 2014 11:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:51 PM
Points: 35,606, Visits: 32,190
rajemessage 14195 (3/3/2014)
Dear ALL
I have to call few strored precedure like sp1 sp2 sp3 from spmain.

spmain will be scheduled and it will run on time basis.
i want to trap each kind of error for loging so that i can check latter.
we need to roll back only the @mid creating porblme from outer loop of spmain( that is the first loop) , so tha all canges made by the other sub stored proces for that id can be rolled back.


--------------

CREATE PROC SPMAIN
AS
BEGIN

SET XACT_ABORT ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT

SET NOCOUNT ON


begin LOOP (@mid is not null) -- loops through all id of a table one by one.

BEGIN TRY

BEGIN TRAN


if (@i= 1)
begin
begin loop
EXEC SP1 @mid
end loop
end

if (@i= 2)
begin

EXEC SP1 @mid

begin loop
EXEC SP2 @mid
end loop
end




COMMIT TRANSACTION;

END TRY
BEGIN CATCH

IF @@trancount > 0 ROLLBACK TRANSACTION;

EXEC LOGERROR


END CATCH;


end loop

SET XACT_ABORT OFF;

END

GO

yours sincerely


To be honest, unless the loops are necessary to support something like sending an email, I'd consider rewriting the whole ball of wax to be set-based instead of RBAR in nature. Stop thinking about rows and start thinking about columns.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1550551
Posted Friday, March 14, 2014 6:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 2:27 AM
Points: 52, Visits: 157
Few part is algorithm like loop.

and i want to roll back only if error comes any where in side outer loop,
and after error handeling i want continue with next id of outer loop.

Post #1551112
Posted Friday, March 14, 2014 7:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
rajemessage 14195 (3/14/2014)
Few part is algorithm like loop.

and i want to roll back only if error comes any where in side outer loop,
and after error handeling i want continue with next id of outer loop.



With that explanation I would agree with Jeff 100%. This could stand to have major overhaul. There is no need for looping to handle this kind of thing. You write set based logic for each step and the stuff that doesn't work you insert into an audit table so you know it "failed".


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1551161
Posted Wednesday, March 19, 2014 8:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 2:27 AM
Points: 52, Visits: 157
We are considering set base apporch for new devlopment
but following is quite complicated SP, which i can not change now.

So please tel me technically the way i have used transaction are ok or not.
Post #1552629
Posted Wednesday, March 19, 2014 8:22 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 5:46 AM
Points: 3,998, Visits: 7,172
rajemessage 14195 (3/19/2014)
We are considering set base apporch for new devlopment
but following is quite complicated SP, which i can not change now.

So please tel me technically the way i have used transaction are ok or not.
It should work, did you look at my example?


______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1552634
Posted Wednesday, March 19, 2014 8:32 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:13 AM
Points: 20,807, Visits: 32,745
Unfortunately without seeing the actual code it is hard to tell you if you are doing things correctly or not.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1552640
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse