【问题标题】:Anomalous behavior in SQL Server Temp tablesSQL Server 临时表中的异常行为
【发布时间】:2014-01-20 19:15:40
【问题描述】:

我不明白为什么下面的代码会返回错误

消息 2714,第 16 级,状态 1,第 16 行
数据库中已经有一个名为“#temptable”的对象。

如果第 1-5 行分别执行,然后是第 6-8 行,则可以正常工作。我认为我缺少有关 SQL Server 如何处理查询的基本知识。请求一位大师阐明这个问题。

select top 10 col1, col2
into #temptable
from tbl

if object_id('tempdb..#temptable') is not null
   drop table #temptable

select top 10 col1, col2
into #temptable
from tbl

【问题讨论】:

    标签: sql-server-2012 temp-tables


    【解决方案1】:

    您的代码在批处理编译时失败,而不是在执行时失败。

    该行为记录在CREATE TABLE

    如果在单个存储中创建了多个临时表 过程或批处理,它们必须具有不同的名称。

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多