回滚在sql server 2008 R2中更新的所有内容

时间:2014-05-19 08:59:55

标签: sql-server-2008

如果在更新最后一条记录(即300)时发生任何错误,我有300条记录,我的下面的代码将会做什么,它将回滚所有更改?或者它只是回滚最后一条记录。 如果它要回滚最后一条记录,我的目标是回滚已更新的每一件事

BEGIN TRY
    BEGIN tran

        UPDATE users
        SET  
        password = p.password
        FROM @UpdatePassword p
        JOIN users u on p.userID = u.userID

    Commit tran

END TRY
BEGIN CATCH
    rollback tran
END CATCH

1 个答案:

答案 0 :(得分:0)

来自MSDN:

A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

在您的情况下,您明确地启动了一个事务,并根据您的条件提交或回滚修改。因此,只要在处理COMMIT语句之前发生错误,所有修改都将被撤消。

演示here