2012-02-20 11 views
5

Si Exec este lote:Cuando un error detiene la ejecución en SQL Server?

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

que la salida es la siguiente:

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'. 

estoy forzando dos errores: - el primero: IMPRESIÓN 1/0 (que genera este error :

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

) Y continuar con la ejecución del lote

- la segunda:

drop table dbo.tblPrueba 
select * from dbo.tblPrueba 

que genera este error:

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

Y detiene la ejecución del lote

¿Cuál es la diferencia entre ellos? ¿Dónde puedo aprender aquellos que detienen la ejecución y aquellos que no?

Muchas gracias!

Respuesta

9

Desde el primer error es un error de división por cero, this behavior depends on your ARITHABORT, ARITHIGNORE and ANSI_WARNINGS settings.

Desde el artículo:

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.

En lo que a la que los errores detener la ejecución y cuáles no, por favor refer to the same article.

4

La forma más fácil de asegurar que todos los errores se manejan correctamente es utilizar TRY/CATCH

Sin esto, los errores pueden ser diferentes comunicado, el alcance o abortar por lotes dependiendo de la configuración, tales como ARITHxx, ANSI_WARNINGS y XACT_ABORT. Esto queda demostrado y discutido en "Error Handling in SQL 2000"

Se puede ver la diferente (no hay opciones que se hayan cambiado) con este

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 

Si funciono esto dos veces, me sale esto porque la caída nunca se ejecuta

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

+0

@PankajGarg: Si terminas en un bloque CATCH, te revertiré.También uso SET XACT_ABORT ON, que se revertirá de todos modos – gbn

1

Where can I learn those that stop execution

puede utilizar la gestión de excepciones


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 

Puede utilizar Set XACT_ABORT ON como a continuación.

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 
Cuestiones relacionadas