|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 09, 2008 5:56 AM
Points: 5,
Visits: 29
|
|
How To trap error in SQL Server? E.g : While executing a procedure an error comes at first part of the procedure. So when error comes i don't want to execute first part, quit from there then second part of the procedure should be executed. Normally when error comes subsequent statement will not be executed.How to handle this error. Regards Ashik Nihal M.s
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Sunday, October 17, 2010 4:18 PM
Points: 360,
Visits: 168
|
|
| Do a BOL lookup for @@ERROR
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, December 07, 2010 5:44 PM
Points: 148,
Visits: 24
|
|
I understand that it may be a BOL question, but why not give a short answer then refer to the docs. I think it's rather rude just to answer RTFM without at least a courteous salutation.
Scott
Kindest Regards,
Scott Beckstead
"We cannot defend freedom abroad by abandoning it here at home!" Edward R. Murrow
scottbeckstead.com
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, July 12, 2012 6:43 PM
Points: 175,
Visits: 32
|
|
Below are two quick examples. The short answer is that if you are using SQL Server 2000 you need to user @@error, if you are in SQL 2005 you can use @@Error or a try catch block.
@@error must be called immediately after the statement executed or if you executing a stored procedure you set your variable holding the error results = to stored procedure. For example: select @X + @Y select @myerror = @@Error or execute @myerror = <stored procedure name) Below are two complete example. copy them into QA for SSMS and give them a try. Hope this helps, Mike print '****** SQL Server 2000/2005 using @@Error'declare @myerror bit --0 success, 1 -- failuredeclare @x intdeclare @y intdeclare @Results intset @x = 2set @y = 0set @myerror =0if @myerror = 0begin--Continue doing part --1st part of step 1set @Results = @x / @yset @myError = @@error -- Must be called as the very next statment--2nd part of step 1set @Results = @x + @yend --Step 2 print 'Part 2 results: ' + cast(@Results as varchar(10))--SQL 2005 using try catch block print '****** SQL Server 2005 using try catch'set @x = 2set @y = 0begin try--1st part of step 1set @Results = @x / @yset @myError = @@error -- Must be called as the very next statment--2nd part of step 1set @Results = @x + @yend trybegin catchPrint 'Error occured in first part of procedure'end catch begin try--Step 2 print 'Part 2 results: ' + cast(@Results as varchar(10))end trybegin catchprint 'error occured in 2nd part of procedure'end catch
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, December 07, 2010 5:44 PM
Points: 148,
Visits: 24
|
|
Thank you Mike. Very Helpful. Though I did RTFM and I wasn't the original requester Courtesy goes a long way.
Kindest Regards,
Scott Beckstead
"We cannot defend freedom abroad by abandoning it here at home!" Edward R. Murrow
scottbeckstead.com
|
|
|
|