---
title: "How To Use Transactions SQL Server"  
description: "How To Use Transactions SQL Server"  
author: "Anonymous User"  
published: 2016-03-09  
updated: 2016-03-09  
canonical: https://www.mindstick.com/forum/34047/how-to-use-transactions-sql-server  
category: "database"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How To Use Transactions SQL Server

We want to Use [Transactions](https://www.mindstick.com/interview/864/explain-acid-rule-of-thumb-for-transactions) [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) . How to do this please help me.

## Replies

### Reply by Anonymous User

**Transaction** is a set of T-[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) statements that are executed together as a unit like as a single T-SQL statement.\
If all of these T-SQL statements executed successfully, \
then a **transaction** is committed and the changes made by T-SQL statements permanently saved to database.\
If any of these T-SQL statements within a **transaction** fail, then the complete **transaction** is cancelled/ rolled back.

We use **transaction** in that case, when we try to modify more than one tables/views that are related to one another.\
Transactions affect SQL [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) performance greatly. \
Since When a **transaction** is initiated then it locks all the tables data that are used in the **transaction**. \
Hence during **transaction** life cycle no one can modify these tables’ \
data that are used by the **transaction**.\
The reason behind the locking of the data is to maintain Data Integrity.

```
CREATE TABLE [Employee](	[EmpNo] [int] IDENTITY(1,1)PRIMARY KEY,	[EmpName] [varchar](50) NOT NULL,	[Salary] [decimal](18, 0) NOT NULL,	[DeptName] [varchar](50) NOT NULL,	[Designation] [varchar](50) NOT NULL)SELECT *  FROM  [Employee]  
```

```
CREATE TABLE [PRODUCT](	[PRODUCT_ID] [bigint] IDENTITY(1,1) PRIMARY KEY,	[PRODUCT_NAME] [varchar](200) NULL,	[PRICE] [decimal](10, 2) NULL)
SELECT *  FROM  [PRODUCT]
```

```
BEGIN TRANSACTION transBEGIN TRYINSERT INTO [dbo].[Employee]([EmpName],[Salary],[DeptName],[Designation])VALUES('Manoj kumar',20000,'IT','Programmer')INSERT INTO [PRODUCT]([PRODUCT_ID],[PRODUCT_NAME],[PRICE])VALUES(2,'Mocro Max',200) -- here we  appears error because we insert identity key value -- first statetment successfully but second statetment not successful so transaction ROLLBACK IF @@TRANCOUNT > 0 BEGIN COMMIT TRANSACTION trans ENDEND TRYBEGIN CATCH print 'Error Occured' IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION trans  ENDEND CATCH
```

```
SELECT * FROM [Employee]SELECT * FROM [PRODUCT]
```


---

Original Source: https://www.mindstick.com/forum/34047/how-to-use-transactions-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
