stored proc

  • 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')))

  • 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/

  • 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

  • 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!

  • Don't i need to put the set x_act_abort to off, on the end of the stored procedure? after the commit transaction?

  • Is the, xact_abort ON, only enabled while the stored proc is executing?

  • 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!

  • 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

  • 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!

  • 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

  • both != and <> are "not equal" and are valid operators for SQL server...i used != because the web site sometimes interprets that as html and makes it disappear.

    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!

  • Ok. thank you very mutch for your help.

    Pedro

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply