Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stored procedure error handler Expand / Collapse
Author
Message
Posted Wednesday, April 22, 2009 4:04 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, May 31, 2009 10:19 AM
Points: 103, 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


















Post #702814
Posted Wednesday, April 22, 2009 7:32 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Sunday, April 13, 2014 9:51 PM
Points: 4,570, Visits: 8,315
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

Post #702855
Posted Thursday, April 23, 2009 10:02 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, May 31, 2009 10:19 AM
Points: 103, 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
Post #703369
Posted Thursday, April 23, 2009 10:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:20 PM
Points: 6,545, Visits: 8,763
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
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
Post #703393
Posted Friday, April 24, 2009 7:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, May 31, 2009 10:19 AM
Points: 103, 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?

Post #703935
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse