Introduction
In this article, I will describe Commit and Rollback commands in SQL Server. Rollback and Commit are transaction statements that are called Data Control Language for SQL and are used to ensure the integrity of data in databases. In my previous article, I describe Grant and Revoke DCL commands; for that visit, Grant and Revoke Command in SQL SERVER.
First of all we create a table named emp on which we enforce the Rollback and Commit commands.
Creation of table
Use the following command to the create table:
- create table emp(empid int constraint PRIMARYKEY primary key, empName varchar(15))
Use the following command for the insertion of data.
- insert into emp
- select 11,'d'union all
- select 12,'ee'union all
- select 13,'p'union all
- select 14,'a'union all
- select 15,'k'
Use the following command to see the output.
- select * from emp

Commit in SQL Server
Commit is used for permanent changes. When we use Commit in any query then the change made by that query will be permanent and visible. We can't Rollback after the Commit.
Syntax
begin tran tranName
Command for operation
commit tran tranName
Command for operation
commit tran tranName
Here tranName is the name of the transaction and the command for operation is the SQL statement that is used for the operation like making a change or inserting data etc.
Example
- begin tran d
- update emp set empName ='D' where empid=11
- commit tran d
Output

Rollback in SQL Server
Rollback is used to undo the changes made by any command but only before a commit is done. We can't Rollback data which has been committed in the database with the help of the commit keyword.
Syntax
begin tran tranName
Command for operation
Rollback tran tranName
Command for operation
Rollback tran tranName
Here tranName is the name of the transaction and the command for the operation is the SQL statement that is used for performing operations like to make any change or insert data etc.
Example
We want that, if data entered by user has an empId less than 10 then the command is rolled back and a message is shown to the user "An id less than 10 is not valid; query is rolled back".
- begin tran t
- declare @id int;
- set @id=1;
- insert into emp values(@id,'d')
- if(@id<10)
- begin
- print'An id less than 10 is not valid; query is rolled back';
- rollback tran t;
- end
- else
- begin
- print 'data is inserted'
- end
Output
When we provide empId 16 which is greater than 10 then:
- begin tran t
- declare @id int;
- set @id=16;
- insert into emp values(@id,'d')
- if(@id<10)
- begin
- print'Less than 10 id is not valid,query is rollbacked';
- rollback tran t;
- end
- else
- begin
- print 'data is inserted'
- end


Summary
In this article, I described Commit and Rollback Commands in SQL Server. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.

Bhavini KothariPosted Jun 24, 2017, 2:19 AM
Can you plz tell us how to use commit and rollback in SP of sql server.
Harish PaudelPosted Jun 22, 2017, 9:50 AM
Wooow ! nice example
tim moranPosted Nov 6, 2015, 2:34 PM
No offense guys, but I hope this was just for illustration purposes. Why would you insert data then validate it afterward??
Manoj KallaPosted Aug 5, 2014, 8:27 AM
Very Good article .. . Explained nicely . .
vasanth krishnaPosted Sep 12, 2013, 5:59 AM
super..
santhiPosted May 8, 2013, 5:04 AM
Good ....
Deepak MiddhaPosted Nov 28, 2012, 10:26 PM
Thanks sir...
Rohatash KumarPosted Nov 28, 2012, 9:42 PM
Nice article. Keep it up. Deepak