Solved

sp_executesql Trap Error

Posted on 2010-11-18
5
467 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard 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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now