2016-08-05 15 views
1

Ich habe diesen Code, der eine temporäre Tabelle löschen sollte, wenn es existiert, aber ich bekomme immer noch einen Fehler: Cannot drop the table '#jobsconsumed', because it does not exist or you do not have permission. Könnte mir jemand helfen? Mein I.T. Admin denkt nicht, dass es ein Berechtigungsproblem ist.#temp Tabelle nicht gelöscht

IF (SELECT object_id('TempDB.#jobsconsumed')) IS NULL 
    BEGIN 
    DROP Table #jobsconsumed 
END 
+1

Ich denke, Sie sollten zwei Punkte versuchen: 'TempDB .. # jobsconsumed' – HoneyBadger

+0

Was macht die 2 Punkte tun? – whatwhatwhat

+1

Und wahrscheinlich ist "IS NOT NULL" die Logik, die Sie wirklich wollen. Warum etwas löschen, das nicht existiert? –

Antwort

3
IF (SELECT object_id('TempDB.#jobsconsumed')) IS NULL 
    BEGIN 
    DROP Table #jobsconsumed 
END 

Der obige Code wird eingeben Klausel beginnen, nur wenn TempTable vorhanden ist ..

Um zu überprüfen, und fallen TempTable, ist richtig weit unter

IF object_id('Tempdb..#test') is Not null 
is same as 
    IF object_id('Tempdb.dbo.#test') is Not null 
Drop Table #test 

Es besteht keine Notwendigkeit der Begin And END-Klausel in diesem Fall, da IF die Sofortige-Anweisung ausführen wird, wenn "True"

s ome Tests auf Schema Aspekte der TEMP Tabellen ..

use tempdb; 

create schema hr 

create table hr.#t(c int) --this will work 
create table #t(c int) --this will fail 

create table #t1 --no schema ,so it will create a temp table in DBO Schema by default. 

--To drop the table 
drop table #t --this will drop across all schemas 
+0

Frage: Ich verwende diese Zeile, um Werte in die Tabelle #temp einzufügen. '(SELECT t.ref_num, t.ref_line_suf INTO # jobsconconsumed VON ISW_LPTrans AS t WHERE t.item = @item UND t.lot = @lot AND (t.trans_type = 'I' ODER t.trans_type = 'W')) 'Aber nach deiner Antwort sollte das scheitern, weil ich' # jobsconsumed' anstelle von 'dbo. # jobsconsumed' verwende. Warum versagt es nicht? – whatwhatwhat

+1

es wird nicht fehlschlagen, bcoz es wird standardmäßig in dbo .schema erstellen – TheGameiswar