willingtolove

#使用场景:

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

 

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

分类:

技术点:

相关文章:

  • 2021-12-29
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-02-02
  • 2021-12-08
  • 2021-09-19
猜你喜欢
  • 2022-02-05
  • 2021-12-03
  • 2022-01-19
  • 2021-07-12
  • 2022-02-02
  • 2022-02-15
  • 2021-12-27
相关资源
相似解决方案