Junior SQL

What is a ROLLBACK statement and when do you use it?

The ROLLBACK statement is used to undo all changes made during a transaction. If an error

occurs or something goes wrong, ROLLBACK ensures that the database is restored to its

state before the transaction began.

  • When to use: If any part of the transaction fails or if you wish to cancel the changes

made during the transaction.

Example:

BEGIN TRANSACTION;

UPDATE employees SET salary = 5000 WHERE id = 1;

  • - Something goes wrong

ROLLBACK;

More from SQL Server Tutorial

All questions for this course