IF exists(select 1 from dbo.sysobjects where id=object_id('tempdb..##temp1') and xtype='U') 
  print '存在'
else print '不存在' 

解决方案 »

  1.   

    临时表是建立在tempdb数据库中,你可以在tempdb数据库的sysobjects表中查询name字段等于#temp1的值
      

  2.   

    Select 1 as a into ##tmp
    if exists(select * from tempdb..sysobjects where name = '##tmp')
    print 'a'
    else
    print 
    'b'drop table ##tmp
      

  3.   

    if object_id('tempdb..##temp') is not null 
      drop table #temp
      

  4.   

    if object_id('tempdb..#temptablename') is not null
    print 'exist'
      

  5.   

    Select 1 as a into ##tmp
    if exists(select * from tempdb..sysobjects where name = '##tmp')
    print 'a'
    else
    print 
    'b'drop table ##tmp
      

  6.   


    use pubs
    select * into #tempa from jobs
    goif object_id('tempdb..#tempa') is not null
    print 'exist'
      

  7.   

    if exists(select 1 from tempdb..sysobjects where name='##temp1' and xtype='U')
    print '临时表已经存在'
    else
    print '不存在'
      

  8.   

    use pubs
    select * into #tempa from jobs
    go
    select * into ##tempa from jobs
    goif object_id('tempdb..#tempa') is not null
    print 'exist'
    if object_id('tempdb..##tempa') is not null
    print 'ok'
      

  9.   

    请注意用sysobjects只可以判断全局临时表
      

  10.   

    if exists(select top 1 * from #temp)
    print '有数据'
      

  11.   

    --呵呵,综合大家的方法if object_id('##temp') is not null 
      begin
        if exists(select top 1 * from ##temp)
          print '有数据'
        else
          print '无数据'
      end
    else
      print '表不存在'