How to work with transactions in SQL Server Management Studio (SSMS)?
Posted: (EET/GMT+2)
When you run T-SQL scripts in SSMS, every statement executes immediately by default. If you want to group several operations and decide later whether to keep or undo them, wrap them in a transaction.
Start with BEGIN TRAN (or BEGIN TRANSACTION):
BEGIN TRAN UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1; UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2;
At this point, the changes are pending but not permanent. You can verify the results in another query window or use SELECT @@TRANCOUNT to see if a transaction is open.
To keep the changes:
COMMIT TRAN
To undo them:
ROLLBACK TRAN
Tip: you can enable "Implicit Transactions" mode in SSMS under Query - Options - Execution - ANSI, so that each statement starts a transaction automatically. Then commit or rollback manually when you're done.
Transactions are the safest way to test data updates. You can practice on live data without leaving permanent changes behind.