2012-02-20 5 views
5

Se exec questo lotto:Quando un errore interrompe l'esecuzione in SQL Server?

begin transaction 
    PRINT 'start' 
    PRINT 1/0 
    PRINT 'continue' 
    drop table dbo.tblPrueba 
    select * from dbo.tblPrueba 
    PRINT 'finish' 
rollback transaction 

L'output è questo:

start 
Msg 8134, Level 16, State 1, Line 3 
Divide by zero error encountered. 
continue 
Msg 208, Level 16, State 1, Line 6 
Invalid object name 'dbo.tblPrueba'. 

sto forzando due errori: - il primo: STAMPA 1/0 (che genera questo errore :

Msg 8134, Level 16, State 1, Line 3 
Divide by zero error encountered. 

) E continuare l'esecuzione del lotto

- il secondo:

drop table dbo.tblPrueba 
select * from dbo.tblPrueba 

che genera questo errore:

Msg 208, Level 16, State 1, Line 6 
Invalid object name 'dbo.tblPrueba'. 

E interrompe l'esecuzione del batch

Qual è la differenza tra loro? Dove posso imparare quelli che fermano l'esecuzione e quelli che non lo fanno?

Grazie mille !!

risposta

9

Poiché il primo errore è una divisione per zero, this behavior depends on your ARITHABORT, ARITHIGNORE and ANSI_WARNINGS settings.

Dall'articolo:

These three SET commands give you very fine-grained control for a very small set of errors. When a division by zero or an overflow occurs, there are no less four choices.

  • No action at all, result is NULL – when ARITHIGNORE is ON.
  • Warning message, result is NULL – when all are OFF.
  • Statement-termination – when ANSI_WARNINGS is ON.
  • Batch-abortion – when ARITHABORT is ON and ANSI_WARNINGS is OFF.

quanto riguarda quale errori interrompere l'esecuzione e quali non lo fanno, per favore refer to the same article.

4

Il modo più semplice per garantire che tutti gli errori vengono gestiti correttamente è quello di utilizzare try/catch

Senza questo, diversi errori possono essere comunicato, la portata o l'interruzione dei lotti a seconda delle impostazioni, come ARITHxx, ANSI_WARNINGS e XACT_ABORT. Questo è dimostrato e discusso "Error Handling in SQL 2000"

È possibile vedere i diversi (nessuna opzione SET modificate) con questo

CREATE TABLE dbo.tblPrueba (gbn int); 
GO 

BEGIN TRY 

    begin transaction 
     PRINT 'start' 
     PRINT 1/0 
     PRINT 'continue' 
     drop table dbo.tblPrueba 
     select * from dbo.tblPrueba 
     PRINT 'finish' 
    rollback transaction 

END TRY 
BEGIN CATCH 
    SELECT ERROR_MESSAGE(); 
    IF XACT_STATE() <> 0 rollback transaction 
END CATCH 

Se corro questo due volte, ottengo questo perché il calo è mai eseguito

Msg 2714, Level 16, State 6, Line 1
There is already an object named 'tblPrueba' in the database.

+0

@PankajGarg: Se si finisce in un blocco CATCH, vorrei Rollback.Utilizzo anche SET XACT_ABORT ON che eseguirà il rollback in ogni caso – gbn

1

Where can I learn those that stop execution

È possibile utilizzare la gestione delle eccezioni


Begin try 
    begin transaction 
    PRINT 'start' 
    PRINT 1/0 
    PRINT 'continue' 
    create table #t 
    ( 
     id int 
    ) 
    drop table #t 
    select * from #t 
    PRINT 'finish' 
    rollback transaction 
End Try 

Begin Catch 
    if(XACT_STATE() == 1) 
    Rollback Tran 
End Catch 

È possibile utilizzare Set XACT_ABORT ON come qui di seguito.

Set XACT_ABORT ON 
begin transaction 
    PRINT 'start' 
    PRINT 1/0 
    PRINT 'continue' 
    create table #t 
    ( 
     id int 
    ) 
    drop table #t 
    select * from #t 
    PRINT 'finish' 
rollback transaction 
Problemi correlati