HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Are implicit transactions per batch or per session in SQL Server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
implicitperaresqlbatchsessionservertransactions

Problem

Imagine I am running multiple batches through management studio, separated by the GO command. I'd like to know how implicit transactions will behave - is the transaction committed on a per batch basis or once for the entire execution.

Solution

What exactly do you mean by "implicit transactions"? Do you mean that you have the session property of IMPLICIT_TRANSACTIONS enabled and are using that feature? Or do you mean that you just aren't specifying BEGIN TRANSACTION? (i.e. creating an "explicit" transaction)?

If you are meaning the IMPLICIT_TRANSACTIONS feature, that simply issues the BEGIN TRAN and nothing more. So whether or not you are using IMPLICIT_TRANSACTIONS to issue the BEGIN TRAN or if you are doing that explicitly yourself, you are still responsible for issuing the COMMIT or ROLLBACK. And yes, Transactions can span batches, whether you are using GO to separate batches or executing multiple queries by hitting F5 at different times.

In either case -- IMPLICIT_TRANSACTIONS set to ON or an explicit BEGIN TRAN statement -- nothing is automatically committed. The only thing that might be automatic in these two cases is a ROLLBACK if you close the connection without issuing the COMMIT or ROLLBACK first.

Without either IMPLICIT_TRANSACTIONS set to ON or an explicit BEGIN TRAN statement, then each individual query is its own transaction, that is "auto-committed" and you need not worry about it. Meaning, 2 separate INSERT statements are two completely separate transactions.

Context

StackExchange Database Administrators Q#177803, answer score: 6

Revisions (0)

No revisions yet.