Create a temporary table as the current table in SQL Server 2005/2008

How to create a temporary table in the same way as the current table in a stored procedure?

+6
sql sql-server tsql stored-procedures
source share
4 answers
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 
+15
source share

SELECT * INTO #t FROM table

if you want it to be empty:

SELECT * INTO #t FROM table WHERE 1 = 2

+7
source share

Alternatively, you can script an existing table and change the name to the temp table name and add the create script table to the beginning of the rest of the script you want to run. I usually do this if it is really important that the temporary table exactly matches the structure of the real table (for example, when I create a fake table called #incerted to use when testing code that I intend to inject into the trigger.)

Most of the time, although the choice will introduce you to what you need.

+1
source share

Shared table expression or table variables can also use the server except the Temp table

0
source share

All Articles