首页 > 数据库 >从Sql server 2008获取表字段属性信息,注释信息

从Sql server 2008获取表字段属性信息,注释信息

时间:2023-03-21 11:32:25浏览次数:53  
标签:INFORMATION name object server sys Sql 2008 id SCHEMA


sqlserver 2008获得表的注释和字段的注释

select a.name, a.object_id as table_id,b.minor_id, b.value  
from sys.tables a left join sys.extended_properties b on a.object_id=b.major_id
where a.name='TABLE3' and b.minor_id=0 and b.name = 'MS_Description'
and a.schema_id=(
select schema_id from sys.schemas where name='dbo'
)

select a.name as table_name, b.name as column_name, c.value as remarks
from sys.tables a left join sys.columns b on a.object_id=b.object_id
left join sys.extended_properties c on a.object_id=c.major_id
where a.name='TABLE3' and c.minor_id<>0 and b.column_id=c.minor_id
and a.schema_id=(
select schema_id from sys.schemas where name='dbo'
)





今天无意中在网上发现Sqlserver有一个扩展属性系统表sysproperties,因为只接触过MSSQL2005及以后的版本,在生产库2008版本及联机文档上搜了下都找不到这个系统表,后来发现这个系统表在2005版本后就被另一个系统表sys.extended_properites所代替。



查看表的扩展属性:


select object_id from sys.sysobjects where name = table_name;
select * from sys.extended_properties where major_id = object_id;


扩展属性中有一name值是MS_Description,这个值是查看备注信息的。


select * from sys.extended_properties where major_id = object_id and name = 'MS_Description';


至于属性的意思,参考:[url]http://msdn.microsoft.com/zh-cn/library/ms177541(v=sql.105).aspx[/url]


[color=red]class[/color],tinyint, 标识其上存在属性的项类。可以是下列值之一:


0 = 数据库


1 = 对象或列


2 = 参数


3 = 架构


4 = 数据库主体


5 = 程序集


6 = 类型


7 = 索引


10 = XML 架构集合


15 = 消息类型


16 = 服务约定


17 = 服务


18 = 远程服务绑定


19 = 路由


20 = 数据空间(文件组或分区方案)


21 = 分区函数


22 = 数据库文件


27 = 计划指南



[color=red]class_desc[/color],nvarchar(60),其上存在扩展属性的类的说明。可以是下列值之一:


DATABASE


OBJECT_OR_COLUMN


PARAMETER


SCHEMA


DATABASE_PRINCIPAL


ASSEMBLY


TYPE


INDEX


XML_SCHEMA_COLLECTION


MESSAGE_TYPE


SERVICE_CONTRACT


SERVICE


REMOTE_SERVICE_BINDING


ROUTE


DATASPACE


PARTITION_FUNCTION


DATABASE_FILE


PLAN_GUIDE



[color=red]major_id[/color],int,其上存在扩展属性的项 ID,根据项类进行解释。对于大多数项,该 ID 适用于类所表示的项。下列是非标准主 ID 的解释:


如果 class 为 0,则 major_id 始终为 0。


如果 class 为 1、2 或 7,则 major_id 为 object_id。



[color=red]minor_id[/color],int,其上存在扩展属性的项辅助 ID,根据项类进行解释。对于大多数项,ID 为 0;否则,ID 为下列值之一:


如果 class = 1,则 minor_id 在项为列的情况下等于 column_id,在项为对象的情况下等于 0。


如果 class = 2,则 minor_id 为 parameter_id。


如果 class = 7,则 minor _id 为 index_id。



[color=red]name[/color],sysname,属性名,其 class、major_id 和 minor_id 是唯一的。



[color=red]value[/color],sql_variant,扩展属性的值。



[url]http://hi.baidu.com/wyg_bd/item/9c153f3d149300697d034b4c[/url]


在SqlServer中如何得到列的描述信息_sys.SysColumns和sys.extended_properties关系,sql-promptx下载


Sql开发工具下载地址 sql-promptx


SELECT a.*,b.value AS'描述内容' FROMsys.syscolumns a 
LEFTJOINsys.extended_propertiesb ON a.id=b.major_id
WHERE a.id=(SELECTOBJECT_ID('表名称'))



方法二:


select 
TableName = tbl.table_schema + '.' + tbl.table_name,
TableDescription = prop.value,
ColumnName = col.column_name,
ColumnDataType = col.data_type,
DescriptionValue=prop.value
FROM information_schema.tables tbl
INNER JOIN information_schema.columns col
ON col.table_name = tbl.table_name
LEFT JOIN sys.extended_properties prop
ON prop.major_id = object_id(tbl.table_schema + '.' + tbl.table_name)
AND prop.minor_id = 0
AND prop.name = 'MS_Description'
WHERE tbl.TABLE_NAME = '表名称'



二、 取得column_id,system_type_id,user_type_id,default_object_id


SELECT c.column_id,c.system_type_id,user_type_id, default_object_id FROM sys.columns AS c INNER JOIN sys.tables AS t ON c.object_id = t.object_id WHERE t.name = 'TabT' AND c.name = 'conlumName of TabT'



三,取得key_constraint_id


SELECT k.object_id FROM sys.key_constraintsAS k INNER JOIN sys.indexes AS i ON k.parent_object_id = i.object_id AND k.unique_index_id = i.index_id INNER JOIN sys.index_columnsAS x ON i.object_id = x.object_id AND i.index_id = x.index_id INNER JOIN sys.columnsAS c ON x.object_id = c.object_id AND x.column_id = c.column_id INNER JOIN sys.tables AS t ON c.object_id = t.object_id AND k.schema_id = t.schema_id WHERE i.is_disabled = 0 AND t.name = 'TabT' AND c.name = 'conlumName of TabT'



四、foreign_key_constraint_id


SELECT f.object_id FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS l ON f.object_id = l.constraint_object_id INNER JOIN sys.columns AS c ON f.parent_object_id = c.object_id AND l.parent_object_id = c.object_id AND l.parent_column_id = c.column_id INNER JOIN sys.tables AS t ON c.object_id = t.object_id AND f.schema_id = t.schema_id WHERE f.is_disabled = 0 AND t.name = 'TabT' AND c.name = 'conlumName of TabT'




[url]http://space.itpub.net/16436858/viewspace-624807[/url]


select   b.[value] from sys.columns a left join sys.extended_properties b on a.object_id=b.major_id
and a.column_id=b.minor_id inner join sysobjects c on a.column_id=c.id
and a.[name]='列名' and c.[name]='表名'
SELECT
表名=case when a.colorder=1 then d.name else '' end,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then '√' else '' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then '√'else '' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xusertype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0
--where d.name='orders' --如果只查询指定表,加上此条件
order by a.id,a.colorder




[url]http://coding-in.net/445/[/url]


select
INFORMATION_SCHEMA.TABLES.TABLE_CATALOG,
INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA,
INFORMATION_SCHEMA.TABLES.TABLE_NAME
from INFORMATION_SCHEMA.TABLES
order by INFORMATION_schema.TABLES.TABLE_NAME


[img]http://coding-in.net/blog/wp-content/uploads/Information_Schema_Sql_1.png[/img]




select
INFORMATION_schema.COLUMNS.TABLE_NAME,
INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME,
INFORMATION_schema.COLUMNS.DATA_TYPE,
INFORMATION_schema.COLUMNS.IS_NULLABLE
from INFORMATION_SCHEMA.COLUMNS
order by INFORMATION_schema.COLUMNS.TABLE_NAME


[img]http://coding-in.net/blog/wp-content/uploads/Information_Schema_Sql_2.png[/img]




select
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
,INFORMATION_SCHEMA.COLUMNS.IS_NULLABLE
,INFORMATION_SCHEMA.COLUMNS.DATA_TYPE
,INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
from INFORMATION_SCHEMA.COLUMNS
left outer join INFORMATION_SCHEMA.KEY_COLUMN_USAGE
on INFORMATION_SCHEMA.KEY_COLUMN_USAGE.COLUMN_NAME = INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
and INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME = INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
and INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_CATALOG = INFORMATION_SCHEMA.COLUMNS.TABLE_CATALOG
and INFORMATION_SCHEMA.KEY_COLUMN_USAGE.ORDINAL_POSITION = INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS on
INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME
and INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.KEY_COLUMN_USAGE.CONSTRAINT_NAME
order by INFORMATION_schema.COLUMNS.TABLE_NAME


[img]http://coding-in.net/blog/wp-content/uploads/Information_Schema_Sql_3.png[/img]


标签:INFORMATION,name,object,server,sys,Sql,2008,id,SCHEMA
From: https://blog.51cto.com/u_3871599/6139969

相关文章

  • mysql5.7奇怪bug
          看这个查询很奇怪,1145就错,子查询返回多于 1 行。。。 1就对。1145跟1144都不行,1143可以。更新为0 ,是正常执行的    1145也只有一条......
  • PostgreSQL查询所有表的信息
    查询PostgreSQL所有表信息有以下两种方式:使用工具提供的\d命令查询使用SQL语句进行查询方法一:通过命令行查询登录:psql-U用户名\l:查看系统中现存的数据库\c:切换库......
  • AndServer
    Android服务器搭建框架https://github.com/yanzhenjie/AndServerhttps://blog.csdn.net/yanzhenjie1003/article/details/64090436......
  • [MySql] 数据库死锁的排查和相关知识
    查看数据库最近的一次死锁执行以下命令:showengineinnodbstatus;查询结果......------------------------LATESTDETECTEDDEADLOCK------------------------2......
  • MySQL——知识脑图
    摘要主要给大家几张的个人总结的有关于Mysql知识脑图,帮助大家更好的学习和记忆相关内容。 博文参考......
  • Mysql数据脱敏
    1、姓名脱敏updatetableset列=REPLACE(列,SUBSTR(列,2,1),'*')PS:脱敏效果:张*,张*三2、手机号脱敏UPDATEtableSET列=(CASEWHEN列ISNOTNULLTHENSUBSTR......
  • SQL Server 导出链接服务器用户名密码
    DACconnectivitytoMSSQLinstancesLocaladministratorprivileges(neededtoaccessregistrykey)SysadminprivilegestoMSSQLinstancesfunctionGet-MSSQLL......
  • 力扣511(MySQL)-游戏玩法分析Ⅰ(简单)
    题目:活动表 Activity:写一条SQL 查询语句获取每位玩家 第一次登陆平台的日期。查询结果的格式如下所示:  解题思路:方法一:使用dense_rank()over(partitionby......
  • #yyds干货盘点#PostgreSQL数据库备份与恢复
    备份所有数据库pg_dumpall>db.out复制代码恢复所有数据库#执行这个命令的时候连接到哪个数据库无关紧要,因为pg_dumpall创建的脚本将会包含恰当的创建和连接数据库的命......
  • MySQL—— 分组查询
    分组查询  分组查询主要涉及到两个子句,分别是:groupby和having。  在实际的应用中,可能有这样的需求,需要先进行分组,然后对每一组的数据进行操作,这个时候我们需要使用......