我不明白为什么下面的代码返回错误
Msg 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
发布于 2014-01-03 07:05:07
在编译批处理时,而不是在执行批处理时,代码会失败。
这种行为记录在创建表中。
如果在单个存储过程或批处理中创建了多个临时表,则它们必须具有不同的名称。
https://stackoverflow.com/questions/20898222
复制相似问题