Wednesday, February 15, 2012

Error Handling in MS SQL Stored Procedures

My problem is about error handling in SQL stored procedures.
Please see my two model procedure below and the output of sp_Two.
CREATE PROCEDURE sp_One
AS
declare @.x float, @.y float, @.z float
set @.x = 1
set @.y = 0
set @.z = @.x/@.y
create procedure sp_Two
as
declare @.err int
exec sp_One
set @.err = @.@.error
if @.err <> 0 goto EH
EH:
if @.err <> 0
print @.err
else
print 'Correct'
Output
--
Server: Msg 8134, Level 16, State 1, Procedure sp_One, Line 7
Divide by zero error encountered.
8134
How do I get only the error number. I do not want the error description and
other texts. I am linking with Actuate. The above output causes the report t
o
fail.
Thanks in advance,
PrasanthHi
Give the SQL Server to raise the error and catch the number on the client
side.
Also ,please read this article
http://www.sommarskog.se/error-handling-I.html
"Prasanth" <Prasanth@.discussions.microsoft.com> wrote in message
news:DF1C119E-06DA-49C3-A738-0B52328FAC14@.microsoft.com...
> My problem is about error handling in SQL stored procedures.
> Please see my two model procedure below and the output of sp_Two.
> CREATE PROCEDURE sp_One
> AS
> declare @.x float, @.y float, @.z float
> set @.x = 1
> set @.y = 0
> set @.z = @.x/@.y
> create procedure sp_Two
> as
> declare @.err int
> exec sp_One
> set @.err = @.@.error
> if @.err <> 0 goto EH
> EH:
> if @.err <> 0
> print @.err
> else
> print 'Correct'
> Output
> --
> Server: Msg 8134, Level 16, State 1, Procedure sp_One, Line 7
> Divide by zero error encountered.
> 8134
> How do I get only the error number. I do not want the error description
and
> other texts. I am linking with Actuate. The above output causes the report
to
> fail.
> Thanks in advance,
> Prasanth

No comments:

Post a Comment