SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ignore Error in T-SQL


Ignore Error in T-SQL

Author
Message
C.K.Shaiju
C.K.Shaiju
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3906 Visits: 711
Hi,

How do we ignore an error in T-SQL?

For e.g.:- The following code throw an error once the @lCounter reach at 15 and will come out. Requirement is it should go till 1000. Even if there are errors in between.


DECLARE @lCounter SMALLINT
DECLARE @lError SMALLINT

SET @lCounter = 0
WHILE (@lCounter <= 1000 )
BEGIN
SELECT CONVERT(VARCHAR, GETDATE(), @lCounter)
SET @lCounter = @lCounter + 1
END



Thanks in advance

_____________________________________________
One ounce of practice is more important than tonnes of dreams
Sean Lange
Sean Lange
SSC Guru
SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)

Group: General Forum Members
Points: 270401 Visits: 19771
C.K.Shaiju (9/24/2013)
Hi,

How do we ignore an error in T-SQL?

For e.g.:- The following code throw an error once the @lCounter reach at 15 and will come out. Requirement is it should go till 1000. Even if there are errors in between.


DECLARE @lCounter SMALLINT
DECLARE @lError SMALLINT

SET @lCounter = 0
WHILE (@lCounter <= 1000 )
BEGIN
SELECT CONVERT(VARCHAR, GETDATE(), @lCounter)
SET @lCounter = @lCounter + 1
END



Thanks in advance


You can't ignore an error. You can however handle it using Try/Catch.

I hope your code is just for an example of how to force an error because a loop is not very efficient in sql.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Craig Wilkinson
Craig Wilkinson
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39278 Visits: 8629
C.K.Shaiju (9/24/2013)
Hi,

How do we ignore an error in T-SQL?

For e.g.:- The following code throw an error once the @lCounter reach at 15 and will come out. Requirement is it should go till 1000. Even if there are errors in between.


DECLARE @lCounter SMALLINT
DECLARE @lError SMALLINT

SET @lCounter = 0
WHILE (@lCounter <= 1000 )
BEGIN
SELECT CONVERT(VARCHAR, GETDATE(), @lCounter)
SET @lCounter = @lCounter + 1
END



Thanks in advance


I can't think of any reason why you'd want to do that. . . but something like this: -

DECLARE @lCounter SMALLINT, @lError SMALLINT;

SET @lCounter = 0;
WHILE (@lCounter <= 1000 )
BEGIN;
BEGIN TRY;
SELECT CONVERT(VARCHAR, GETDATE(), @lCounter);
SET @lCounter = @lCounter + 1;
END TRY
BEGIN CATCH;
SET @lCounter = @lCounter + 1;
IF @lCounter >= 1000
BEGIN;
RAISERROR('Error',16,1);
BREAK;
END;
END CATCH;
END;




Forever trying to learn
For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

If you litter your database queries with nolock query hints, are you aware of the side effects? Try reading a few of these links...
(*) Missing rows with nolock
(*) Allocation order scans with nolock(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock

Craig Wilkinson - Software Engineer
LinkedIn
C.K.Shaiju
C.K.Shaiju
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3906 Visits: 711
Sean LangeI hope your code is just for an example of how to force an error because a loop is not very efficient in sql.


Yes, An example

Cadavre
I can't think of any reason why you'd want to do that


Would like to know whether we can do that or not. Remember some old vb code with "on error resume next".

Thanks much for the reply.

_____________________________________________
One ounce of practice is more important than tonnes of dreams
Sean Lange
Sean Lange
SSC Guru
SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)SSC Guru (270K reputation)

Group: General Forum Members
Points: 270401 Visits: 19771
C.K.Shaiju (9/24/2013)
[quote]
Would like to know whether we can do that or not. Remember some old vb code with "on error resume next".

Thanks much for the reply.


No you can't simply ignore errors like vb used to do. The closest you can get is the example posted above. :-)

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
C.K.Shaiju
C.K.Shaiju
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3906 Visits: 711

No you can't simply ignore errors like vb used to do. The closest you can get is the example posted above. :-)


Ok Sean, Thanks.

_____________________________________________
One ounce of practice is more important than tonnes of dreams
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search