如何在 SQL Server 会话中设置自动提交?

发布于 2024-07-26 11:01:14 字数 32 浏览 6 评论 0原文

如何在 SQL Server 会话中设置自动提交?

How do you set autocommit in an SQL Server session?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-08-02 11:01:14

您可以通过将implicit_transactions设置为OFF来打开自动提交:

SET IMPLICIT_TRANSACTIONS OFF

当设置为ON时,它将返回到隐式事务模式。 在隐式事务模式下,您所做的每个更改都会启动一个必须手动提交的事务。

也许举个例子就更清楚了。 这会将更改写入数据库:

SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
COMMIT TRANSACTION

这不会将更改写入数据库:

SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION

下面的示例将更新一行,然后抱怨没有要提交的事务:

SET IMPLICIT_TRANSACTIONS OFF
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION

就像 Mitch Wheat 所说,自动提交是 Sql Server 2000 的默认设置及以上。

You can turn autocommit ON by setting implicit_transactions OFF:

SET IMPLICIT_TRANSACTIONS OFF

When the setting is ON, it returns to implicit transaction mode. In implicit transaction mode, every change you make starts a transactions which you have to commit manually.

Maybe an example is clearer. This will write a change to the database:

SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
COMMIT TRANSACTION

This will not write a change to the database:

SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION

The following example will update a row, and then complain that there's no transaction to commit:

SET IMPLICIT_TRANSACTIONS OFF
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION

Like Mitch Wheat said, autocommit is the default for Sql Server 2000 and up.

佼人 2024-08-02 11:01:14

我想要一种更持久、更快捷的方法。 因为我往往会忘记在编写实际的更新/插入查询之前添加额外的行。

我通过选中“选项”中的SET IMPLICIT_TRANSACTIONS复选框来完成此操作。
导航到“选项” 在 Microsoft SQL Server Management Studio 中选择工具>选项>查询执行>SQL Server>ANSI

只需确保在执行完查询后执行commitrollback 即可。 否则,您运行查询的表将被其他表锁定。

I wanted a more permanent and quicker way. Because I tend to forget to add extra lines before writing my actual Update/Insert queries.

I did it by checking SET IMPLICIT_TRANSACTIONS check-box from Options.
To navigate to Options Select Tools>Options>Query Execution>SQL Server>ANSI in your Microsoft SQL Server Management Studio.

Just make sure to execute commit or rollback after you are done executing your queries. Otherwise, the table you would have run the query will be locked for others.

手长情犹 2024-08-02 11:01:14

自动提交是SQL Server默认的事务管理模式。 (SQL 2000 及以上)

参考:自动提交事务

Autocommit is SQL Server's default transaction management mode. (SQL 2000 onwards)

Ref: Autocommit Transactions

心安伴我暖 2024-08-02 11:01:14

使用 SQLServer 2005 Express,我发现即使关闭了自动提交,也可以提交对数据库表的插入,而无需我从 Management Studio 会话中实际发出提交命令。 唯一的区别是,当自动提交关闭时,我可以回滚所有插入; *打开自动提交,我不能。*
实际上,我错了。关闭自动提交模式后,我只能在发出命令的 QA(查询分析器)窗口中看到更改。 如果我弹出一个新的 QA(查询分析器)窗口,我看不到第一个窗口(会话)所做的更改,即它们没有提交! 我必须发出明确的提交或回滚命令才能使更改对其他会话(QA 窗口)可见 - 我的错! 一切正常。

With SQLServer 2005 Express, what I found was that even with autocommit off, insertions into a Db table were committed without my actually issuing a commit command from the Management Studio session. The only difference was, when autocommit was off, I could roll back all the insertions; with *autocommit on, I could not.*
Actually, I was wrong. With autocommit mode off, I see the changes only in the QA (Query Analyzer) window from which the commands were issued. If I popped a new QA (Query Analyzer) window, I do not see the changes made by the first window (session), i.e. they are NOT committed! I had to issue explicit commit or rollback commands to make changes visible to other sessions(QA windows) -- my bad! Things are working correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文