SQL SERVER 中的 OBJECT / OBJECT_ID 函数(以及 OBJECT_NAME 函数)
SQL SERVER 中的 OBJECT / OBJECT_ID函数
OBJECT 或OBJECT_ID 都一样
语法:OBJECT ('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
例1:
SELECT OBJECT_ID('BS') -- 返回ID :12535785 SELECT OBJECT_ID('BS','U') -- 返回ID :12535785
--以上等同于: SELECT * FROM SYSOBJECTS WHERE name='BS' AND XTYPE='U' -- 返回ID :12535785
例2:
-- 判断表是否存在
IF OBJECT_ID('Table1', 'U') IS NOT NULL
BEGIN
PRINT '表Table1存在';
END;
ELSE
BEGIN
PRINT '表Table1不存在';
END;
OBJECT_NAME 函数
OBJECT_ID 根据对象名称返回该对象的ID.
OBJECT_NAME 根据对象ID返回对象名称.
SELECT OBJECT_ID(对象名)
--等同于:
SELECT * FROM SYSOBJECTS WHERE name='对象名'
SELECT OBJECT_NAME( ID )
--等同于:
SELECT * FROM SYSOBJECTS WHERE id='ID'
创建时间:2020.03.17 更新时间: