SQL Server Migrator

How to Rollback a Transaction in SQL Server?

Andrew Jackson ~ Modified: December 19th, 2023 ~ SQL Transaction Log ~ 6 Minutes Reading

rollback SQL server

In SQL server each entry in a database count as a transaction. Through these transactions, SQL Server manages its database. Sometime a user performs such tasks in SQL Server that he did not want to perform. To handle such type of actions SQL Server uses some transaction control commands. Hence, rollback command in SQL server is our todays’ topic to study.

These commands are used to control the data integrity and take control on database errors. In this article we will discuss the transaction control command in depth. Let us know how to rollback SQL transactions and recover deleted records in SQL Server.

Table of Content

How to Rollback in SQL Server – In A Glance

Users just need to understand that SQL Server rollback statement syntax is quite easy to execute. Users need to enter the statement ROLLBACK TRANSACTION with the name of the transaction that is required to roll back. This way users can easily get the desired results they want.

The Syntax for ROLLBACK command in SQL is:

ROLLBACK { TRAN | TRANSACTION } 
[ transaction_name | @tran_name_variable 
| savepoint_name | @savepoint_variable ] 
[ ; ]

Step-1. Backup All the Database Files for Restore

In case, users want an easier solution, we would recommend them to restore the database version before the executed command users want to ROLLBACK. However, if users don’t have a backup, then also, they need to keep a backup to ensure a smooth manual operation. In case, anything goes wrong while executing the ROLLBACK in SQL Server, users can restore the latest version of the database.

The best-in-class SQL recovery solution can help users in getting the perfect results:

Download Now Purchase Now

Step-2. Learn How to Use Rollback Command?

The ROLLBACK command in SQL Server is generally used to undo the transaction that have not been saved to the database.

SYNTAX

ROLLBACK;

Now for the rollback SQL query, first of all, we will create a table and check it by running select statement.

Create Table

After executing the above statement, the data from student table will fetch in our database. Now, to continue rollback in SQL, I will insert a record in this table by executing insert statement:

Insert Into

The above statement will create a record in student table.

Now I am going to DELETE this record from the student table by executing below statement:

Delete Statement

The above statement will remove the record from student table.

Oops! But there is something wrong with this statement, I forgot to place BEGIN TRANSACTION before the above two statements. Therefore, users need to pay attention to this step for rollback SQL cmd.

BEGIN TRAN:

If you place the BEGIN TRANSACTION before your statement (Insert, Delete,Update), The transaction will automatically turn into the explicit transaction and it will lock the table until the transaction is committed or rolled back.

So, Now I will place the begin transaction before the Insert,Delete,Update statements.

Lets again insert a value in student table

Begin Transaction Insert

Now, executing the delete statement to further proceeding in rollback in SQL server.

Begin Transaction Delete

Evidently, the transaction is now deleted. Since the transaction is locked by BEGIN TRANSACTION, so I can rollback the above-deleted record by using the ROLLBACK command.

Rollback Transaction

After executing the ROLLBACK SQL command, and running select statement we see that we have successfully recovered our deleted record.

NOTE: The rollback command in SQL only works until the transaction is not COMMITTED.

Conclusion:

The article discusses on how an uncommitted transaction can be rolled back in such situation, when a user does not want to perform it. But you need to keep in your mind, whenever you perform such task, your transaction must be locked from BEGIN TRANSACTION and the transaction should not be committed. The above solution help users learn how to ROLLBACK UPDATE query in SQL database.

The BEGIN TRANSACTION locks your transaction until you do not rollback or commit your transaction. Also, the long running queries in transaction can result in failed rollback. To prevent dataloss failures,you can use SQL Log Reader tool as well. Evidently, this way users can easily get the best-in-class solution without facing any hassles like SQL server rollback command.

FAQs

Q-1. How do I ROLLBACK changes in SQL server?

Ans: Follow these 7 simple steps –

  1. State a table variable @TEST to start
  2. Now, insert a record here to continue
  3. BEGIN TRANSACTION for explicit transaction
  4. Now simply update the record in a table variable
  5. Update that record in the table variable @TEST
  6. Now just ROLLBACK TRANSACTION to proceed
  7. Verify the value of the record in the table variable

Q-2. What is the ROLLBACK in SQL Server do?

Ans: The rollback command is defined as a transaction command that users perform to undo their transactions if they are not saved to the database.

Q-3. Is it possible to rollback a committed transaction?

Although, I have deleted all the data in a table. But, the delete statement is inside Begin Transaction and I have committed the transaction with a commit transaction command. Therefore, is it possible to rollback and get the data back?

Ans: Well, it depends on several factors. Putting it simply, No. It is known that committed transactions make changes to the contents of database tables before the data files themselves. In case there are no triggers, stored procedures, or other preventive measures taken, users don’t have any solution. They must restore the database before the transaction ends to get the desired results. Also, it does not matter if users want to learn how to ROLLBACK DELETE query in SQL Server or how to ROLLBACK in SQL server after DELETE, they must follow the proper procedures.

Q-4. How do I ROLLBACK committed data in SQL server?

Ans: Follow the five simple steps to rollback committed data in SQL

  1. Examine schema & references of current DB
  2. Now begin a migration transaction
  3. Set & apply all the required changes
  4. Verify if the changes are reflecting
  5. Commit the transaction or revert it.

Q-5. How do I ROLLBACK delete in SQL server?

Ans: Follow these 4 simple steps to ROLLBACK DELETE:

  1. Execute BEGIN TRANSACTION
  2. Now perform a transaction & then Delete
  3. Transaction is locked due to BEGIN TRAN
  4. Now Perform ROLLBACK TRANSACTION