2009-02-03 17 views

risposta

192

La cosa easisest fare è di avvolgere il tuo codice in una transazione, quindi esegui ogni batch di codice T-SQL riga per riga.

Per esempio,

Begin Transaction 

     -Do some T-SQL queries here. 

Rollback transaction -- OR commit transaction 

Se si desidera incorporare la gestione è possibile farlo utilizzando una TRY ... CATCH BLOCK errore. Se si verifica un errore, è possibile eseguire il rollback della transizione all'interno del blocco catch.

Ad esempio:

USE AdventureWorks; 
GO 
BEGIN TRANSACTION; 

BEGIN TRY 
    -- Generate a constraint violation error. 
    DELETE FROM Production.Product 
    WHERE ProductID = 980; 
END TRY 
BEGIN CATCH 
    SELECT 
     ERROR_NUMBER() AS ErrorNumber 
     ,ERROR_SEVERITY() AS ErrorSeverity 
     ,ERROR_STATE() AS ErrorState 
     ,ERROR_PROCEDURE() AS ErrorProcedure 
     ,ERROR_LINE() AS ErrorLine 
     ,ERROR_MESSAGE() AS ErrorMessage; 

    IF @@TRANCOUNT > 0 
     ROLLBACK TRANSACTION; 
END CATCH; 

IF @@TRANCOUNT > 0 
    COMMIT TRANSACTION; 
GO 

vedere il seguente link per maggiori informazioni.

http://msdn.microsoft.com/en-us/library/ms175976.aspx

Spero che questo aiuti, ma per favore fatemi sapere se avete bisogno di ulteriori dettagli.

+1

Ciao, grazie. Ho visto per la prima volta circa @@ TRANCOUNT qui e puoi dirmi cosa succede a "IF @@ TRANCOUNT> 0 COMMIT TRANSACTION" dopo l'elaborazione di ROLLBACK? e quale valore ha @ TRANCOUNT? Grazie ancora. – QMaster

+1

Dopo aver eseguito la transazione ROLLBACK TRANSACTION @@ TRANCOUNT viene impostato su 0. In questo modo COMMIT TRANSACTION non verrà eseguito. Vedere https://msdn.microsoft.com/de-de/library/ms187967.aspx –

2

Voglio aggiungere un punto che è possibile anche (e dovrebbe se ciò che si sta scrivendo è complesso) aggiungere una variabile di prova al rollback se si è in modalità test. Quindi puoi eseguire l'intera operazione in una sola volta. Spesso aggiungo anche il codice per vedere i risultati precedenti e successivi di varie operazioni, specialmente se si tratta di uno script complesso.

Esempio di seguito:

USE AdventureWorks; 
GO 
DECLARE @TEST INT = 1--1 is test mode, use zero when you are ready to execute 
BEGIN TRANSACTION; 

BEGIN TRY 
    IF @TEST= 1 
     BEGIN 
      SELECT *FROM Production.Product 
       WHERE ProductID = 980; 
     END  
    -- Generate a constraint violation error. 
    DELETE FROM Production.Product 
    WHERE ProductID = 980; 

    IF @TEST= 1 
     BEGIN 
      SELECT *FROM Production.Product 
       WHERE ProductID = 980; 
      IF @@TRANCOUNT > 0 
       ROLLBACK TRANSACTION; 
     END  
END TRY 

BEGIN CATCH 
    SELECT 
     ERROR_NUMBER() AS ErrorNumber 
     ,ERROR_SEVERITY() AS ErrorSeverity 
     ,ERROR_STATE() AS ErrorState 
     ,ERROR_PROCEDURE() AS ErrorProcedure 
     ,ERROR_LINE() AS ErrorLine 
     ,ERROR_MESSAGE() AS ErrorMessage; 

    IF @@TRANCOUNT > 0 
     ROLLBACK TRANSACTION; 
END CATCH; 

IF @@TRANCOUNT > 0 AND @TEST = 0 
    COMMIT TRANSACTION; 
GO 
Problemi correlati