开发者

Will SQL Server roll back single statement after it was terminated?

I have a statement with a single UPDATE command. If I manually termina开发者_开发技巧te it will all the results be rolled back?


If you kill the connection on which the update was issued, or otherwise manage to cancel the query, the update will be rolled back. Every DML statement in SQL runs within the context of a transaction - SQL Server will automatically create one if one doesn't exist, and commit it after the statement completes.


If no errors occur then this statement cannot be rolled back

Update table
Set MyCol = 'foo'
Where MyOtherCol = 'bar'

However, what you can do in SQL is run the following statements together:

    begin transaction

    Update table
    Set MyCol = 'foo'
    Where MyOtherCol = 'bar'

Then perform any checks that you may need to do. If everything is ok then you can run the following.

commit transaction

if you need to cancel the update you can run this

rollback transaction

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜