檢查資料庫是否存在某物件(SQLserver)

2021-09-30 16:09:15 字數 1299 閱讀 1720

objectproperty : 返回當前資料庫中物件的有關資訊

語法 : objectproperty ( id , property )

例如:objectproperty (object_id(n'authors') , n'istable') = 1

或select * from dbo.sysobjects   where object_id(n'mytable') and objectproperty(id, n'istable') = 1

object_id:返回資料庫物件標識號。n是顯式的將非unicode字元轉成unicode字元,它來自 sql-92 標準中的 national(unicode)資料型別,用於擴充套件和標準化,在這裡可以不用,寫作object_id(perpersondata)。

objectproperty:返回當前資料庫中物件的有關資訊。1表「真」。同樣可以寫成objectproperty(id, susertable) = 1。

整條語句的意思是判斷資料庫裡有沒有存在perpersondata這樣一張表。

整條語句可以簡寫成:

if exists (select * from sysobjects where objectproperty(object_id('perpersondata'),'istable') = 1)

判斷mytalbe物件是否是乙個表。

if exists (select * from dbo.sysobjects

where id = object_id(n'mytable') and objectproperty(id, n'isusertable') = 1)

判斷myproc物件是否是乙個儲存過程。

if exists (select * from dbo.sysobjects

where id = object_id(n'myproc') and objectproperty(id, n'isprocedure') = 1)

判斷myfun物件是否是乙個自定義、標量值函式。

if exists (select * from dbo.sysobjects

where id = object_id(n'myfun') and objectproperty(id, n'isscalarfunction') = 1)

判斷myfun物件是否是乙個錶值函式。

if exists (select * from dbo.sysobjects

where id = object_id(n'myfun') and objectproperty(id, n'istablefunction') = 1)

SQL查詢資料庫是否存在

在實際工作中會遇到通過sql查詢資料庫是否存在的情況,下面一些語句可以提供一些幫助,本文的語句是在sql08r2中測試的 1,查詢當前資料庫伺服器所有資料庫 select from master.dbo.sysdatabases 2,查詢資料庫是否存在 select count from maste...

判斷SQL資料庫是否存在表,是否存在記錄

sql資料庫,當判斷一條記錄存在時,則更新更記錄,當記錄不存在時,則新增該記錄 使用sql語句在c 中實現,sql語句 if exists select from 表 where 條件 begin update 表 set 字段 字段值 where 條件 endelse begin insert i...

檢查mysql資料庫是否存在壞表指令碼

1 bin bash 2 此指令碼的主要用途是檢測mysql伺服器上所有的db或者單獨db中的壞表 3 變數說明 pass mysql賬戶口令 name mysql賬號名稱 data path mysql目錄路徑 directory list 目錄列表 file list檔案列表 db name 資...