?
Solved

SQL Stored procedure While loop or cursor error trapping

Posted on 2008-10-22
3
Medium Priority
?
2,933 Views
Last Modified: 2012-05-05
Hello experts,

I basically need a example of stored procedure transaction in a while loop or cursor so I can trap errors as it loops but continue with transaction and commit only the ones that are successful.
0
Comment
Question by:krisred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22783056
Here is a reference for doing commit/rollback on a transaction level:
http://www.4guysfromrolla.com/webtech/041906-1.shtml

The Try/Catch works in newer versions of SQL, the @@Error approach will work in SQL 2000.

You would just do something like this:
WHILE (someconition)
BEGIN
 
BEGIN TRANSACTION
-- other code here based on version, see link
-- either commit or rollback, but when done goes to next in loop
 
END

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 22783066
Using a cursor the logic flows the same.
DECLARE yourcursor CURSOR FOR ...
 
OPEN yourcursor
 
FETCH NEXT FROM yourcursor
 
WHILE @@FETCH_STATUS = 0
BEGIN
    BEGIN TRANSACTION
    -- commit or rollback
 
    FETCH NEXT FROM yourcursor
END 
 
CLOSE yourcursor
DEALLOCATE yourcursor

Open in new window

0
 
LVL 11

Assisted Solution

by:Louis01
Louis01 earned 1000 total points
ID: 22783523
Take note of the CURSOR_CLOSE_ON_COMMIT and XACT_ABORT.
--If you use a cursor:
SET CURSOR_CLOSE_ON_COMMIT OFF;
 
-- When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing. Depending upon the severity of the error, the entire transaction may be rolled back even when SET XACT_ABORT is OFF. OFF is the default setting.
SET XACT_ABORT OFF;
 
WHILE @Variable1 <= @Variable2
 BEGIN
	BEGIN TRANSACTION
	BEGIN TRY
		BEGIN TRANSACTION;
		-- Your transaction here
		COMMIT TRANSACTION;
	END TRY
	BEGIN CATCH
		ROLLBACK TRANSACTION;
	END CATCH;
 
  SET @Variable2 = @Variable2 + 1;
 END;

Open in new window

0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question