MSSQL判断表或临时表是否存在
http://www.yqxbc.com  发布时间:2015-04-05 19:26 来源: 浏览:加载中

一、判断数据表是否存在:

方法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'不存在'
如果你有好的win10资讯或者win10教程,以及win10相关的问题想要获得win10系统下载的关注与报道。
欢迎加入发送邮件到657025171#qq.com(#替换为@)。期待你的好消息!