2013-10-08 13 views
5

ho un ciclo while in SQL che fare qualcosa in quantoImpossibile eseguire il backtraction. Nessuna transazione o punto di salvataggio di quel nome è stato trovato

begin tran one 

    do some inserts in others tables 

     --start loop 
    begin tran two 
     --do something 
     begin try 
--if something fail then a trigger does rollback and this return a error (and this goes to catch), then don't i need do the rollbak in catch? this could not be dissable because this is working on production 
     --something finished ok 
     commit tran two 
     end try 
     begin catch 
    rollback tran two 
     end catch 

    --finished loop 
    commit 


---------- 

ho ottenuto questo errore viene rilevato

transazione Uncommittable alla fine della partita . La transazione viene ripristinata.

begin tran one 
begin tran two 

rollback tran two 

facendo questo codice ottengo questo:

non possa rotolare indietro di due. Nessuna transazione o punto di salvataggio di quel nome è stata trovata.

Voglio solo che il subquery ripristini il secondo ciclo e continui con altri record.

+0

recensione a questa domanda: http://stackoverflow.com/questions/4614942/how-can-i-ensure-that-nested-transactions-are-committed-independent-of-each-ot Presta particolare attenzione ai link nella risposta superiore. – NotMe

+0

In sostanza, non esistono transazioni nidificate in SQL Server. – NotMe

risposta

6

rollback Operatore rollback tutte le transazioni, per rollback solo secondo ciclo si è necessario utilizzare i punti di salvataggio:

begin tran one 

-- do some inserts in others tables 

    --start loop 
    save tran two -- begin tran two 

    --do something 
    begin try 
    update product set id = 1 --if something fail then a trigger does rollback and this return a error (and this goes to catch), then don't i need do the rollbak in catch? this could not be dissable because this is working on production 

    --something finished ok 
    commit tran two 
    end try 
    begin catch 

    rollback tran two 
    end catch 

--finished loop 
commit 

innesco esempio:

create table product (id int) 
GO 
create trigger product_trigger on product for update 
as 
    set xact_abort off 

    if (select count(*) from inserted i join product p on i.id=p.id)=0 begin 
    if (@@trancount>0) begin 
     /* rollback */ 
     raiserror('product does not exist', 16, 1) 
    end 
    end 
+0

bene questo è ok se posso rinominare la prima transacion, ma come ho detto, c'è un trigger che esegue il rollback, quindi non potevo modificarlo perché questo è in produzione, e questo solo rollback, questo potrebbe funzionare se questo fa rollback, ma solo rollback, quindi questo codice non funziona. – angel

+0

Puoi dare un esempio di lavoro con trigger? – Alexandr

+0

if (selezionare il conteggio (*) dall'inserimento unire il prodotto p a i.id = p.id) = 0 iniziare se (@@ trancount> 0) iniziare rollback raiseerror ('prodotto non esiste') end end – angel

Problemi correlati