How to rollback a transaction in TSQL when string data is truncated?

前端 未结 3 1981
忘掉有多难
忘掉有多难 2020-12-28 13:30

Currently I have a large import process that I\'m trying to wrap inside a transaction so if anything breaks - i could rollback. The issue I have is that when the TSQL insid

相关标签:
3条回答
  • 2020-12-28 13:47

    How about turning on xact_abort

    set xact_abort on
    
    0 讨论(0)
  • 2020-12-28 13:52

    I would also point out that if you are receiving this error often, you need to revise the size of the column you are entering data into or adjust your cleaning process to prep the data before putting it into the prod table. In SSIS, You could also have the data that deosn't meet the standard size go to a bad data table and process the rest.

    0 讨论(0)
  • 2020-12-28 13:55

    If your on SQL 2005 you can try:

    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
    
    0 讨论(0)
提交回复
热议问题