How to retrieve a specific table or rows from database backups or transaction log backups in SQL Server
This article describes how to retrieve a specific table or rows from the database and transaction log backups if you do not want to restore your original database. This can be helpful if someone accidentally deletes a table or rows, or to retrieve specific data that existed at an earlier date.
You must have a sequence of database and transaction log backups for the procedures in this article to work:
- If you do not have transaction log backups but only full database backups, you can only retrieve data that existed at the time that you took the last full database backup.
- If you have never performed a full database backup, you cannot retrieve earlier data.
Back up the current transaction logBack up your current transaction log with the NO_TRUNCATE option. For more information, see the "BACKUP" topic in SQL Server Books Online.
Restore a partial or full database backup
- SQL Server 2000 or SQL Server 2005 If your backup strategy includes filegroup backups, you can perform a partial database restore to a different location and restore only the part of the database that contains the table that you want to retrieve. For more information about this method, see the "Partial Database Restore Operations" topic in SQL Server Books Online, and then see the "Restore Transaction Log Backups" section in this article.
Note If you are using SQL Server 2005, for more information about the partial database restore operations, visit the following Microsoft Developer Network (MSDN) Web site:Note If your strategy does not include filegroup backups, or if you are on SQL Server 7.0, go to step 2.
- If you do not have filegroup backups or are on SQL Server 7.0, restore the database from the full database backup that you want to a different database name and do not recover the database. This creates a copy of the database that contains data from the time when you took the backup and leaves the database in a state ready for transaction log backup restores. You can restore the copy to the same server or to a different server.
- See the following references for more information about how to restore a database to a different name and location:SQL Server Books OnlineFor more information, click the following article number to view the article in the Microsoft Knowledge Base:
"How to restore files to a new location (Transact-SQL)"221465 INF: Using the WITH MOVE option with the RESTORE statement
Restore transaction log backups by using the STOPAT optionRestore the transaction log backups to the partial or full database restore and stop before the point in time when the table or rows were deleted. Use the STOPAT option of RESTORE LOG command to stop the transaction log restore and to recover the database before the time when the table was deleted.
Note You do not have the option to selectively skip the time when the data was deleted. You can only recover up to the time before the data was deleted and no later.
See the following reference for more information about how to restore to a point in time:
SQL Server Books Online
"How to restore to a point in time"
"How to restore to a point in time"
Retrieve dataAfter you restore the database, you can copy the table or rows back to your original database by using either INSERT, Bcp (Bulk Copy Utility) or SELECT INTO. (This article discusses INSERT, bcp and SELECT INTO. You can also use DTS, but this article does not recommend the use of DTS.
INSERTUse INSERT to retrieve a subset of rows or only a small table. INSERT requires the most transaction log space when you insert the rows back in to the original database. If you have many indexes on the original table this increases the logging for the INSERT statement. You may want to drop the indexes before you perform the insert, and then re-create the indexes.
BCPConsider using the Bcp utility when you must retrieve a whole table and if the table size is large.
- If the table does not already exist, generate a script of the table if you do not already have one, and then re-create the table.
- Use the Bcp utility to copy the table data (rows) from the restored database to a text file, and then from the text file to the original database. For more information about how to use the Bcp utility, see the "bcp Utility" topic in SQL Server Books Online.
Note If you perform a "minimally logged" or "nonlogged" bulk copy, perform a full database backup of your original database after you complete the bcp. For more information, see the "Logged and Minimally Logged Bulk Copy Operations" topic in SQL Server 2000 Books Online, and see the "Logged and Nonlogged Bulk Copy Operations" topic in SQL Server 7.0 Books Online.
SELECT INTOIf the table was deleted, you can select the table from the restored database to your original database by using SELECT INTO. You do not have to create the table first with this method. SELECT INTO creates the table and copies the table data.
- SQL Server 7.0 You must have the select into/bulkcopy option turned on in your original database to select into a permanent table. Also, you must perform a full database backup after you complete the SELECT INTO. For more information about SELECT INTO, see the "SELECT (Transact-SQL)" topic in SQL Server Books Online topic, and then see the "INTO Clause" subtopic.
- SQL Server 2000 or SQL Server 2005 With SQL Server 2000 or SQL Server 2005, you do not use the select into/bulkcopy option if you do not need it to select into a permanent table. The amount of logging and whether you must take a full database backup of your original database after the select into depends on the recovery model of your original database. See the "SELECT Clause" topic in SQL Server Books Online and the subtopics "INTO Clause" and "Using Recovery Models".
Re-create indexes, triggers, and constraintsYou must re-create any indexes, full-text indexes, triggers, and constraints if your original table was lost.
Important Because you have retrieved data that is from a different point in time than the current database, you may have broken the referential integrity of the database. Take the appropriate steps to resolve any referential integrity issues.
DBCC CHECKTABLERun the DBCC CHECKTABLE Transact-SQL reference on the new table to verify the integrity of the data. For more information, see the "DBCC CHECKTABLE" topic in SQL Server Books Online.
SQL Server Books Online:
- "Designing a Backup and Restore Strategy"
- "Backing Up and Restoring Databases"
- "Backup/Restore Architecture"
307775 INF: Disaster recovery articles for Microsoft SQL Server
281122 INF: Restore file and filegroup backups in SQL Server
Article ID: 321836 - Last Review: 06/22/2014 11:24:00 - Revision: 5.0
Microsoft SQL Server 2000 Standard Edition, Microsoft SQL Server 2000 64-bit Edition, Microsoft SQL Server 7.0 Standard Edition, Microsoft SQL Server 2005 Standard Edition, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2005 Developer Edition, Microsoft SQL Server 2005 Enterprise Edition, Microsoft SQL Server 2005 Workgroup Edition
- kbsqlsetup kbhowtomaster KB321836