¿Cómo revertir una transacción en TSQL cuando los datos de cadena se truncan?


Actualmente tengo un gran proceso de importación que estoy tratando de envolver dentro de una transacción, por lo que si algo se rompe, podría revertir. El problema que tengo es que cuando el TSQL dentro de la trans explota, no se revertirá cuando se produzca el siguiente error SQL

Msg 8152, Level 16, State 14, Line 249
String or binary data would be truncated.
The statement has been terminated.

Lo siguiente envuelve esta importación TSQL

DECLARE @error INT
SELECT @error = 0
BEGIN TRANSACTION

--** begin import TSQL

--** end import TSQL

SELECT @error = @@error 
IF @error != 0 GOTO handle_error

COMMIT

handle_error: 
IF @error != 0 
BEGIN 
ROLLBACK 
END
Author: gbn, 2009-03-12

3 answers

Si está en SQL 2005 puede probar:

BEGIN TRANSACTION
BEGIN TRY
    --Run your Statements
    COMMIT TRANSACTION
END TRY
BEGIN CATCH
        ROLLBACK TRANSACTION
        DECLARE @Msg NVARCHAR(MAX)  
        SELECT @Msg=ERROR_MESSAGE() 
        RAISERROR('Error Occured: %s', 20, 101,@msg) WITH LOG
END CATCH
 80
Author: JoshBerke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-12 15:53:22

¿Qué tal encender xact_abort

set xact_abort on
 20
Author: Sung Kim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-12 15:45:02

También quiero señalar que si recibe este error a menudo, debe revisar el tamaño de la columna en la que está ingresando los datos o ajustar su proceso de limpieza para preparar los datos antes de colocarlos en la tabla prod. En SSIS, También podría hacer que los datos que deosn no cumplen con el tamaño estándar vayan a una tabla de datos defectuosa y procesen el resto.

 0
Author: HLGEM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-12 19:24:37