Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
Stored procedure error handler
Stored procedure error handler
Rate Topic
Display Mode
Topic Options
Author
Message
pedro.ribeiro
pedro.ribeiro
Posted Wednesday, April 22, 2009 4:04 PM
SSC-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
Sergiy
Sergiy
Posted Wednesday, April 22, 2009 7:32 PM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:01 PM
Points: 4,540,
Visits: 8,184
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
pedro.ribeiro
pedro.ribeiro
Posted Thursday, April 23, 2009 10:02 AM
SSC-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
WayneS
WayneS
Posted Thursday, April 23, 2009 10:23 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 6,368,
Visits: 8,230
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
pedro.ribeiro
pedro.ribeiro
Posted Friday, April 24, 2009 7:23 AM
SSC-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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.