一、判断数据表是否存在:
方法1:
use yourdb;
go
if object_id(n'tablename',n'u') is not null
print '存在'
else
print '不存在'
例如:
use fireweb;
go
if object_id(n'temp_tbl',n'u') is not null
print '存在'
else
print '不存在'
方法2:
use [实例名]
go
if exists(select * from dbo.sysobjects where id=object_id(n'[表名]') and objectproperty(id, 'istable') = 1)
print '存在'
else
print'不存在'
例如:
use fireweb;
go
if exists(select * from dbo.sysobjects where id=object_id(n'temp_tbl') and objectproperty(id, 'istable') = 1)
print '存在'
else
print'不存在'
二、判断临时表是否存在:
方法1:
use fireweb;
go
if exists(select * from tempdb.dbo.sysobjects where id=object_id('tempdb..##temp_tbl'))
print '存在'
else
print'不存在'
方法2:
use fireweb;
go
if exists (select * from tempdb.dbo.sysobjects where id=object_id(n'tempdb..#temp_tbl') and type='u')
print '存在'
else
print'不存在'