How to select temporary tables in Temporary Tables

user3011414 picture user3011414 · Mar 31, 2016 · Viewed 7.9k times · Source

Currently, I have some temp tables as this format "#A0089D2C", "#A0232241"

How could I select them to see what are its data?

I tried these queries as below:

Select * from #A0089D2C
Select * from tempdb.dbo.#A0089D2C

But I got the error:

Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.
Msg 208, Level 16, State 0, Line 1
Invalid object name '#A0089D2C'.

Please advise.

Thanks.

Answer

Anuj Tripathi picture Anuj Tripathi · May 18, 2016

Technically, only that user who created the local temporary table can access it within the scope. That means, local temporary table (# tables) can be accessed within the same scope by the same user while global temporary table(## tables) can be accessed among all the users until the last user session that references the table disconnects.

you can check table schema via below trick:

  • Right click on Tempdb database
  • Task > Export Data

enter image description here

  • Keep source database as TempDb
  • Select destination (Remember: you can not transfer data)
  • On Select source table and view form you will see "Edit mapping". Click on that to see table structure

enter image description here

but if you are really enthusiastic to see data in temporary table then check out this awesome post about viewing another session's Temporary table by Paul White