Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback without transaction

I made a huge mistake, I executed this query:

update Contact set ContaPassword = '7FD736A3070CB9766'

I forgot the WHERE clause, so this way it updated the password for all users. :(

Is there a way I can recover the data before this query?

like image 950
kbaccouche Avatar asked Sep 20 '12 15:09

kbaccouche


People also ask

Can we use rollback without begin transaction?

Once SQL Server commits a transaction, you cannot run the ROLLBACK statement. Each rollback statement should have an association with the BEGIN Transaction statement.

Can we rollback without COMMIT?

A rollback command can only be executed if the user has not performed the COMMIT command on the current transaction or statement.

How do you rollback in SQL Server after delete without using transaction?

You cannot ROLLBACK an operation without a transaction. You could probably use implicit transactions, but you still need to call COMMIT or ROLLBACK explicitly. However, for better control, it's better to wrap the statement(s) in a BEGIN TRANSACTION... COMMIT / ROLLBACK block anyway.

What happens if you don't rollback a transaction?

If you neither commit nor rollback the transaction, the transaction will continue to exist indefinitely.


1 Answers

You cannot undo the change if you ran it outside of a BEGIN TRANSACTION / ROLLBACK. This is why I begin any sort of production data update with:

BEGIN TRANSACTION

-- report the bad or undesired data condition before-hand
SELECT ...

-- change the data
INSERT/UPDATE/DELETE ...

-- ensure we changed a reasonable number of records; may not be accurate if table has triggers
SELECT @@ROWCOUNT

 -- get the data condition afterwards and be sure it looks good.
SELECT ...

-- always start with this enabled first
ROLLBACK

-- don't do this until you are very sure the change looks good
-- COMMIT

Martin Smith pointed out this excellent post by Brent Ozar on dba.stackexchange.com on this topic. In full recovery mode, it is possible to examine the log files to see what changed.

Also, as Oded pointed out, if you have backups, it is not hard to get back to the original data. You can restore the backup somewhere and copy the original data.

like image 149
Paul Williams Avatar answered Sep 23 '22 03:09

Paul Williams