关于此 SQL 语句的 ROLLBACK TRANSACTION 的任何想法
有人可以帮我解决这个 SQL 语句吗?我在 SQL Server 引擎上运行它。
我有以下语句,删除表中的所有条目并用新条目替换它们:
SET XACT_ABORT ON;
BEGIN TRANSACTION;
DELETE FROM [t1] WHERE [id]>10;
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'a1', 'b1' FROM [t1];
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'a2', 'b2' FROM [t1];
--and so on, I may have up to 100 of these inserts
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'aN', 'bN' FROM [t1];
COMMIT;
SET XACT_ABORT OFF;
我想知道的是,如果上述事务失败,如何使用 ROLLBACK?
附言。我基本上需要将数据库恢复到以前的状态,以防上述语句中出现任何错误。
编辑: 使用 SET XACT_ABORT ON 进行更新;下面建议的声明。这是它应该看起来的样子吗?
Can someone help me with this SQL statement. I run it on the SQL Server engine.
I have the following statement that removes all entries in the table and replaces them with new ones:
SET XACT_ABORT ON;
BEGIN TRANSACTION;
DELETE FROM [t1] WHERE [id]>10;
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'a1', 'b1' FROM [t1];
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'a2', 'b2' FROM [t1];
--and so on, I may have up to 100 of these inserts
INSERT INTO [t1] ([id], [v2], [v3]) SELECT COALESCE(MAX([id]), 0)+1, 'aN', 'bN' FROM [t1];
COMMIT;
SET XACT_ABORT OFF;
What I want to know is how do you use ROLLBACK in case the transaction above fails?
PS. I basically need to revert the database to what it used to be in case of any error in that statement above.
EDIT:
Updated with the SET XACT_ABORT ON; statement suggested below. Is it how it's supposed to look?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望事务在发生错误时完全回滚,则需要设置
SET XACT_ABORT ON
。将其设置为
ON
后,如果任何语句失败,事务将回滚。您不需要调用ROLLBACK
来实现这一点。在这种情况下,当您使用
BEGIN TRANSACTION
时,每个语句都将成为该事务的一部分,这意味着它们要么全部工作,要么全部失败。如果在COMMIT
之前出现错误,事务将回滚,并且您的数据库将处于与BEGIN TRANSACTION
之前相同的状态(假设没有其他客户端正在更改事务)同时数据库)。请参阅
XACT_ABORT
的文档。You need to set
SET XACT_ABORT ON
if you want the transaction to fully rollback on an error.With this set to
ON
, if any of the statements fails, the transaction will rollback. You do not need to callROLLBACK
for that to happen.Under this condition, the moment you use a
BEGIN TRANSACTION
, every statement will be part of that transaction, meaning they will all either work or all fail. If there is an error before theCOMMIT
, the transaction will rollback and you database will be at the same state it was as beforeBEGIN TRANSACTION
(assuming no other clients are changing the database at the same time).See the documentation for
XACT_ABORT
.@Oded 介绍了一种自动处理回滚的好方法。为了完整起见,我将为您提供另一种方法来显式处理这种情况,使用 try catch< /a>.
@Oded has covered an good approach to handle your rollback automatically. For completeness, I'll give you another method to handle that situation explicitly using try catch.
http://www.codeproject.com/KB/database/sqlservertransactions.aspx
以上或许可以给你一个想法。
http://www.codeproject.com/KB/database/sqlservertransactions.aspx
The above may give you an idea.
您没有提到您的 SQL Server 版本 - 但从 2005 版本开始,您可以使用
BEGIN TRY... END TRY BEGIN CATCH... END CATCH
样式异常处理。我通常将语句块包装在事务/try-catch 块骨架中,如下所示:
该块尝试执行我的语句 - 如果成功,则提交事务,如果发生异常,则执行跳转到 CATCH 块,打印出详细的错误信息,并回滚交易
You didn't mention your version of SQL Server - but starting with the 2005 version, you can use
BEGIN TRY... END TRY BEGIN CATCH... END CATCH
style exception handling.I typically wrap my statement blocks in a transaction/try-catch block skeleton something like this:
That block tries to execute my statements - if it succeeds, the transaction is committed, if an exception happens, the execution jumps into the
CATCH
block, prints out detailed error info, and rolls back the transaction您可以尝试以下方法:
You can try the following method: