Is there a way to get a list of all current temporary tables in SQL Server?

后端 未结 5 1506
别那么骄傲
别那么骄傲 2020-12-08 12:58

I realize that temporary tables are session/connection bound and not visible or accessible out of the session/connection.

I have a long running stored procedure that

5条回答
  •  庸人自扰
    2020-12-08 13:43

    For SQL Server 2000, this should tell you only the #temp tables in your session. (Adapted from my example for more modern versions of SQL Server here.) This assumes you don't name your tables with three consecutive underscores, like CREATE TABLE #foo___bar:

    SELECT 
      name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
      t.id
    FROM tempdb..sysobjects AS t
    WHERE t.name LIKE '#%[_][_][_]%'
    AND t.id = 
      OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
    

提交回复
热议问题