SQL Server: If I stop a single long running update before it is finished, will it roll back automatically?

StackOverflow https://stackoverflow.com/questions/19065738

Question

I happened to execute a query similar to this one:

update table1
set data=(select data from table1 where key1=val1 and key2=val2)

which was supposed to update only one row, but since I missed the second where clause, I guess it started to update every row in the database, which contains a few million rows.

The correct query would have taken about 0 seconds and would be:

update table1
set data=(select data from table1 where key1=val1 and key2=val2)
where key1=val1 and key2=val3

After a few seconds, I realized it took too long and stopped it.

The database is set to full recovery mode and running on sql server 2008 r2.

The question is, what was the effect of this query? My hope is that there would be no effect since the query was stopped before completion and SQL Server rolled back the changes automatically. Is that correct?

If not, how do I roll back the database to its state at a particular point in time (right before I did the unfortunate update)?

(I saw this question: If I stop a long running query, does it rollback? but it is different in that it performs several changes as opposed to just one.)

(And yes, I do have very recent backups, but given the size of the DB I would prefer not to have to restore from backup)

Was it helpful?

Solution

If your command to cancel came in time, it was rolled back in its entirety. DML statements are always all or nothing. You should probably check the data to make sure that your cancel did arrive in time. It might have arrived in the last millisecond or so after the transaction was already committed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top