create proc temp(@tablename varchar(200))
as 
declare @exec varchar(8000)
set @exec='use tempdb
           if exists(select * from sysobjects 
                                 where id=object_id(''tempdb..'+@tablename+'''))
    print '''+@tablename+'表存在''
      else 
                      print '''+@tablename+'表不存在'''
exec (@exec)--测试
create table #test(id int,name varchar(20))temp '#test'
go
temp '#test1'
-----------
#test表存在 
#test1表不存在

解决方案 »

  1.   

    create proc temp(@tablename varchar(200))
    as 
    declare @exec varchar(8000)
    set @exec='use tempdb
               if exists(select * from sysobjects 
                                     where id=object_id(''tempdb..'+@tablename+'''))
        print '''+@tablename+'表存在''
          else 
                          print '''+@tablename+'表不存在'''
    exec (@exec)go--测试
    create table #test(id int,name varchar(20))
    go
    temp '#test'
    go
    temp '#test1'
    -----------
    #test表存在 
    #test1表不存在
      

  2.   

    IF OBJECT_ID(N'tempdb..#TableName') IS NOT NULL DROP TABLE #TableName
    -------------------------------------
    ------------------------------------
    以上语句表示如果#TableName 临时表存在则直接做删除处理
      

  3.   

    if exists (select * from tempdb.dbo.sysobjects where id = object_id(''tempdb.dbo.#pzgrid'))
    drop table #pzgrid Create table #pzgrid