1 object_id()函数
在SQLServer数据库中,如果查询数据库中是否存在指定名称的索引或者外键约束等,经常会用到object_id('name','type')方法,
语法:object_id('objectname')或object('objectname','type')
作用:该函数会返回指定对象的ID值,可以在sysobjects表中进行验证。
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
操作看一下;
2 进一步熟悉
第二个参数是类型,'U'是用户表;省略第二个也可以;
使用object_id()函数等同于 select id from sysobjects where name='xxx' and type='x';
也可以从id获取对象名,使用object_name();
类型 'V' 是指用户的视图;我有如下图的几个视图,名称都是view_xxx;
如果要模糊查询返回所有名称以 'view_' 打头的对象的情况,使用如下图语句;
select object_id('students','u')
select object_id('students')
select ID from sysobjects where name='students' and type='u'
select object_name(2105058535)--会取出ID值为ID的对象名
select name from sysobjects where id=2105058535
select object_id('view_activitys','V')
SELECT id, name, type FROM sysobjects WHERE name LIKE 'view_%';