Sql Server中判断表或者数据库是否存在

  1. 数据库
      法(一):
 select * From master.dbo.sysdatabases where name='数据库名'

法(二):

    if db_id('数据库名') is not null
      drop database 。。。
      go
      create 。。。
  1. 表对象
  select count(*) from sysobjects where id = object_id('数据库名.dbo.表名')
  if exists (select count(*) from sysobjects where id = object_id('数据库名.dbo.表名'))
    print '存在'
  else
    print '不存在'
  1. 表中字段
  if exists(select * from syscolumns where name='colname1' and id=object_id('数据库名.dbo.表名'))
    print '存在'
  else
    print '不存在'
 (代表表tablename1中存在colname1字段 )
例:select * from syscolumns where name='Test' and id=object_id('dbo.test')
  1. 存储过程或视图
  if object_id('视图或存储过程名') is not null
    drop proc/view 。。。
   go
   create proc/view 。。。
  或
  if Exists(select * from sysobjects where name='视图或存储过程名' AND   type = 'P/V')
    drop proc/view 。。。
  go  
  create proc/view 。。。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容