TCL OPERATIONS:


  • TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring within a database.


Command
Description
CommitUsed to save any transaction permanently.
RollbackThis command Is used to restore database to its last committed state.
Save TranThis command is used to save the transaction so that we can rollback that transaction to the point whenever necessary.
For example, we have a table named "tblStudent" with 3 records as shown below.

name

Now we'll begin our transaction and add another record and commit that transaction.
  1. Begin Tran  
  2. Insert INTO tblStudents VALUES ('Sumit')  
  3. COMMIT  
Now we have 4 Records.

id

Now, we'll add three records, one by one with save point, but we don't commit our transaction.
  1. Begin Tran  
  2. Insert INTO tblStudents VALUES ('Kajal')  
  3. SAVE Tran A;  
  4. Insert INTO tblStudents VALUES ('Rahul')  
  5. SAVE Tran B;  
  6. Insert INTO tblStudents VALUES ('Ram')  
  7. SAVE Tran C;  
  8.   
  9. SELECT * from tblStudents  
Now we have the following records in the table, from which the last three records are not yet committed.

unCommited records

Now we have 3 savepoints, in other words A, B and C. Since our transaction is not yet committed, we can roll it back to any savepoint. We'll roll it back to point B, in other words at "Rahul".
  1. ROLLBACK TRAN B  
  2. COMMIT  
Now when you fire the select query, you'll get records up to ID 6.

records upto ID 6

So this was the article based on types of commands in SQL Server with which you can play with data stored in SQL Server.

In this article, we have seen the types of commands in SQL Server and done some overview of that. We have also seen how to commit transactions and how to roll back any transaction to any saved point.

In my next article, we'll be explaining how to deal with the GRANT and REVOKE commands. Until then keep learning and keep sharing.

If there's any mistake in this article then please let me know. Please provide your valuable feedback and comments that enable me to provide a better article the next time.

Comments

Popular posts from this blog