Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin.
Search for scripts directly from SSMS, and instantly access any saved scripts in your
SSC briefcase from the favorites tab.
Download now (direct download link)
Thank this author by sharing:
By Jesse McLain,
This stored procedure script is useful for generating INSERT statements to insert data into a table that has an identity column. What I specifically had in mind when I wrote it was to backup a table for testing, run the tests, and then if I needed to revert the table back to its original state, I could truncate it and then re-insert from the backup table. When using the Management Studio's scripting tasks to create the insert statement, it wouldn't include the identity column. Also, you would have to add the "SELECT FROM" code to it. This spd basically combines all that into one call.
EXEC spd_Build_Insert_Into_Stmt_With_Identity 'Invoices_Backup', 'Invoices'
Results (as text in the results pane):
TRUNCATE TABLE Invoices
SET IDENTITY_INSERT Invoices ON
INSERT INTO Invoices (
SET IDENTITY_INSERT Invoices OFF
Inserting zero in Identity Column
Invoice = ?
Zero being inserted in identity column