TCL Command in SQL SERVER

TCL : Transaction Control Language.
These commands are used to control the transactions.

Before we go in depth into TCL commands , lets see what is TRANSACTION in SQL SERVER.

TRANSACTION:  Transaction is set of command ( or sql server operations ) grouped together. In other words transaction is a group of sequentially arranged logical units of work.

Transactions are generally group together insider BEGIN and END block.

General syntax of a Transaction is:

      BEGIN TRAN

               SQL Command1
               SQL Command2
                        -|
                        -|
                        -|
               SQL CommandN
      END TRAN

Transactions are generally controlled using the following TCL Commands: COMMIT and ROLLBACK

COMMIT: This command commits (or saves) the transaction.
Result of the database transactions are either saved in database or discarded based on the TCL commands issued at the end of the transaction command.

Unless explicitly mentioned , Transactions are by default committed. That's Transaction changes will be saved in the database unless ROLLBACK command is explicitly specifie

In simple words commit saves the transaction changes in database.

Syntax for the COMMIT command will be:

COMMIT {TRAN/TRANSACTION} < Transaction Name >

Example:

         BEGIN TRAN ABC

                   SQL Commands

         COMMIT TRAN ABC

COMMIT command makes the changes permanent in the database.

ROLLBACK:

ROLLBACK Commands uncommit the changes done to the database from last committed transaction i.e., it discards all the changes which are done as part of that transaction.

ROLLBACK will work as Ctrl+Z ( undo) . This command will undo the transactions.

Syntax for ROLLBACK Command is:

ROLLBACK {TRAN/TRANSACTION} <Transaction Name >

Example:

ROLLBACK TRAN ABC

By Default all the transactions are committed unless mentioned.

Summary:

We have learnt about TCL Commands.

Commands learnt :

COMMIT, ROLLBACK, TRAN, TRANSACTION

This entry was posted in . Bookmark the permalink.

One Response to TCL Command in SQL SERVER