Solved

sp_executesql Trap Error

Posted on 2010-11-18
5
471 Views
Last Modified: 2012-06-21
Hi,

I am building SQL statements in a loop in SQL 2000 then using sp_executesql to execute each statement, if an individual statement fails I want to log the error and move on to the next one, but I can't trap the error.  At the moment if an individual statement fails the loop just stops.  

Note:  I am using SQL 2000 NOT 2005 or 2008

Any ideas?

thanks
0
Comment
Question by:kinton
5 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 34162812
Why does it fails? print the dynamic sql statement and try to correct the error
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34162813
What error is it failing with?
0
 
LVL 2

Author Comment

by:kinton
ID: 34162840
IT's not the error that I am struggling with, I want to be able to catch errors should they appear in the future as this job will run every night.  At the moment I am deliberately making one statement in the loop fail so I can catch the error.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34162861
Within SQL there there are 3 types of error Information/Warning/Error. SQL Server can only continue running if Information or Warning messages appear.

In SQL 2005 on the other hand you would be able to use a TRY / CATCH block for this.

With SQL 2000 the most you will be able to do is look at the job history for what error occured.
0
 
LVL 8

Accepted Solution

by:
lomo74 earned 500 total points
ID: 34175047
first of all
set xact_abort off
otherwise transaction will stop whenever an error occurs
then you can lookup error in sysmessages
look at very trivial example

set xact_abort off

declare @i int, @err int

set @i = 3

while @i > 0 begin
	exec sp_executesql @statement = N'select 1/0 -- divide by zero error'
	--remember error
	select @err = @@error
	--log error if necessary
	if @err <> 0 begin
/*		insert into yourlogfile ([time], [description])    */
		select getdate(), [description] from sysmessages where error = @err
	end
	--next loop
	set @i = @i - 1
end

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 5 54
Order by but want it in specific order 2 34
My Query is not giving correct result. Please help 5 51
Anyway to make these 2 SQL statements into one? 13 43
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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