怎样判断局部临时表是否存在,如果存在就删除它?
以下这个帖子好像只能删除全局临时表啊,是不是局部临时表不能删除?是不是只有断开连接时局部临时表才能自动删除啊?
http://community.csdn.net/Expert/topic/4895/4895674.xml?temp=.9407007

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4895/4895674.xml?temp=.9407007
    --------------------------------
    是我开的帖,现在到这里来蹭点分:)
      

  2.   

    create table #t(id int)if exists(select 1 from tempdb..sysobjects where left(name,len('#t'))='#t')
    begin
    print '存在'
    drop table #t
    end
    else
    print '不存在'
    if exists(select 1 from tempdb..sysobjects where left(name,len('#t'))='#t')
    print '存在'
    else
    print '不存在'这个就是删除的局部临时表啊
      

  3.   

    只不过,这种判断可能会有问题。如:一个连接分别创建了#t局部临时表后,其它连接通过(select 1 from tempdb..sysobjects where left(name,len('#t'))='#t')也能查到记录。
    在一个代码块中,创建临时表然后再drop掉。
      

  4.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablename]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[tablename]
    GO
      

  5.   

    If Object_id('TempDB..临时表名') Is Not Null
       Drop Table 临时表名
      

  6.   

    If Object_id('TempDB..临时表名') Is Not Null这个问题在其他地方问过了,呵呵,学来的
      

  7.   

    临时表在系统TempDB中保存的表名并不是给定的表名,而是带了后缀的。