Articles → SQL SERVER → Transactions In SQL Server

Transactions In SQL Server






What Are Transactions?







Syntax


Begin Transaction -- commit command will make the changes permanent in database
Commit Transaction -- rollback command will cancel the whole transaction
RollBack TRANSACTION



Example




Picture showing the table schema of three tables
Click to Enlarge



Begin Transaction

Insert Into T1 values('Test1')
Insert Into T2 values('Test1')

Commit Transaction

IF @@ERROR > 0

RollBack TRANSACTION






Picture showing the data inserted into table1 and table2
Click to Enlarge



Begin Transaction

Insert Into T1 values('Test2')
Insert Into T3 values('Test2')

Commit Transaction

IF @@ERROR > 0

RollBack TRANSACTION




Picture showing the error message when tries to enter string value in the date field
Click to Enlarge



Picture showing that values is not inserted in any of the table because the transaction fails
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, September 12, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250