How do you set autocommit in an SQL Server session?
Asked Answered
G

4

71

How do you set autocommit in an SQL Server session?

Grishilde answered 7/7, 2009 at 3:31 Comment(0)
L
100

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.

Lilienthal answered 7/7, 2009 at 7:43 Comment(0)
G
53

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.

Georgettageorgette answered 5/1, 2012 at 19:53 Comment(2)
I tried and I can confirm that it seems exactly the same as to write SET IMPLICIT_TRANSACTIONS ON, since it seems the more correct answer in 24 hours I will award with the bounty.Oneself
@Grishilde This should be the Accepted Answer. The point of turning off Autocommit is to avoid mistakes, which changing the settings achieves. if one needs to remember to run SET IMPLICIT_TRANSACTIONS ON before running DML, one runs the risk of forgetting to start a transaction and running a statement which updates the database immediately. So the correct answer would be to always turn on transactions as this answer states.Headway
H
11

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

Ref: Autocommit Transactions

Hopple answered 7/7, 2009 at 3:38 Comment(0)
A
2

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.

Adrell answered 26/2, 2012 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.