Unix Technical Forum

@@Error not catching error.

This is a discussion on @@Error not catching error. within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all, I want to catch error in stored procedure and return error message. I want to catch error ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 01:14 PM
trialproduct2004@yahoo.com
 
Posts: n/a
Default @@Error not catching error.

Hi all,

I want to catch error in stored procedure and return error message.
I want to catch error 'Syntax error converting the varchar value 'a'
to a column of data type int.' Means error occuring if i enter wrong
value.

Say suppose i have statment like

select * from emp where rowid = 'a'
PRINT @@ERROR
print 'reach'

here rowid is integer value so i am getting above mention error.

So what i am expecting is it should print error and then print 'reach'
which is not happening.
can anyone tell me reason behind this and how to overcome this
problem.

thanks in advance.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 01:15 PM
Erland Sommarskog
 
Posts: n/a
Default Re: @@Error not catching error.

(trialproduct2004@yahoo.com) writes:
> I want to catch error in stored procedure and return error message.
> I want to catch error 'Syntax error converting the varchar value 'a'
> to a column of data type int.' Means error occuring if i enter wrong
> value.
>
> Say suppose i have statment like
>
> select * from emp where rowid = 'a'
> PRINT @@ERROR
> print 'reach'
>
> here rowid is integer value so i am getting above mention error.
>
> So what i am expecting is it should print error and then print 'reach'
> which is not happening.
> can anyone tell me reason behind this and how to overcome this
> problem.


If you are on SQL 2005, you need to use TRY-CATCH. If you are using SQL
2000, you first need to upgrade to SQL 2005. In SQL 2000 you cannot detect
this error, because the entire batch is aborted because of the error.
If you want to know more about error handling in SQL 2000, I have an
article on my web site: http://www.sommarskog.se/error-handling-I.html.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 01:15 PM
Erland Sommarskog
 
Posts: n/a
Default Re: @@Error not catching error.

(trialproduct2004@yahoo.com) writes:
> I want to catch error in stored procedure and return error message.
> I want to catch error 'Syntax error converting the varchar value 'a'
> to a column of data type int.' Means error occuring if i enter wrong
> value.
>
> Say suppose i have statment like
>
> select * from emp where rowid = 'a'
> PRINT @@ERROR
> print 'reach'
>
> here rowid is integer value so i am getting above mention error.
>
> So what i am expecting is it should print error and then print 'reach'
> which is not happening.
> can anyone tell me reason behind this and how to overcome this
> problem.


If you are on SQL 2005, you need to use TRY-CATCH. If you are using SQL
2000, you first need to upgrade to SQL 2005. In SQL 2000 you cannot detect
this error, because the entire batch is aborted because of the error.
If you want to know more about error handling in SQL 2000, I have an
article on my web site: http://www.sommarskog.se/error-handling-I.html.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:24 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com