Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored procedure error handler


Stored procedure error handler

Author
Message
pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 982
Hi masters,

i have learned in this site that if i want to protect my stored procedures, from executing some statements even if other fails (inside the stored proc) that i can do like below:



create proc test
as
declare @ierror int

begin transaction

update table1 set name ='paul'
set @ierror =@@error

if @ierror = 0
begin
update table2 set address ='xpto'
set @ierror =@@error
end


if @ierror = 0
begin
update table3 set address2 ='xpto2'
set @ierror =@@error
end


if @ierror=0
begin
commit transaction
end

GO




If i am right, the stored procedure will only execute the updates, if non of them(updates) fail.

I will not use the "xact_abort on", because i think that it is no need in this case.


I don't need that the stored procedure stops if it raises any error. i only need that it
can not execute any statement, if one of the statements fails.

Is this correct?




If the anwser is yes, then i would like to make other questions about errors in SQL Server 2000.
I know that the errors that are raised, have severity levels.


But i don't know if what i will write below is correct:


1 )taking the stored proc test on this post as an e.g, if one of the instructions raises an error with a severity level of 15 or below, then the stored procedure will not stop from executing, it will continue to execute until the last statement, but it will not commit anything to the database because one of the instructions have failed with that severity level.


2) If one of the instructions raises an error of severity level of 16 or bigger, then the stored procedure will not go to the other instructions. It will stop executing.

is this correct?

tks,

Pedro
Sergiy
Sergiy
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6370 Visits: 11529
If one of the statements fails your procedure leave the transaction open.
Completing of procedure code does not complete transaction.

it should be like this:

if @ierror=0
begin
commit transaction
end
ELSE
begin
rollback transaction
end


pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 982
It's done, tks.

What about the:

"Set nocount on" does this instructuction affects the error count (@@error) inside the stored procedure while it is executing?
Or the Set nocount on, only affects the lines returned by the stored procedure?

tks
WayneS
WayneS
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6899 Visits: 10430
pedro.ribeiro (4/23/2009)
It's What about the:

"Set nocount on" does this instructuction affects the error count (@@error) inside the stored procedure while it is executing?
Or the Set nocount on, only affects the lines returned by the stored procedure?

tks


see http://msdn.microsoft.com/en-us/library/ms189837.aspx

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 982
I have seen your link. thank you
I have seen taht the variable @@error is not affected by the "Set nocount on".

But, what about the:

@declare erro int
set @erro =@@erro

The variable that i declared (@erro) is not affected by the "Set nocount on"?



Other question that i have is this:

if @erro=0
begin
update bulk_contribuinteinst set valido_bulk=1,motivo_irregular_bulk =null
where nif_antigo in
(select rgc1 from CTBS_BULK_VALIDOS_REPETIDOS
where motivo_irregular_bulk1 ='NIF REPETIDO' and rgc1 not in
(select a.rgc1 as rgc from CTBS_BULK_VALIDOS_REPETIDOS as a inner join allctb as b
on a.rgc1 <> isnull(b.rgc,-111) and a.nifctb1 =b.nifctb))
set @erro=@@error
end

the update is the first line of the instruction , then i have a select.
I am putting the "Set @erro=@@error", on the end of the statement, to verify it it went ok (if succeded). Is this correct?, or should the "set @erro=@@error"
be afetr the update and befour the select?
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