eiu165
eiu165

Reputation: 6261

Create a temporary table in SQL on the fly

How can I create a temporary table without first creating the columns?

CREATE TABLE #Yaks (
YakID int,
YakName char(30) )

select name
from tempdb..sysobjects 
where name like '#yak%'

drop table #yaks

It is a pain to have to define the table first.

Upvotes: 9

Views: 24866

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300489

Create a (temp) table with the same columns as another (no data copied):

select * into #TempTable
from MyTable
where 1=0

Note: Does not create any Foreign keys, indexes etc...

Upvotes: 19

Related Questions