我在SQL server management studio中有一个查询脚本,如下所示:
if OBJECT_ID('tempdb..#temp') IS NOT NULL
drop table #temp
select somecolumn into #temp from sometable where somecondition
if OBJECT_ID('tempdb..#temp') IS NOT NULL
drop table #temp
select somecolumn2 into #temp from sometable2 where somecondition2
我添加 drop 表行以确保从缓存中清除#temp表。但是,为了重复运行脚本,我仍然会收到错误,因为“这里已经是数据库中名为'#temp'的对象了。”在第二个选择行中。似乎丢弃表没有按照我的意愿生效。
答案 0 :(得分:2)
if OBJECT_ID('tempdb..#temp') IS NOT NULL
drop table #temp
select somecolumn into #temp from sometable where somecondition
GO --<-- Separate these two block with a batch separator
if OBJECT_ID('tempdb..#temp') IS NOT NULL
drop table #temp
select somecolumn2 into #temp from sometable2 where somecondition2