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 12»»

stored proc Expand / Collapse
Author
Message
Posted Wednesday, April 1, 2009 4:04 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
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')))
Post #687739
Posted Wednesday, April 1, 2009 4:25 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, December 1, 2014 7:30 AM
Points: 2,128, Visits: 5,565
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/
Post #687746
Posted Wednesday, April 1, 2009 4:30 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
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
Post #687748
Posted Wednesday, April 1, 2009 5:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:23 PM
Points: 12,963, Visits: 32,511
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #687776
Posted Wednesday, April 1, 2009 6:08 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
Don't i need to put the set x_act_abort to off, on the end of the stored procedure? after the commit transaction?
Post #687808
Posted Wednesday, April 1, 2009 6:11 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
Is the, xact_abort ON, only enabled while the stored proc is executing?
Post #687810
Posted Wednesday, April 1, 2009 6:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:23 PM
Points: 12,963, Visits: 32,511
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #687818
Posted Wednesday, April 1, 2009 6:25 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
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
Post #687822
Posted Wednesday, April 1, 2009 6:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:23 PM
Points: 12,963, Visits: 32,511
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #687827
Posted Wednesday, April 1, 2009 6:43 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
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
Post #687833
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse