简体   繁体   中英

Create a temporary table like a current table in SQL Server 2005/2008

如何创建一个与存储过程中的当前表完全相同的临时表?

select * into #temp_table from current_table_in_stored_procedure

#temp_table - locally temp
##temp_table - globally temp

select top 0 * into #temp_table from current_table_in_stored_procedure to have empty table

SELECT * INTO #t FROM table

如果你想要它是空的:

SELECT * INTO #t FROM table WHERE 1 = 2

Alternatively you can script the existing table and change the name to the temp table name and add the create table script to the top of the rest of the script you want to run. I generally do this if it really important the temp table exactly match the structure of the real table (for instance when I am creating a fake table called #inserted to use when testing the code I intend to put into a trigger.)

Most of the time though the select into will get you what you need.

公共表表达式或表变量也可以用于除临时表之外的目的

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