Controlling Transactions in Your Programs

You used BEGIN TRANSACTION, COMMIT TRANSACTION, and ROLLBACK TRANSACTION in batches throughout yesterday's lesson on locking. You should have a pretty good idea what they do at this point. This section will present the transaction control statements more formally, and then we'll look at transaction nesting and transaction modes.

Transaction Control Statements

SQL Server supports four transaction control statements. (You can use the words TRANSACTION and TRAN interchangeably.)

  • BEGIN TRANSACTION—Starts a transaction block. Locks are held from this point forward. Optionally, name the transaction, like this:

    begin transaction my_tran
  • COMMIT TRANSACTION—Ends a transaction block. Work is completed and written to ...

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.