SQL Server - 事务回滚错误? [英] SQL Server - transactions roll back on error?

查看:46
本文介绍了SQL Server - 事务回滚错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有在 SQL Server 2005 上运行一些 SQL 的客户端应用程序,如下所示:

We have client app that is running some SQL on a SQL Server 2005 such as the following:

BEGIN TRAN;
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
COMMIT TRAN;

它是由一个长字符串命令发送的.

It is sent by one long string command.

如果其中一个插入失败,或者命令的任何部分失败,SQL Server 会回滚事务吗?如果它不回滚,我是否必须发送第二个命令来回滚它?

If one of the inserts fail, or any part of the command fails, does SQL Server roll back the transaction? If it does not rollback, do I have to send a second command to roll it back?

我可以提供有关我正在使用的 api 和语言的详细信息,但我认为 SQL Server 应该对任何语言做出相同的响应.

I can give specifics about the api and language I'm using, but I would think SQL Server should respond the same for any language.

推荐答案

您可以在事务之前设置 set xact_abort on 以确保 sql 在发生错误时自动回滚.

You can put set xact_abort on before your transaction to make sure sql rolls back automatically in case of error.

这篇关于SQL Server - 事务回滚错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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