简体   繁体   中英

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM