MS SQL Server: Temporary Tables

Just a way of using temporary tables in Microsoft SQL Server, if you want to duplicate one or more records quickly, you can use commands like this:


SELECT *
INTO #temporary
FROM table1
WHERE idfield=1; --create similar table by selecting a record
GO

UPDATE #temporary
SET idfield=2; -- change primary key value
-- otherwise, we cannot insert into source table1
GO

INSERT INTO table1
SELECT * FROM #temporary;
GO

This is much shorter if you have 50 fields in your source table. Otherwise, you have to specify each field and their data types;

Leave a Reply