Skip to content

Instantly share code, notes, and snippets.

@daniel-liuzzi
Last active April 22, 2016 18:08
Show Gist options
  • Save daniel-liuzzi/fb0861f85c4ee8b7e87684fc66ad5bad to your computer and use it in GitHub Desktop.
Save daniel-liuzzi/fb0861f85c4ee8b7e87684fc66ad5bad to your computer and use it in GitHub Desktop.
CREATE TABLE SourceTable (Id int IDENTITY(1,1), Name nvarchar(50) NOT NULL);
INSERT INTO SourceTable VALUES ('Superman'), ('Batman'), ('Homer Simpson');
SELECT TOP 0 * INTO MyTableCopy FROM SourceTable;
-- Commenting out the following line gives "Cannot insert explicit value for identity column in table 'MyTableCopy' when IDENTITY_INSERT is set to OFF."
SET IDENTITY_INSERT MyTableCopy ON;
INSERT INTO MyTableCopy (Id, Name) SELECT Id, Name FROM SourceTable;
DROP TABLE MyTableCopy;
DROP TABLE SourceTable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment