且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-28 13:13:17

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



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

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

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



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

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

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





谢谢...


开始 交易

// 执行此处的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..