如何在Sql Server 2005中回滚插入,更新和删除查询? [英] How to Roll Back Insert,Update and Delete Query in Sql Server 2005?

查看:83
本文介绍了如何在Sql Server 2005中回滚插入,更新和删除查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Sql Server 2005中回滚插入,更新和删除查询?

解决方案

执行这些操作时,如果出现任何问题,您需要回滚查询操作,否则你将提交交易。



所以,请仔细阅读以下文章了解更多信息。

1. 保持数据库与事务的一致性 [ ^ ]。

2. 管理SQL Server存储过程中的事务 [ ^ ]。



基本上,你需要处理错误,如下所示,如下所示。

   -   如果有任何错误,则回滚事务 
IF @@ ERROR <> 0
BEGIN
- 回滚交易
ROLLBACK

- 提出错误并返回
RAISERROR ' 删除DeleteDepartment中的员工时出错。' 16 1
返回
END





谢谢...


< pre lang =sql> 开始 交易

// 执行此处的SQL代码

rollback 交易







以下文章将帮助您更好地了解你的问题。



http://www.sqlusa.com/ bestpractices2005 / rollback / [ ^ ]



http://blog.sqlauthority.com/2010/03/04/sql-server-rollback-truncate-command-in-transaction/ [ ^ ]



希望这些能帮到你..


How to Roll Back Insert,Update and Delete Query in Sql Server 2005?

解决方案

You need to rollback the query whenever there is any issue occurs while performing these operations, otherwise you will commit the transaction.

So, go through the articles below to know more.
1. Maintaining Database Consistency with Transactions[^].
2. Managing Transactions in SQL Server Stored Procedures[^].

Basically, you need to handle Errors, if any, like below, for instance.

-- Rollback the transaction if there were any errors
IF @@ERROR <> 0
 BEGIN
    -- Rollback the transaction
    ROLLBACK

    -- Raise an error and return
    RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
    RETURN
 END



Thanks...


begin transaction

// execute SQL code here

rollback transaction




Below mentioned articles will help you to get better idea on ur problem.

http://www.sqlusa.com/bestpractices2005/rollback/[^]

http://blog.sqlauthority.com/2010/03/04/sql-server-rollback-truncate-command-in-transaction/[^]

Hope these will help you..


这篇关于如何在Sql Server 2005中回滚插入,更新和删除查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆