What it does
Migrates an active table month by month, resumable via an idempotent MERGE batch procedure, into a partitioned copy in a separate archive database. Supports single-column and composite keys (up to four columns), real date columns as well as numeric/string date-surrogate keys (e.g. an INT column formatted as YYYYMMDD). -PurgeSourceAfterArchive reclaims disk space in the source via DBCC SHRINKFILE after each confirmed archived month. -CutoverToArchiveView renames the source table at the end and replaces it with a view over the archive copy, existing application code keeps accessing the same table name, unchanged.
When to use it
An entire, active table needs to move permanently to another database, e.g. a separate archive instance with a different backup/storage profile, without adjusting application code. Also suited to very large tables (hundreds of GB up to TB): progress is visible via Write-Progress and a console line per month, and stopping at any point loses nothing.
Common pitfalls
- The target archive database must already exist, it isn't created automatically
- A heap, or a key with more than four columns, needs an explicit
-KeyColumn
- Without an index with the date column as its leading column, every batch potentially scans the entire table, consider a suitable index first for very large tables
- The renamed original table is never deleted automatically, deliberately, so nothing is lost, but check it manually before deleting it for good
Benefits
- Fully resumable, a stop (network, maintenance window) loses nothing; running it again picks up exactly where it last committed
- Transparent cutover via a view, no application code needs to change
- The archive copy is automatically registered for its own sliding-window maintenance
- Visible progress for migrations that can take hours to days
Invoke-sqmTableArchiveMigration -SqlInstance "SQL01" -Database "Sales" -Schema "dbo" -Table "OrderHistory" -ArchiveDatabaseName "SalesArchive" -DateColumn "OrderDate" -AllowKeyChange -PurgeSourceAfterArchive -CutoverToArchiveView