· Uwe Janke
Commands / Restore-sqmTableFromBackup

Restore-sqmTableFromBackup

Backup & Restore sqmSQLTool v1.9.244+ · Restore ✓ -WhatIf supported  ·  original tables are never touched
SQL Server cannot restore a single table from a backup. The usual workaround is manual: restore a copy of the whole database, copy the table over, drop the copy. Restore-sqmTableFromBackup does all of it in one call. It finds the backup chain for the requested point in time (from the msdb history, for an AlwaysOn database on all replicas, or from -BackupPath), restores it as a temporary copy <Database>_sqmTR_<timestamp> on the same instance (with STOPAT for -RestoreTime), copies each table with SELECT INTO into the target database as <table>_restore_<yyyyMMdd> and drops the copy again. Because SQL Server itself reads the backup, everything it can restore works: compressed, striped and multi-set backup files, backup encryption, TDE (the instance holds the certificate) and URL backups.
ⓘ  Partial restore. If the source database uses the FULL or BULK_LOGGED recovery model, has several data filegroups and the requested tables (including LOB data and partition schemes) live in a subset of them, only PRIMARY and those filegroups are restored (RESTORE ... WITH PARTIAL). That saves time and disk space for large databases. The filegroups are read from the live database, so a table that was dropped there always leads to a full restore. Under SIMPLE recovery SQL Server requires all read/write filegroups, so the copy is restored completely. If the partial restore fails for any reason, the function falls back to a full restore on its own.
⚠  Safety. An existing table in the target database is never overwritten, unless this function created it earlier (extended property sqmTableRestore) and -Force is given. Before the copy database is dropped, its file paths are compared with the files of every other database on the instance; if one is shared, the copy stays and a warning is written. Before the restore, the free space per drive is checked against the file sizes in the backup.
ⓘ  What is copied: all columns with data types, nullability, identity property and data; computed columns become regular columns holding their values. With -IncludeIndexes also the primary key, unique constraints and rowstore indexes (filters and INCLUDE columns kept). Not copied: foreign keys, check and default constraints, triggers, permissions, columnstore/XML/spatial indexes. The result is a data copy for comparing and repairing rows, not a drop-in replacement for the original table.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringRequired, Instance with the source database. For an AG database any replica; the work runs on the primary.
-SqlCredentialPSCredentialOptional, SQL authentication.
-DatabasestringRequired, Name of the database in the backup.
-Tablestring[]Required, schema.table or table (dbo). Brackets allowed: [Sales].[Order Lines]. Several tables share one restore of the copy.
-RestoreTimedatetimeOptionalnewest statePoint in time. With log backups exact (STOPAT); without logs the newest Full/Diff finished before it, reported in RestorePoint.
-BackupPathstring[]Optionalmsdb historyBackup files or folders instead of the history. Readable by the SQL Server service account; other databases in the folder are ignored.
-TargetDatabasestringOptional-DatabaseDatabase that receives the restored tables.
-TargetSchemastringOptionalsource schemaMust exist in the target database.
-SuffixstringOptional_restore_{yyyyMMdd}Appended to the table name; a part in braces is a date format for the current time.
-IncludeIndexesswitchSwitch$falsePrimary key, unique constraints and rowstore indexes. Constraint names get the suffix (unique per schema).
-NoPartialRestoreswitchSwitch$falseAlways restore the whole database.
-DataPath / -LogPathstringOptionalinstance defaultsFolders for the files of the temporary copy.
-SkipSpaceCheckswitchSwitch$falseSkips the free-space check.
-KeepCopyswitchSwitch$falseKeeps the copy database (name in CopyDatabase).
-ForceswitchSwitch$falseReplaces a target table that an earlier run of this function created. Never applies to other tables.
-EnableExceptionswitchSwitch$falseThrow instead of returning Status = 'Failed'.

Return Value

One PSCustomObject per table: SqlInstance, SourceDatabase, SourceTable, TargetDatabase, TargetTable, Rows, IndexesCreated, RestoreMode (Full/Partial), RestorePoint, BackupSets, CopyDatabase, CopyDropped, Duration, Status (Success/Failed/WhatIf), Message.

Execution Flow

START Find backup chain for -RestoreTime (or newest state) msdb history on all AG replicas, or -BackupPath; RESTORE HEADERONLY Tables in a subset of filegroups (FULL)? YES Partial restore PRIMARY + table filegroups NO: full Enough free space per drive? NO Failed, nothing restored -DataPath / -SkipSpaceCheck YES -WhatIf? YES: return plan (chain, mode, names) RESTORE copy <Database>_sqmTR_<timestamp> on the same instance Full [FILEGROUP ..., PARTIAL] → Diff → Logs [STOPAT] → RECOVERY partial fails: drop the half copy, restore everything For each table 1. Table in the copy? No: Failed for this table 2. Target exists? not created by us: Failed, never touched created by us: replace only with -Force 3. SELECT <columns> INTO target FROM copy (same instance) 4. Extended property sqmTableRestore (source, point in time) 5. -IncludeIndexes: PK, unique constraints, rowstore indexes FINALLY: runs after errors as well Copy file used by another database? YES Copy is kept warning, check by hand NO DROP DATABASE copy skipped with -KeepCopy DONE one result object per table

Examples

Newest state of one table, as dbo.Orders_restore_<today> in the same database
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Orders
State before an accidental DELETE, two tables from one restore, with primary key and indexes
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop `
    -Table dbo.Orders, dbo.OrderLines `
    -RestoreTime '2026-11-30 07:45' -IncludeIndexes
Get back a table that was dropped: the copy is restored fully, the table lands next to the others
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Customers `
    -RestoreTime '2026-11-30 16:10' -Suffix '_before_drop'
Backup files from another server into a scratch database, plan only
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table Orders `
    -TargetDatabase Scratch -BackupPath '\\backup01\sql\Shop' -WhatIf
Repair rows in the original table from the restored copy
$r = Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Orders `
    -RestoreTime '2026-11-30 07:45'
$r | Select-Object TargetTable, Rows, RestoreMode, RestorePoint, Status

# then, in SSMS or Invoke-DbaQuery:
# SET IDENTITY_INSERT dbo.Orders ON;
# INSERT dbo.Orders (OrderId, CustomerId, Amount)
#   SELECT r.OrderId, r.CustomerId, r.Amount FROM dbo.Orders_restore_20261201 r
#   WHERE NOT EXISTS (SELECT 1 FROM dbo.Orders o WHERE o.OrderId = r.OrderId);
# SET IDENTITY_INSERT dbo.Orders OFF;