码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-07-02 21:32:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:else   div   users   cts   null   rom   code   print   _id   

#使用场景:

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 不存在!‘

 

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

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

标签:else   div   users   cts   null   rom   code   print   _id   

原文地址:https://www.cnblogs.com/willingtolove/p/11122386.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!