SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


stored proc


stored proc

Author
Message
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
HI - Masters,

Suppose i have this stored procedure below.
If i execute the stored proc like:

exec validation

When the stored procedure is executing, if one of 3 transactions that is inside the proc fails (raises error) will the sotred procedure be suceded? or will the stored proc raise an error?

Suppose that the transaction 2 (inside the stored proc) fails. Will the other 2 transaction be commited to the database if they are suceded? or because of the fact that the transaction are inside of a stored proc, if one fails, they all will not be commited to the db?

tks,

Pedro



create proc validation
as


update bulk_contribuintesest set valido_bulk = 0 where rgc is not null and rgc in
(select a.rgc from bulk_contribuintesest as a inner join todosctbs2 as b on
a.rgc is not null and a.rgc=b.rgc and dbo.rounddates(isnull(a.dta_alteracao,'1753-01-01 00:00:00'))
<=dbo.rounddates (isnull(b.dta_alteracao1,'1753-01-01 00:00:00')))

update bulk_contribuintee set valido_bulk = 0 where nif_antigo is not null and nif_antigo in
(select a.nif_antigo from bulk_contribuintee as a inner join todosctbs5 as b on
a.nif_antigo is not null and a.nif_antigo=b.rgc and dbo.rounddates(isnull(a.dta_alteracao,'1753-01-01 00:00:00'))
<=dbo.rounddates (isnull(b.dta_alteracao1,'1753-01-01 00:00:00')))

update bulk_contribuinteinst set valido_bulk = 0 where nif_antigo in
(select a.nif_antigo from bulk_contribuinteinst as a inner join todosctbs7 as b on
a.nif_antigo=b.rgc and dbo.rounddates(isnull(a.dta_alteracao,'1753-01-01 00:00:00'))
<=dbo.rounddates (isnull(b.dta_alteracao1,'1753-01-01 00:00:00')))
Adi Cohn
Adi Cohn
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7827 Visits: 6594
Your procedure has no transaction at all, so there could be cases where only part of the procedure will be committed.

If the procedure will encounter a run time error it will raise the error. Depending on the error’s severity it might continue to the next statement or stop running the procedure but continue work with its session or kill the session. In very rare and extreme cases with very high severity it can stop the server (but I only read about such cases and never encountered any). In short it all depends on the severity of the error that you’ll get.

Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
What i whan't is that this procedures only commmits the 3 updates if non of them fails.
How can i code the procedure above, so that only if the 3 suceded, the procedure commits the updates?

tks,

Pedro
Lowell
Lowell
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68612 Visits: 40900
use a transaction like his:

SET XACT_ABORT ON --any error, rollback any trnasactions
BEGIN TRAN
--do first update
--second update
....
--last update
COMMIT TRAN --if there was any error, everything rollsback, no error checking needed



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
Don't i need to put the set x_act_abort to off, on the end of the stored procedure? after the commit transaction?
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
Is the, xact_abort ON, only enabled while the stored proc is executing?
Lowell
Lowell
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68612 Visits: 40900
it depends on the scope;
outside of a stored procedure, if you issue that command, it is for the life of the session/window or until you set XACT_ABORT to OFF;

if you create a proc like below, it is only for the execution of the procedure:
CREATE PROCEDURE MYPROC
AS
BEGIN
SET XACT_ABORT ON
BEGIN TRAN
...
COMMIT TRAN
END

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
Ok. I understud very well tks.
sorry to take your time, colud you explain me how to do this but without the Xact_abort? so that i can learn the two ways?

I think that if i do like:

begin transaction

update1
update2
insert1
insert2

commmit transaction
if @@error <> 0 then
roolback transaction


It functions too. is this correct?
Or will the @@error only detect errors inn the last instructuction (insert 2) and it only roolback that last insert?

tks,

Pedro
Lowell
Lowell
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68612 Visits: 40900
to do it the hardway, you have to check @@error after each update/insert: each command changes the @@error:

ALTER procedure MyProc
As
BEGIN
begin transaction
update1
if @@error != 0 GOTO Bailout
update2
if @@error != 0 GOTO Bailout
insert1
if @@error != 0 GOTO Bailout
insert2
if @@error != 0 GOTO Bailout
commit transaction
return 0 --by returning, no lines below this point will be executed UNLESS sent there directly by the GOTO command
Bailout:
print 'Error!'
rollback transaction
return 1
END


begin transaction
update1
if @@error != 0 GOTO Bailout
update2
insert1
insert2
Bailout:
commmit transaction
pedro.ribeiro (4/1/2009)
Ok. I understud very well tks.
sorry to take your time, colud you explain me how to do this but without the Xact_abort? so that i can learn the two ways?

I think that if i do like:

begin transaction

update1
update2
insert1
insert2

commmit transaction
if @@error <> 0 then
roolback transaction


It functions too. is this correct?
Or will the @@error only detect errors inn the last instructuction (insert 2) and it only roolback that last insert?

tks,

Pedro


Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
pedro.ribeiro
pedro.ribeiro
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 982
Last question about this subject if you do not mind...
Why did you put the:

"!=" instead of "<>"

Is it the same thing for SQl Server?

tks,

Pedro
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