93

Is it necessary to use # before creating a temporary table in SQL server?

Example:

SELECT column1, column2, someInt, someVarChar 
INTO ItemBack1 
FROM table2
WHERE table2.ID = 7

For ItemBack1 is it necessary to use the # symbol?

If not, then what is the use of # in creating temp tables?

1
  • 3
    Yes. # creates temporary table. Without it creates a table. May 25, 2013 at 11:27

2 Answers 2

154

Yes. You need to prefix the table name with "#" (hash) to create temporary tables.

If you do NOT need the table later, go ahead & create it. Temporary Tables are very much like normal tables. However, it gets created in tempdb. Also, it is only accessible via the current session i.e. For EG: if another user tries to access the temp table created by you, he'll not be able to do so.

"##" (double-hash creates "Global" temp table that can be accessed by other sessions as well.

Refer the below link for the Basics of Temporary Tables: http://www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005

If the content of your table is less than 5000 rows & does NOT contain data types such as nvarchar(MAX), varbinary(MAX), consider using Table Variables.

They are the fastest as they are just like any other variables which are stored in the RAM. They are stored in tempdb as well, not in RAM.

DECLARE @ItemBack1 TABLE
(
 column1 int,
 column2 int,
 someInt int,
 someVarChar nvarchar(50)
);

INSERT INTO @ItemBack1
SELECT column1, 
       column2, 
       someInt, 
       someVarChar 
  FROM table2
 WHERE table2.ID = 7;

More Info on Table Variables: http://odetocode.com/articles/365.aspx

6
  • 11
    In SQL Server, the SELECT syntax is INSERT INTO @ItemBack1 SELECT column1, column2, someInt, someVarChar FROM table2 WHERE table2.ID = 7
    – mhenry1384
    Oct 9, 2013 at 15:21
  • 8
    Variable tables are not stored in RAM, they are also stored in tempdb. I would recommend to not use table variables unless you understand some of their downsides, as their rowcounts are automatically set to one, and bad plans can result. Mar 20, 2014 at 22:41
  • Can you give a source for the 5000 rows, nvarchar(max), varbinary(max) restrictions? This is allowed, but I'm not sure why it is not recommended.
    – Brad
    Jun 5, 2014 at 17:07
  • 1
    I too thought table variables were stored in memory. I stand corrected: dba.stackexchange.com/a/16386/23720
    – Baodad
    Oct 2, 2015 at 16:14
  • 1
    I'm only going to comment too that it is a fact that table variables are stored in tempdb. I'm commenting so maybe MORE people read comments and get it in there heads as I am so tired of walking into a joint as a senior consultant to have to argue with a full-time employee that is ignorant and tries to say otherwise. I'm talking over dozens of companies in Fortune 500 , so glad to see people comment. SADLY, unless this OP that answers the question actually EDITS their answer , we will continue to have this ignorant persisting for another decade or so. Jun 7, 2016 at 21:27
14

The difference between this two tables ItemBack1 and #ItemBack1 is that the first on is persistent (permanent) where as the other is temporary.

Now if take a look at your question again

Is it necessary to Use # for creating temp table in sql server?

The answer is Yes, because without this preceding # the table will not be a temporary table, it will be independent of all sessions and scopes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.