SQL Server: Statements vs. Batches vs. Transactions vs. Connections

Transactions and Batches are two independent concepts. Both can be used in a one to many configuration.

Transaction blocks are a single "unit of work", a concept that committed sql must either fully work or not work at all. For example, if you update two tables linked to each other; both must succeed for the data change to be committed. [https://msdn.microsoft.com/en-us/library/ms174377.aspx]

Batch is a Microsoft concept. With the tools created by Microsoft such as sqlcmd and osql, the batch simply ensures a single execution plan. For example, if you create a variable and use it outside the batch, the tool will throw an error. [https://msdn.microsoft.com/en-us/library/ms188037.aspx]

So, you can have multiple batches that update multiple tables inside one transaction block. As far as they do not violate individual batch execution plans that is.

Also, within a batch, you can have multiple transaction blocks, ensuring data integrity between database entities like tables.

Connection is simply the communication handshake that approves one to run queries on server.

Statements are individual lines forming a query. GO (T-Sql batch separator) and BEGIN TRANSACTION (ANSI SQL for starting new transaction block) are both statements.


Pretty much.

A batch is just that, a batch of commands that need to be executed. A transaction is a set of commands that are guaranteed to succeed or fail totally (i.e it won't complete half the commands and then fail on the rest, if one fails they all fail).

As far as I am aware SQL Server makes use of connection pooling so I wouldn't rely on the one connection per client idea.

Tags:

Sql

Sql Server