Is there any way to rollback transactions in SQL Server? - sql

Yesterday I wrote some code module, that wrote wrong data in almost 400 existing records in important database on SQL Server 2008. I didn't make backup of this database (my mistake). So the question is how do I rollback these 400 transactions? Is there any way to do this? Thanks.

You can't rollback, but if the database is in full recovery model, then you can restore to another servar with stopat, and recover the deleted rows from there.

You can roll back a transaction as long as you haven't committed it.
But if you ran it yesterday, chances are very high you did commit - and then there's no way back.
So you're options are to
either restore a backup (and next time remember to take one before such an operation!)
manually "undo" those 400 transactions

Related

SQL restore data from transaction log

I have a transaction which update data today, but the code used in the transaction have bugs and some of the data updated wrongly.
May i ask if it's possible to restore data by using the transaction log stored in the database?
Thanks a lot
using a full back up and a collection of transaction logs you can restore a database to a point in time. If you are looking to not take the database offline to restore, you would need to restore the database to a new database and identify the data that you want to fix and manually "fix" it. See https://technet.microsoft.com/en-us/library/ms190982(v=sql.105).aspx
Its very complicated to restore from Tlog,unless you know what you are looking for..use some kind of tool like Apex SQL log (not a freeware) to generate statements from Tlog
You can read more details here
http://www.apexsql.com/sql_tools_log.aspx

roll back SQL query executed by mistake

I think the question says it all,
the following update query has been executed - by mistake - in SQL Server management studio
update kms_students set student_campus='4' where student_campus='KL'
The effected rows are more than 1000, and i can't identify it since that table is already have the student_campus='4' for many previous rows.
Is it possible to roll back?
I believe ApexSQL should do the trick.
ApexSQL works by analyzing the physical transaction log which basically has all the necessary info to restore specific transactions and data, but MS doesn't provide an out-of-box tool to manage it, other than restoring a backup and then manually restoring the transaction log up to a particular date using RESTORE LOGS
Backup. Most Hosting companies keep one, try calling everyone asap.
Your own backups. Even if they're old they will be helpful.
Keep lots of Backups and NEVER try out queries on production environment. NEVER.(Bet you learned that, right?)
To make it a bit easier, you can try putting the backup DB online and execute some PHP/Python/whatever so as to compare each record from the Backup and change the current database fom '4' to 'KL' where needed.
May not be perfect, but can help you avoid a few days of work.

How to recover deleted rows from SQL server table?

I accidentaly ran a DELETE command against a table with a wrong WHERE
clause.
I am using SQL Server 2005.
Is there a way that could help me recover the lost data?
It is possible using Apex Recovery Tool,i have successfully recovered my table rows which i accidentally deleted
if you download the trial version it will recover only 10th row
check here http://www.apexsql.com/sql_tools_log.aspx
You have Full data + Transaction log backups, right? You can restore to another Database from backups and then sync the deleted rows back. Lots of work though...
(Have you looked at Redgate's SQL Log Rescue? Update: it's SQL Server 2000 only)
There is Log Explorer
I think thats impossible, sorry.
Thats why whenever running a delete or update you should always use BEGIN TRANSACTION, then COMMIT if successful or ROLLBACK if not.
What is gone is gone. The only protection I know of is regular backup.

How to recover a deleted row from SQL Server 2005 table?

How to recover a deleted row from SQL Server 2005 table?
If you have database backups that have deleted data:
Restore backup in separate database and recover deleted data from there
If there are no backups but your database is in full recovery mode:
Try reading transaction log using some third party transaction log
reader or using DBCC LOG command.
You’ll need help from third party tools because transaction log is not well documented. This is because it’s purpose is not to be used for this kind of recovery. However, if you can read it there are a lot of useful details there that can be used to recover accidentally deleted data.
Rollback the transaction (if you started one).
Restore a backup (if you have one).
[edit] If you have transaction logs, you should be able to restore the backup of the database to the point roughly just before the row was deleted (assuming you know when that was).
There are two ways we can recovery specific table:
The first one: restore fullback up with no recovery after restore with no_truncate option for t-log backup
The second way: using triggers we can recovery deleted tables with audit table.
The ApexSQL Log tool can be the solution for deleted rows. In case the DELETE operation exists (the database was not using the Simple recovery model) in database transaction logs (online, backups), the tool can create an undo T-SQL script for the operation.
Disclaimer: I work as a Product Support Engineer at ApexSQL
Don't forget to set full recovery model for a database if you need the "restore to a point in time" option!

How to rollback changes in SQL Server

By mistake I have updated data on production database. Is there any way to rollback those transactions.
I have executed the update statement from management studio and the script does not have in
Begin Trans/rollback/commit.
Thanks
Here is what I would do in this case:
Restore backup in separate database and compare these databases to recover rows that exist in backup?
If your database is in full recovery mode try reading transaction log to recover the remaining rows.
In order to read transaction log you can use a third party tool such as ApexSQL Log or try to do this yourself through fn_dblog function (here is an example but it’s quite complex).
Here are other posts on this topic:
Read the log file (*.LDF) in SQL Server 2008
How can I rollback an UPDATE query in SQL server 2005?
Without transaction (or indeed even with a committed transaction), there is no easy way to revert the changes made.
Transaction are mostly useful to ensure that a series of changes to the database are performed as a single unit, i.e. so that either all of these changes get performed [in the order prescribed] or that none of them get performed at all (or more precisely that the database server rolls-back whatever changes readily done would there be a problem before all changes are completed normaly).
Depending on the recovery model associated with your database, the SQL log file may be of help in one of two ways:
If you have a backup and if the log file was started right after this backup, the logfile may help "roll forward" the database to the point that preceded the unfortunate changes mentioned in the question. (aka point-in-time restore)
If no such backup is avaiable, the log file may be suitable to reverse the unfortunate changes
Both of these approaches imply that the SQL log was indeed maintained as some of the recovery models, are such that the log file get truncated (its data lost) after each successful batch/transaction. And neither of these approaches is easy, the latter in particular probably require third party software (or a lenghty procedure) etc.
Depending on how your backups are set up, you may be able to do a point in time restore. Talk to your DBA. You may also want to take the DB offline ASAP to prevent more changes that would eventually be lost when you do the restore.