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'
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#test') and type='U')
print '#test exists'
print 'exists'
请试一下下面的语句。
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'
请试一下下面的语句。
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'
臨時的對象是系統自己存在tempdb庫中的啦.
我只是說明,要靈活機動.
如果是臨時表,只要用object_id判斷就行了.
用戶又不會創建一個別的數據庫對象名字以#打頭,
而且將其存放在tempdb中!
而其他的就要判斷其xtype了.