SQL Server : Rollback without BEGIN TRANSACTION

前端 未结 3 792
迷失自我
迷失自我 2021-01-13 01:25

Is there a way we can rollback to previous state of the transaction using ROLLBACK without BEGIN TRANSACTION?

delete from table1;
ROLLBACK
         


        
3条回答
  •  鱼传尺愫
    2021-01-13 02:11

    As SQL server error tells you -- no you can't. And many people would be curious why would you want that in the first place.

    Keep in mind SQL server has an implicit transaction -- that is for DML you issue without explicit BEGIN TRAN, SQL server will start and finish a transaction for you behind the screen.

    A common usage of ROLLBACK is for error handling. If somewhere in the middle of the transaction you realize you cannot proceed further due to bad user input or other reason -- then a reasonable action is to ROLLBACK to return to the starting point

    The worst thing that can happen is leave your data state 'somewhere in the middle'.

提交回复
热议问题