all groups > sql server programming > november 2005 >
You're in the

sql server programming

group:

Check error question.


Check error question. ReTF
11/13/2005 9:45:21 PM
sql server programming:
Hi All,

I need check if one of thise SP had error.

EXEC gerar_dnum_sp @dnum OUTPUT
EXEC gerar_auto_sp @auto OUTPUT
EXEC gerar_aute_sp @dnum, @auto, @aute OUTPUT

Then I did this:

EXEC gerar_dnum_sp @dnum OUTPUT
IF @@ERROR <> 0
BEGIN
....
END

EXEC gerar_auto_sp @auto OUTPUT
IF @@ERROR <> 0
BEGIN
....
END

EXEC gerar_aute_sp @dnum, @auto, @aute OUTPUT
IF @@ERROR <> 0
BEGIN
....
END

But I need 3 IF @@ERROR <> 0..., i would like know if have way to check all
in one time.

like this:

EXEC gerar_dnum_sp @dnum OUTPUT
EXEC gerar_auto_sp @auto OUTPUT
EXEC gerar_aute_sp @dnum, @auto, @aute OUTPUT

IF(error in same one)
BEGIN
do same thing
END

Thanks to help

Re: Check error question. ReTF
11/14/2005 12:00:00 AM
Hi,
I did see
BEGIN TRY ...

I think that this is good to do what I need.

See:

BEGIN TRY

EXEC gerar_dnum_sp @dnum OUTPUT
EXEC gerar_auto_sp @auto OUTPUT
EXEC _aute_sp @dnum, @auto, @aute OUTPUTTRY

--and more....

END TRY

BEGIN CATCH

-- if error do samething here

END CATH



What you think?



Thanks

[quoted text, click to view]

Re: Check error question. Hugo Kornelis
11/14/2005 12:47:57 AM
[quoted text, click to view]

(snip)
[quoted text, click to view]

Hi ReTF,

You'll have to do SOME work in between the calls, even though you don't
need to cpopy the complete error handling three times.

Most simple:

DECLARE @err1 int, @err2 int, @err3 int
EXEC gerar_dnum_sp @dnum OUTPUT
SET @err1 = @@ERROR
EXEC gerar_auto_sp @auto OUTPUT
SET @err2 = @@ERROR
EXEC gerar_aute_sp @dnum, @auto, @aute OUTPUT
SET @err3 = @@ERROR

IF @err1 <> 0 OR @err2 <> 0 OR @err3 <> 0
BEGIN
....
END


Or, better yet because you mostly don't want to keep on executing code
once an error is found:

DECLARE @err int
EXEC gerar_dnum_sp @dnum OUTPUT
SET @err = @@ERROR
IF @err = 0
BEGIN
EXEC gerar_auto_sp @auto OUTPUT
SET @err = @@ERROR
END
IF @err = 0
BEGIN
EXEC gerar_aute_sp @dnum, @auto, @aute OUTPUT
SET @err = @@ERROR
END
IF @err <> 0
BEGIN
....
END


Best, Hugo
--

Re: Check error question. v-sguo NO[at]SPAM online.microsoft.com
11/14/2005 8:17:13 AM
Hello,

You may use the return status:

DECLARE @retstat1 int, @retstat2 int, @retstat3 int
DECLARE @dnum int, @auto int, @aute int
EXEC @retstat1=gerar_dnum_sp @dnum OUTPUT
EXEC @retstat2=gerar_auto_sp @auto OUTPUT
EXEC @retstat3=gerar_aute_sp @dnum, @auto, @aute OUTPUT

if @retstat1<>0 or @retstat2<>0 or @retstat3<>0
begin
print 'error'
end

I hope the information is helpful.

Sophie Guo
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security

=====================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Re: Check error question. Rebecca York
11/15/2005 12:00:00 AM
TRY ... CATCH is a good way of doing error handling if you have SQL 2005.

Otherwise, (sql7/2000)

You should put an @@ERROR catch after every failable statement within the
stored proc, if the @@ERROR return it to the caller.

Checking @@ERROR after the SP wont guarentee you'll catch the error (as it
may have run other commands since the error and reset it back to 0)

EXEC @re1 = gerar_dnum_sp @dnum OUTPUT
EXEC @re2 = gerar_auto_sp @auto OUTPUT
EXEC @re3 = gerar_aute_sp @dnum, @auto, @aute OUTPUTTRY

IF ( @re1 != 0 OR @re2 != 0 OR @re3 != 0 )
BEGIN
PRINT '...'
END


[quoted text, click to view]

AddThis Social Bookmark Button