create table #test(name char(8))
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#test') and type='U')
print '#test exists'

解决方案 »

  1.   

    if object_id('tempdb.dbo.#test') is not null
        print 'exists'
      

  2.   

    不判断对象类型,考虑不周。
    请试一下下面的语句。
    CREATE PROCEDURE  dbo.#test  AS
     select * from dbo.sysfiles
    if object_id('tempdb.dbo.#test') is not null
    print 'exists'
    create table #test(name char(8))
    if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') and type='U')
    print '#test exists'
      

  3.   

    不判断对象类型,考虑不周。
    请试一下下面的语句。
    CREATE PROCEDURE dbo.#test AS
    select * from dbo.sysfiles
    go
    if object_id('tempdb.dbo.#test') is not null
    print 'exists'
    if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') and type='U')
    print '#test exists'
      

  4.   

    foolishchao:你會將別的對象類型存到tempdb數據庫中,並取名為#test嗎?不要死扣書本.
      

  5.   

    临时对象(如临时表、临时存储过程等)会自动存到tempdb数据库的,这样做是为了预防万一。
      

  6.   

    我當然知道
    臨時的對象是系統自己存在tempdb庫中的啦.
    我只是說明,要靈活機動.
    如果是臨時表,只要用object_id判斷就行了.
    用戶又不會創建一個別的數據庫對象名字以#打頭,
    而且將其存放在tempdb中!
    而其他的就要判斷其xtype了.