I need to create a temp table with same columns and type as a permanent table. What is the best way to do it? (The Permanent table has over 100 columns)
i.e.
Usually I create table like this.
DECLARE #TT TABLE( member_id INT, reason varchar(1), record_status varchar(1) , record_type varchar(1) )
But is there any way to do it without mentioning the column names and type, but mention the name of another table with the required columns?
In SQL Server you can create a temporary table based on another table by using the SELECT... INTO syntax. You can create the table with or without data. In other words, you can copy data from the original table if you wish, or you can create the table without any data.
Looking at SQL Profiler results from these queries (each were run 10 times and averages are below) we can see that the CTE just slightly outperforms both the temporary table and table variable queries when it comes to overall duration.
select top 0 * into #mytemptable from myrealtable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With