SQL SERVER 中的 object_id()函數

來源:互聯網
上載者:User

標籤:

在SQLServer資料庫中,如果查詢資料庫中是否存在指定名稱的索引或者外鍵約束等,經常會用到object_id(‘name‘,‘type‘)方法,做筆記如下:

?

文法:object_id(‘objectname‘)或object(‘objectname‘,‘type‘)

作用:該函數會返回指定對象的ID值,可以在sysobjects表中進行驗證。

其中objectname資料類型為 char nchar。如果 object 的資料類型是 char,那麼隱性將其轉換成 nchar。

type如下列表:

AF = Aggregate function (CLR)
C = CHECK constraint
D = DEFAULT (constraint or stand-alone)
F = FOREIGN KEY constraint
FN = SQL scalar function
FS = Assembly (CLR) scalar-function
FT = Assembly (CLR) table-valued function
IF = SQL inline table-valued function
IT = Internal table
P = SQL Stored Procedure
PC = Assembly (CLR) stored-procedure
PG = Plan guide
PK = PRIMARY KEY constraint
R = Rule (old-style, stand-alone)
RF = Replication-filter-procedure
S = System base table
SN = Synonym
SQ = Service queue
TA = Assembly (CLR) DML trigger
TF = SQL table-valued-function
TR = SQL DML trigger
U = Table (user-defined)
UQ = UNIQUE constraint
V = View
X = Extended stored procedure

?

例子如下:

select object_id(‘fk_xxx_xx‘,‘F‘)會取出約束名為fk_xxx_xx的ID值,如返回144444444這個值。

上面的語句等同:

select ID from sysobjects where name=‘fk_xxx_xx‘ and type=‘F‘

======================================================================================= 

大家都知道在資料庫中有一個系統資料表sysobjects,裡面儲存了資料庫各個對象的資訊。可以查詢下看看結果。可以看出每個對象都有一個ID,這個表格儲存體了表,預存程序,觸發器,視圖等相關資訊。注意:欄位沒有。

  object_id就是根據對象名稱返回該對象的id.
  object_name是根據對象id返回對象名稱.
 

  select object_id(對象名)等同於:
  select id from sysobjects wherename=對象名

  select object_name(id號)等同於:
  select name from sysobjects where id=id號

 

 

 SQL SERVER2000以上版本都支援這個函數。

SQL SERVER 中的 object_id()函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.