简体   繁体   中英

drop temp table not take effect for SQL Server

I had a query script in SQL server management studio which is as below:

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

I add the drop table line to ensure the #temp table is cleaned from the cache. However, for repeated running the script, I still got error as "here is already an object named '#temp' in the database." in the second select line. It seems that the drop table didn't take effect as I wish.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM