SQLTreeo Restore · How-to

Restore a single table from a SQL Server backup

Get one table out of a SQL Server backup without restoring the database: open the .bak in SQLTreeo Restore, tick the table, export or copy it. Or use T-SQL.

You can get one table out of a SQL Server backup without restoring the database. SQLTreeo Restore reads the .bak file directly: open the file, tick the table, and export it to CSV or copy it into a database on a server of your choice. There is no temporary database to make, and you need room for that table, not for the whole database.

That is the quick way back when somebody deleted the wrong rows, or when a table has to be compared with how it was last Tuesday. This article gives the steps, and the T-SQL way for when you have a server with room for the whole database.

Restore one table with SQLTreeo Restore

  1. Download SQLTreeo Restore and open the .bak file. The tables are listed with their rows and sizes; select one to see its columns and first rows and check it is the right one.
  2. Tick the tables you need.
  3. Choose Export to CSV… for files that open in Excel, or Copy to SQL Server… to copy them into a database on a server of your choice.

Opening the backup and looking at its tables and rows is free. A license key exports and copies every row. Without one, export and copy write the first 1,000 rows of each table, marked as an unlicensed preview (dbo.Orders (first 1000 rows - unlicensed).csv, or a table named dbo.Orders_preview). That is enough to check you have the right table.

When you copy, a table that does not exist in the target is created with the same columns, types and collations, its primary key, unique constraints and clustered index, and its computed and sparse columns. Other indexes, foreign keys, check constraints, defaults and triggers are not created. When the table exists already, you choose once: add the rows, replace the rows, or cancel. Each table is copied completely or not at all, so a failure leaves the table as it was.

Getting started has the steps, and How to open a .bak file without SQL Server compares all the ways to look inside a backup.

Check before you put rows back

  • Copy the table into a new database first when you want to compare before you change anything. To see what changed between the restored table and the live one, compare them with SQLTreeo Compare: its data compare shows the rows that differ, key by key.
  • A table from a backup is as it was at that moment. Tables that refer to it may have changed since; check foreign keys before you put rows back.

The T-SQL way: restore under another name, copy the table

RESTORE works on whole files, so with T-SQL the whole database comes back first and you copy the table from there.

  1. Find the logical file names of the backup:

    RESTORE FILELISTONLY FROM DISK = N'D:\Backups\Sales_FULL.bak';
  2. Restore it as a new database, with its files somewhere that has room (the Size column of the previous query is what the files need):

    RESTORE DATABASE Sales_Temp
    FROM DISK = N'D:\Backups\Sales_FULL.bak'
    WITH MOVE N'Sales' TO N'E:\Temp\Sales_Temp.mdf',
         MOVE N'Sales_log' TO N'E:\Temp\Sales_Temp_log.ldf',
         STATS = 10;
  3. Copy the table out of the temporary copy. SELECT ... INTO creates a new table in the live database:

    SELECT * INTO Sales.dbo.Orders_from_backup
    FROM Sales_Temp.dbo.Orders;

    To put rows back in the table that exists, use INSERT ... SELECT with a column list, and SET IDENTITY_INSERT ... ON when the table has an identity column.

  4. Drop the temporary copy:

    DROP DATABASE Sales_Temp;

Things to know:

  • Step 2 needs room for the whole database. With a large database, SQLTreeo Restore is the shorter way.
  • SELECT ... INTO copies the columns and the rows, not the primary key, indexes, constraints or triggers.
  • When the backup has several filegroups and the table sits in its own, a partial restore (RESTORE DATABASE ... FILEGROUP = ... WITH PARTIAL) brings back only the primary filegroup and the ones you name. It only helps when the table is in a filegroup of its own.