Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a T-SQL transaction get rolled back if cancelled?

Tags:

sql

sql-server

In haste, I stupidly ran a statement to update a table without qualifying it with a where statement. So it started to update all records in the table.

I immediately noticed the error and hit the 'Cancel Execution" button in SQL Server Management Studio, but it took a minute to stop running.

So my question is, did it roll back the changes or were they made until it was told to stop?

I can't tell which records were updated just by looking at them. I'd have to restore the table if it did make any changes.

Thanks.

I wanted to run:

Update tableA 
set newdate = '2019-01-01' 
where account = 'abc'

but instead I ran:

Update tableA 
set newdate = '2019-01-01'

The database is a transactional type database.

like image 564
John Avatar asked Oct 27 '25 06:10

John


2 Answers

SQL Server has the default transaction behaviour by default. That means that each sentence you run in the query editor is like:

BEGIN TRANSACTION
<YOUR COMMAND>
COMMIT TRANSACTION

So, if you have cancelled before finished, the transaction should be rolled back.

like image 118
Alvaro Mendoza Avatar answered Oct 28 '25 22:10

Alvaro Mendoza


If the query hasn't finished before being cancelled then yes, it was rolled back. Either whole update was executed or nothing was changed.

like image 30
nimdil Avatar answered Oct 28 '25 22:10

nimdil