Error handling question

I have the following code to loop through a list of names in Table1. I want to add error handling, so that if error occurs while executing usp_Storedproc1, the execution moves to the next name in the cursor without breaking the program flow.

declare lcursor cursor read_only
				for select DISTINCT Name from Table1 where flag = 1 AND type='Test'

			open lcursor
			fetch next from lcursor into @Name
			while @@fetch_status = 0
				begin
					exec usp_Storedproc1 @Name
					fetch next from lcursor into @lservername
				end
			close lcursor
			deallocate lcursor

Open in new window

LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
If you are using SQL 2005 or higher, you can surround the procedure call in a try-catch:


while @@fetch_status = 0
   begin

        BEGIN TRY
             exec usp_Storedproc1 @Name
        END TRY
        BEGIN CATCH
             print 'Error caught while processing ' + @Name
        END CATCH

      fetch next from lcursor into @lservername
   end
0
MisbahCommented:
just use try and catch block around that statement :

declare lcursor cursor read_only
				for select DISTINCT Name from Table1 where flag = 1 AND type='Test'

			open lcursor
			fetch next from lcursor into @Name
			while @@fetch_status = 0
				begin
begin try
					exec usp_Storedproc1 @Name
end try
beging catch
SELECT ERROR_NUMBER() AS ErrorNumber;
end catch
					fetch next from lcursor into @lservername
				end
			close lcursor
			deallocate lcursor

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aneesh RetnakaranDatabase AdministratorCommented:
if you are using sql 2000

begin
 exec usp_Storedproc1 @Name
 IF @@error <> 0
 BEGIN
             Print 'Error '
             
 END
 fetch next from lcursor into @lservername
end
0
knightEknightCommented:
Since my answer and the one provided by Dr-Hussain are essentially the same, a point-split is appropriate.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.