Re: rollback transaction not rolling back

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 10/21/04


Date: Thu, 21 Oct 2004 21:43:15 +0200

On Thu, 21 Oct 2004 11:09:02 -0700, Mike D wrote:

>Thanks for the reply and the link. So from that article I added the Print
>line and when I get an error it doesn't print 'Error Occured' So does that
>mean I will not be able to rollback this transaction?? This doesn't seem
>right.

Hi Mike,

You should save the error code DIRECTLY adter the statement that may cause
an error. Your current logic looks like this
  WHILE ...
  BEGIN
    do something
    do something
    INSERT statement that might fail
                                -- now, @@error might be non-zero.
  END
  SET @err = @@error -- @@error already reset to 0.
  IF @err > 0
    ...

You shouyld change this to
  WHILE ..
      AND @err = 0 -- THIS LINE IS ADDED!!!!
  BEGIN
    do something
    do something
    INSERT statement that might fail
    SET @err = @@error -- MOVED THIS LINE!!
  END
  IF @err > 0
    ...

> I am trying to trap a Cannot insert duplicate key in object
>'Tab_Inst_Schedules' error. Do I query to see if it exists first??

Either that, or you change the INSERT .. VALUES to an INSERT ... SELECT
and include a WHERE NOT EXISTS clause to prevent the duplicate key error
from happening. Good code should always _prevent_ errors (and it should
still trap errors - but only to handle _unforeseen_ situations).

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: What to do in case rollback fails?
    ... If you mean you want to handle the circumstance that the rollback *itself* fails, ... SQL Server tries to give strong guarantees that that can't happen (assuming a transaction was started, of course), and if those fail there's nothing meaningful you could do from within your stored procedure. ... because error handling in stored procedures is more subtle than you might expect. ...
    (microsoft.public.sqlserver.programming)
  • Re: **HAVE NULL AND NOT DUBLICATED VALUES IN COL**
    ... trigger and it will fail because of duplicated rows and rollback happen ... >>fires and duplicated value was inserted then it will rollback to the poit ... > changes made since the BEGIN TRANSACTION. ...
    (microsoft.public.sqlserver.programming)
  • Re: hard to track update error
    ... the transaction is important because an inventory change ... I need them to either all complete or all fail. ... >> ROLLBACK ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Merge from pl/sql table
    ... I need to be able to process a complete transaction of fail ... Once complete doing a merge into the transaction tables from the pl/sql ... you need too much rollback - you shouldn't use rollback, ...
    (comp.databases.oracle.server)
  • Re: Transaction Fails
    ... actually cause my vba to FAIL. ... commit record without begin transaction. ... InvoiceSub = InvoiceSub + eachline ... Set rstInvoice = Nothing ...
    (microsoft.public.access.modulesdaovba)