---
title: "Transaction in SQL Server"  
description: "In this blog, I’m trying to explain the transaction in sql server.  A transaction is a unit of work in which either all the statements are processed s"  
author: "Sumit Kesarwani"  
published: 2013-05-29  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/519/transaction-in-sql-server  
category: "database"  
tags: ["database"]  
reading_time: 3 minutes  

---

# Transaction in SQL Server

In this blog, I’m trying to explain the [transaction in sql](https://www.mindstick.com/forum/156833/what-is-acid-property-of-transaction-in-sql-server) server.\

A transaction is a [unit of work](https://www.mindstick.com/articles/13071/generic-repository-pattern-with-unit-of-work-uow) in which either all the statements are processed successfully or none of the statements are processed. Transaction is a group of [sql queries](https://www.mindstick.com/forum/159818/how-to-use-the-coalesce-function-to-handle-null-values-in-sql-queries) performed as a single logical unit of work. Transaction begins with a specific task and ends when all the tasks in the group successfully complete. If any of the tasks fails, the transaction fails.

[Transactions](https://www.mindstick.com/interview/864/explain-acid-rule-of-thumb-for-transactions) are made up of the following four properties commonly [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) [ACID properties](https://www.mindstick.com/articles/338513/acid-properties-in-database-transactions):

· Atomicity

· Consistency

· Isolation

· Durability

##### Atomicity

Transaction is said to be atomic, if either all the statements within a transaction are performed or none of them is performed. If anyone statement in the transaction fails then all the statement that is a part of the transaction also fails. You will never find a database in a state where only a part of the transaction is performed.

##### Consistency

Whenever a transaction is committed or rolled back everything must be left in a consistent state. This means that no statements within the transaction can violate any of the [constraints](https://www.mindstick.com/forum/34816/what-is-mapping-constraints-in-database-management-system) or the rules of the database. The changes made by the transaction are consistence from one state to another.

##### Isolation

A transaction is said to be isolated when it does not interact or conflict with any other transactions. When a transaction goes to read [data from the database](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp), the transaction will find everything in the state it was before other transactions were stated or in the state that it becomes after they are committed.

A transaction never sees an intermediate state.

##### Durability

A transaction is said to be durable if the work remains completed regardless of what happens to the database after the transaction is committed. If the power fails and the database server crashes, the result of the transaction will still be present after the computer restarts.

##### COMMIT Command

The COMMIT command is the transactional command used to save changes invoked by a transaction to the database. It saves all transactions to the database since the last COMMIT or ROLLBACK command.

##### Rollback Command

The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. It can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

##### Example-1

```
BEGIN TRANSACTIONinsert into EMP(EMPID,EMPNAME) values('5','Bill');update EMP set EMPNAME='Tom' where EMPID=4select * from EMPdelete from EMP where EMPID=6COMMIT TRANSACTION
```

![Transaction in SQL Server](https://www.mindstick.com/blogs/f67ee4b3-0222-4eff-8f0b-400365502d82/images/eab5f931-ccf8-47b7-9f3b-9a1a41e5f260.jpg)

##### Example-2

```
BEGIN TRANSACTIONdelete from EMP where EMPID=5if @@error>0begin print 'Statement being rolled back'rollback tranendelsebeginprint 'Statement being committed'endCOMMIT TRANSACTION
```

---

Original Source: https://www.mindstick.com/blog/519/transaction-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
