select object_id('名称') ,object_id('名称','类型')
1. 等价于
select * from sys.objects where name ='名称'select * from sys.objects where name ='名称' and type ='类型'
2. 可用于判断表等是否存在
如:
if(OBJECT_ID('rdrecord','u') is not null)print '存在'elseprint '不存在'
3. 可返回sys.objects系统视图中的object_id的值 如果没有则返回null
4. sys.objects里有所有的数据库对像 如:表、视图、列、存储过程、函数、约束等等等等 5. 视了试图、表、存储过程类型明确的可用 object_id('名称','类型')判断。当然sql2008r2(以前版本未测试)一个数据库中的对像名是不能重复的所以也可只用对像名判断别的类型可用类似方法
select * from sys.objects where object_id =object_id(N'DF__rdrecord__Sex__07020F21') and parent_object_id=object_id('表名','类型')select COL_LENGTH('表','列');--列的长度 判断列是否存在
6. 类型如下
V = View
U = Table (user-defined)P = SQL Stored ProcedureAF = Aggregate function (CLR)C = CHECK constraintD = DEFAULT (constraint or stand-alone)F = FOREIGN KEY constraintFN = SQL scalar functionFS = Assembly (CLR) scalar-functionFT = Assembly (CLR) table-valued functionIF = SQL inline table-valued functionIT = Internal tablePC = Assembly (CLR) stored-procedurePG = Plan guidePK = PRIMARY KEY constraintR = Rule (old-style, stand-alone)RF = Replication-filter-procedureS = System base tableSN = SynonymSQ = Service queueTA = Assembly (CLR) DML triggerTF = SQL table-valued-functionTR = SQL DML triggerUQ = UNIQUE constraintX = Extended stored procedure表中结构如下图