Sql-server – Names of temp tables

sql servertemporary-tables

I created a temp table which resides in the temp table folder. The problem is the name is unacceptably long which make it almost impossible to efficiently work on querying some field from it. Is it any way to have a shorter name like db.#SPR1 instead of the long one as below?

dbo.#SPR1_______________________________________________________________________________________________________________000000003A3E

Best Answer

This appears to be Sql Server.

When you created the temporary table, you gave it a meaningful name:

Create Table #Sales (id int, sales decimal(11,2))

Perhaps you created the temporary table using SELECT INTO #Sales FROM...

That is the name you use to reference the table (#Sales).

Due to the fact that other users in the instance can also create a local temporary table with the exact same name, Sql Server has to use an internal 'generated' name to differentiate between YOUR #Sales and another persons #Sales.

You can observe this by creating a temporary table in one session called #Sales with some columns. Under SSMS object explorer, you can see that entry under TEMPDB temporary tables. Now, go to a brand new query window and create the same table. Refresh the TEMPDB temporary tables node and you'll see the newly added temp table. But they are completely independent from each other.

Don't worry about the long weird table name under TempDB – just use the name you gave it at creation time.

And speaking of temporary tables, I'd recommend reading the information in Temporary Tables: Local vs. Global so you'll have a better understanding of how temporary tables work and how long they exist.

Additional information can be found on the Internet by searching for

Difference between local and global temporary table in sql server