Nested transaction cannot be canceled

I have SQL Server 2008 and I want to do this transaction:

begin transaction oo;

......

begin try        
    save transaction xx;
    alter table ....; -- this will fail
    alter table ....;
    alter table ....;
end try
begin catch  
    rollback transaction xx; -- error here
end catch;

......

commit transaction oo;

In rollback transaction xx;, I get a message

3931 The current transaction could not be committed and could not be returned to the save point. Rollback the entire transaction.

What am I doing wrong here?

Refresh . To explain the scenario:

  • There is a big transaction "oo" that will change the structure of the database tables from product version X to product version Y.

  • In nested transactions, user tables must be checked for changes (= internal transaction).

  • If some some damaged user table is incorrect, the whole product update process should not be rolled back.

  • , , , , - ( ).

+4
1

CATCH

ROLLBACK TRANSACTION; 

, ( Q),

COMMIT TRANSACTION BEGIN TRANSACTION.

TRY

COMMIT TRANSACTION oo;

, ,

BEGIN TRANSACTION oo;

BEGIN TRY        
    SAVE TRANSACTION xx;
    CREATE TABLE test (ID INT); -- this will fail from second time
    SELECT 3;

    COMMIT TRANSACTION oo;
END TRY

BEGIN catch  

    ROLLBACK TRANSACTION; 
END CATCH;

BEGIN TRY        
    BEGIN TRANSACTION xx1;
    select 1; -- this will always success
    COMMIT TRANSACTION xx1;

    BEGIN TRANSACTION xx2;
    CREATE TABLE test (id int); -- this will fail from second time
    COMMIT TRANSACTION xx2;

    BEGIN TRANSACTION xx3;
    select 3; -- this will fail from second time
    COMMIT TRANSACTION xx3;


END TRY

BEGIN catch  

    ROLLBACK TRANSACTION 
END CATCH;
+1

Source: https://habr.com/ru/post/1627972/


All Articles