How do you create a temporary table exactly like a current table in a stored procedure?
Create a Global Temporary Table in SQL Server. You can also create a global temporary table by placing double hash (##) before the temporary table name. The global temporary table will be available across different connections. 3 records will be inserted into the table.
To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as DROP TABLE , INSERT , UPDATE , or SELECT .
A valuable alternatives for the SQL temp table and table variable are SCHEMA_ONLY Memory-Optimized tables and the Memory-optimized Table Variable, where the data will be completely stored in the memory without the need to touch the TempDB database, providing the best data access performance.
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
if you want it to be empty:
SELECT * INTO #t FROM table WHERE 1 = 2
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