Sql Server中判斷表或者數(shù)據(jù)庫是否存在

  1. 數(shù)據(jù)庫
      法(一):
 select * From master.dbo.sysdatabases where name='數(shù)據(jù)庫名'

法(二):

    if db_id('數(shù)據(jù)庫名') is not null
      drop database 。。。
      go
      create 。。。
  1. 表對象
  select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫名.dbo.表名')
  if exists (select count(*) from sysobjects where id = object_id('數(shù)據(jù)庫名.dbo.表名'))
    print '存在'
  else
    print '不存在'
  1. 表中字段
  if exists(select * from syscolumns where name='colname1' and id=object_id('數(shù)據(jù)庫名.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 。。。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容