2012-02-17 47 views
-3

我創建了一個臨時表。 然後我運行一個With語句。我把結果放在一個臨時表中,然後在我使用這些數據後,我不能刪除它,因爲這個錯誤顯示出來:「數據庫中已經有一個名爲'#ArmadoPlantilla'的對象。」使用Statement後無法刪除sql server上的臨時表

我在做什麼錯?

+2

顯示您的代碼。 – 2012-02-17 17:36:01

+2

你真的需要告訴我們你正在使用的代碼 – Lamak 2012-02-17 17:44:40

回答

1

確保在完成臨時表後刪除臨時表。聽起來像你創建臨時表,使用它,然後離開存儲的過程(或其他),而不會丟棄它。我通常使用這種方法(雖然@ Giscard的可能工作):

CREATE TABLE #ArmandoPlantilla(
whatever int, 
whateverAgain char(30)) 

--sql inserting records and doign stuff with the temp table 

drop table #ArmandoPlantilla -- **HERE** Are you missing this? 

另外...張貼您freaking代碼。你是否想要讓我們難過?

0

請發佈您的代碼。您提到的錯誤消息表示您正嘗試多次創建表。在創建表之後,您是否正在使用select ... into #ArmandoPlantilla...插入臨時表中?

下面是如何確保在腳本開始時刪除和重新創建臨時表的示例。如果臨時表尚不存在,您應該可以修改爲僅創建臨時表:

if object_id('tempdb..#ArmandoPlantilla') is not null begin drop table #ArmandoPlantilla end 
go 
create table #ArmandoPlantilla (id int not null) 
go 
with f as (
    select 1 as [id] 
    union select 2 
) 
insert #ArmandoPlantilla 
    select * from f 
go 
select * from #ArmandoPlantilla 
+0

是的,我嘗試在過程結束時刪除臨時表,但它返回該錯誤。你能更具體地瞭解'temp ..#armadoplantilla'嗎?我在哪裏可以找到臨時表使其工作? – Max 2012-02-17 17:59:44

+2

@Max - 你爲什麼不顯示你的代碼?,你會在幾分鐘後得到你的問題的答案 – Lamak 2012-02-17 18:04:17

+0

我以爲你會知道#armadoplantilla臨時表來自哪裏,因爲你發佈了它在你的問題。如果你需要更多的幫助,你將不得不張貼代碼。 – 2012-02-17 18:11:13