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.

相关文章