纵有疾风起
人生不言弃

Sql Server 判断表是否存在方法总结

#使用场景:

1、在创建表之前,需要先判断该表是否已经存在;

2、在删除表之前,需要先判断该表是否已经存在;

#方法总结:

1、判断实体表是否存在的方法:

1)、方法一:

if Exists(select top 1 * from sysObjects where Id=OBJECT_ID(N'UserInfos') and xtype='U')
    print '表UserInfos 存在'
else 
    print '表UserInfos 不存在'

2)、方法二:

if OBJECT_ID(N'UserInfos',N'U') is not null
    print '表UserInfos 存在!'
else 
    print '表UserInfos 不存在!'

2、判断临时表是否存在的方法:

1)、方法一:

if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#TempUsers') and type='U')
    print '临时表#TempUsers 存在!'
else 
    print '临时表#TempUsers 不存在!'

2)、方法二:

if OBJECT_ID(N'tempdb..#TempUsers',N'U') is not null
    print '临时表#TempUsers 存在!'
else 
    print '临时表#TempUsers 不存在!'

 

————————————————————————————————————

转载于:https://www.cnblogs.com/willingtolove/p/11122386.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/98829795

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » Sql Server 判断表是否存在方法总结
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录