【问题标题】:How to tell what temporary tables are currently in scope in SQL Server?如何判断 SQL Server 中当前有哪些临时表?
【发布时间】:2010-11-15 14:03:23
【问题描述】:

我经常收到错误:

Msg 208, Level 16, State 0, Line 1
Invalid object name '#foo'.

Msg 3701, Level 11, State 5, Line 1
Cannot drop the table '#foo', because it does not exist in the system catalog.

我如何知道范围内有哪些临时表?它们显然不像基表那样出现在 SSMS 中。

【问题讨论】:

    标签: sql-server sql-server-2005 sql-server-2000 temp-tables


    【解决方案1】:

    您可以在尝试对其执行查询之前检查该表是否存在。

    IF object_id('tempdb..#foo') IS NOT NULL 
    

    【讨论】:

      【解决方案2】:

      扩大布兰登的答案...

      在 SSMS 查询窗口 1:

      CREATE TABLE #foo (bar int)
      GO
      CREATE TABLE ##bar (foo int)
      GO
      SELECT object_id('tempdb..#foo'), object_id('tempdb..##bar')
      GO
      

      在窗口 2 中:

      SELECT object_id('tempdb..#foo'), object_id('tempdb..##bar')
      

      ##bar 在两个会话中都可见,正如预期的那样。 #foo 仅在本地会话中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 1970-01-01
        • 2011-10-27
        • 2012-08-11
        • 1970-01-01
        • 2011-03-02
        • 1970-01-01
        相关资源
        最近更新 更多