Amarnauth Persaud
Amarnauth Persaud

Reputation: 537

Exporting Result Set to Another Table?

How can you export a result set given by a query to another table using SQL Server 2005?

I'd like to accomplish this without exporting to CSV?

Upvotes: 0

Views: 105

Answers (3)

MaD
MaD

Reputation: 106

insert into table(column1, columns, etc) select columns from sourcetable 

You can omit column list in insert if columns returned by select matches table definition. Column names in select are ignored, but recommended for readability.

Select into is possible too, but it creates new table. It is sometimes useful for selecting into temporary table, but be aware of tempdb locking by select into.

Upvotes: 1

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

INSERT INTO TargetTable(Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM SourceTable

Upvotes: 2

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

SELECT col1, col2, ...
INTO dbo.newtable
FROM (SELECT ...query...) AS x;

Upvotes: 0

Related Questions