Undelete recently deleted rows sql server [closed] Undelete recently deleted rows sql server [closed] sql-server sql-server

Undelete recently deleted rows sql server [closed]


SQL Server keeps log for each deleted records. you can query these logs via fn_dblog sql server function.

Select [RowLog Contents 0]FROM   sys.fn_dblog(NULL, NULL)WHERE  AllocUnitName = 'dbo.TableName'       AND Context IN ( 'LCX_MARK_AS_GHOST', 'LCX_HEAP' )       AND Operation in ( 'LOP_DELETE_ROWS' )  

But this log is in Hex format. and you need to convert this Hex format to your actual data.

Given below is the article will help you to recover the deleted records in the same way defined above.

http://raresql.com/2011/10/22/how-to-recover-deleted-data-from-sql-sever/


If your database is in simple recovery mode, then you are possibly out of luck. You can restore to the most recent backup, but if it was a long time ago it may not contain copies of the rows you deleted and wish to reinsert. It is also very likely that other data has been inserted in the meantime. You could restore to a new database and then do SQL surgery to get the vanished data back in.

If your database is in full recovery mode, then:

  1. Find the last full backup, any incrementals since then, and all log backup files since the last incremental or full, and restore them up to the correct point in time. You could overwrite your database if that is acceptable, or you can restore to a new database and perform SQL surgery.

  2. The restoration process will look something like this:

    BACKUP DATABASE YourDB TO DISK = 'D:\MSSQL\Data\YourDB\YourDB Pre-Repair.bak'-- It is CRUCIAL you take a new backup before doing ANYTHING so you don't-- make another mistake you can't reverse.RESTORE DATABASE YourDB FROM DISK   = 'D:\MSSQL\Data\YourDB\YourDB 20121208 110000.bak' WITH REPLACE, NORECOVERY;-- be very careful with REPLACE as needing it proves there is un-backed-up dataRESTORE LOG YourDB FROM DISK   = 'D:\MSSQL\Data\YourDB\YourDB 20121208 111500.log' WITH NORECOVERY;RESTORE LOG YourDB FROM DISK   = 'D:\MSSQL\Data\YourDB\YourDB 20121208 113000.log' WITH NORECOVERY;-- restore more log files, all of them, in order, with none skippedRESTORE LOG YourDB FROM DISK   = 'D:\MSSQL\Data\YourDB\YourDB 20121209 020000.log'   WITH STOPAT = '20121209 01:57:00', RECOVERY;

    Note that I used WITH STOPAT here, which allows you to restore your database up to a specific point in time. In my example, log backups were taken every 15 minutes, and the fatal query was issued at 1:57:15 AM on 2012-12-09.

If you want to restore a database to a new one, you have to do something like this:

RESTORE DATABASE NewDB FROM DISK   = 'D:\MSSQL\Data\YourDB\YourDB 20121208 110000.bak'WITH   MOVE 'YourDBLogicalName' TO 'D:\MSSQL\Data\NewDB.mdf',   MOVE 'YourDBLogicalName_Log' TO 'L:\MSSQL\Logs\NewDB.ldf';

Use RESTORE FILELISTONLY to figure out what's in the backup. If there are multiple backups in the same file, there's more syntax to read out that info and then specify which one you want to work with. Use sp_helpdb 'YourDB' to find out where to put your NewDB database and Log files.

Then there's more script to rename the logical files, if you want (which I always do).

Of course, silly me, I'm just realizing now that you could use the SSMS GUI to do most of this. But if you want to start understanding all this stuff and becoming really good at it, I recommend writing the script. I am a developer, but can restore databases lickety-split without having to ask for the "official" DBA's help.