博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Ms Sql server 表列等是否存在
阅读量:5306 次
发布时间:2019-06-14

本文共 1352 字,大约阅读时间需要 4 分钟。

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 Procedure
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
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
UQ = UNIQUE constraint
X = Extended stored procedure

表中结构如下图

 

转载于:https://www.cnblogs.com/SoftWareIe/p/9472726.html

你可能感兴趣的文章