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


Trap Error


Trap Error

Author
Message
ashiknihal
ashiknihal
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 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


Sql Junkie-204042
Sql Junkie-204042
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 168
Do a BOL lookup for @@ERROR
Scott Beckstead
Scott Beckstead
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 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

Mike Dominick
Mike Dominick
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 33

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 -- failure

declare @x int

declare @y int

declare @Results int

set @x = 2

set @y = 0

set @myerror =0

if @myerror = 0

begin

--Continue doing part

--1st part of step 1

set @Results = @x / @y

set @myError = @@error -- Must be called as the very next statment

--2nd part of step 1

set @Results = @x + @y

end

--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 = 2

set @y = 0

begin try

--1st part of step 1

set @Results = @x / @y

set @myError = @@error -- Must be called as the very next statment

--2nd part of step 1

set @Results = @x + @y

end try

begin catch

Print 'Error occured in first part of procedure'

end catch

begin try

--Step 2

print 'Part 2 results: ' + cast(@Results as varchar(10))

end try

begin catch

print 'error occured in 2nd part of procedure'

end catch





Scott Beckstead
Scott Beckstead
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 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

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