Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use begin try and transaction with in a loop which call other stored procs.


How to use begin try and transaction with in a loop which call other stored procs.

Author
Message
rajemessage 14195
rajemessage 14195
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 329
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
MyDoggieJessie
MyDoggieJessie
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4909 Visits: 7365
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 :-D

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" ;-)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45248 Visits: 39932
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
rajemessage 14195
rajemessage 14195
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 329
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
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)
rajemessage 14195
rajemessage 14195
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 329
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.
MyDoggieJessie
MyDoggieJessie
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4909 Visits: 7365
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" ;-)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24255 Visits: 37978
Unfortunately without seeing the actual code it is hard to tell you if you are doing things correctly or not.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search