Sql 未删除临时表

Sql 未删除临时表,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,我有这样的代码,如果临时表存在,应该删除它,但我仍然得到一个错误:无法删除表“jobsconsumed”,因为它不存在或者您没有权限。有人能帮我吗?我的it管理员认为这不是权限问题 IF (SELECT object_id('TempDB.#jobsconsumed')) IS NULL BEGIN DROP Table #jobsconsumed END 上述代码仅在存在TENTABLE时才会进入Begin子句 要检查和删除诱惑,正确的方法如下 IF object_id('

我有这样的代码,如果临时表存在,应该删除它,但我仍然得到一个错误:无法删除表“jobsconsumed”,因为它不存在或者您没有权限。有人能帮我吗?我的it管理员认为这不是权限问题

IF (SELECT object_id('TempDB.#jobsconsumed')) IS NULL
    BEGIN
    DROP Table #jobsconsumed
END 
上述代码仅在存在TENTABLE时才会进入Begin子句

要检查和删除诱惑,正确的方法如下

IF object_id('Tempdb..#test') is Not null  
 is same as
  IF object_id('Tempdb.dbo.#test') is Not null  
Drop Table #test
在这种情况下不需要Begin和END子句,因为如果为true,IF将执行immediate语句

关于临时表的模式方面的一些测试

  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
上述代码仅在存在TENTABLE时才会进入Begin子句

要检查和删除诱惑,正确的方法如下

IF object_id('Tempdb..#test') is Not null  
 is same as
  IF object_id('Tempdb.dbo.#test') is Not null  
Drop Table #test
在这种情况下不需要Begin和END子句,因为如果为true,IF将执行immediate语句

关于临时表的模式方面的一些测试

  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
这对我2017年的SQL小姐很有用

这对我2017年的SQL小姐很有用


我认为你应该尝试两个点:TempDB..jobsconsumed这两个点的作用是什么?你真正想要的逻辑可能不是空的。为什么要删除不存在的东西?谢谢@GordonLinoff,就是这样。但仍然对这些点感到好奇……如果我没弄错的话,它告诉SQLServer在所有模式中查找表。TempDB.jobsconsumed将查找架构jobsconsumed。但是我必须承认我没有经常使用它,而且我一点也不确定…我认为你应该尝试两个点:TempDB..jobsconsum这两个点做什么?可能不是空的,这是你真正想要的逻辑。为什么要删除不存在的东西?谢谢@GordonLinoff,就是这样。但仍然对这些点感到好奇……如果我没弄错的话,它告诉SQLServer在所有模式中查找表。TempDB.jobsconsumed将查找架构jobsconsumed。但我必须承认我没有经常使用它,而且我一点也不确定……问题:我用这行代码将值放入临时表中。选择t.ref_num,t.ref_line_suf to jobsconsummed FROM ISW_LPTrans AS t,其中t.item=@item和t.lot=@lot和t.trans_type='I'或t.trans_type='W',但根据您的回答,这应该会失败,因为我使用的是jobsconsummed而不是dbo.jobsconsummed。为什么它没有失败?它不会失败,因为默认情况下它会在dbo中创建。schemaQuestion:我使用这一行将值放入临时表。选择t.ref_num,t.ref_line_suf to jobsconsummed FROM ISW_LPTrans AS t,其中t.item=@item和t.lot=@lot和t.trans_type='I'或t.trans_type='W',但根据您的回答,这应该会失败,因为我使用的是jobsconsummed而不是dbo.jobsconsummed。为什么它没有失败?它不会失败,默认情况下它将在dbo.schema中创建