Solved

sp_executesql Trap Error

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

16 Experts available now in Live!

Get 1:1 Help Now