首页 > 代码库 > sql server: MS_Description
sql server: MS_Description
--SQL Server表描述 及 字段描述的增、删、改、查询--sql server 2000系统表sysproperties在SQL 2008中无效的问题 今天无意中在网上发现Sqlserver有一个扩展属性系统表sysproperties,因为只接触过MSSQL2005及以后的版本,在生产库2008版本及联机文档上搜了下都找不到这个系统表,后来发现这个系统表在2005版本后就被另一个系统表sys.extended_properites所代替。 --select * from sys.extended_properites where major_id = object_id and name = ‘MS_Description‘--测试:--创建表及描述信息create table geovindu(duname varchar(10),isname char(2))--为表添加描述信息EXECUTE sp_addextendedproperty N‘MS_Description‘, ‘人员信息表‘, N‘user‘, N‘dbo‘, N‘table‘, N‘geovindu‘, NULL, NULL--为字段duname添加描述信息EXECUTE sp_addextendedproperty N‘MS_Description‘, ‘姓名‘, N‘user‘, N‘dbo‘, N‘table‘, N‘geovindu‘, N‘column‘, N‘duname‘--为字段isname添加描述信息EXECUTE sp_addextendedproperty N‘MS_Description‘, ‘性别‘, N‘user‘, N‘dbo‘, N‘table‘, N‘geovindu‘, N‘column‘, N‘isname‘--更新表中列duname的描述属性:EXEC sp_updateextendedproperty ‘MS_Description‘,N‘聚文‘,‘user‘,dbo,‘table‘,‘geovindu‘,‘column‘,dunameEXEC sp_updateextendedproperty N‘MS_Description‘, ‘涂聚文‘, N‘user‘, N‘dbo‘, N‘table‘, N‘geovindu‘, N‘column‘, N‘duname‘--删除表中列duname的描述属性:EXEC sp_dropextendedproperty ‘MS_Description‘,‘user‘,dbo,‘table‘,‘geovindu‘,‘column‘,duname--至于查询出来,sql server有提供系统函数fn_listextendedproperty ():--获取某一个字段的描述SELECT * FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘geovindu‘, ‘column‘, default)--其他变数,按照你的要求你照写即可,只要表名换成你的where objname = ‘字段名--删除测试drop table geovindugo---1.SQL查询表的所有字段的备注说明SELECT Sysobjects.name AS TABLE_NAME, syscolumns.Id, syscolumns.name AS COLUMN_NAME,systypes.name AS DATA_TYPE, syscolumns.length as CHARACTER_MAXIMUM_LENGTH, sys.extended_properties.[value] AS COLUMN_DESCRIPTION, syscomments.text as COLUMN_DEFAULT,syscolumns.isnullable as IS_NULLABLE FROM syscolumns INNER JOIN systypes ON syscolumns.xtype = systypes.xtype LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id LEFT OUTER JOIN sys.extended_properties ON ( sys.extended_properties.minor_id = syscolumns.colid AND sys.extended_properties.major_id = syscolumns.id) LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id WHERE syscolumns.id IN (SELECT id FROM SYSOBJECTS WHERE xtype = ‘U‘) AND (systypes.name <> ‘sysname‘) ORDER BY syscolumns.colid--2.SQL查询表的所有字段的备注说明SELECT (case when a.colorder=1 then d.name else ‘‘ end) N‘表名‘, a.colorder N‘字段序号‘, a.name N‘字段名‘, (case when COLUMNPROPERTY( a.id,a.name,‘IsIdentity‘)=1 then ‘√‘else ‘‘ end) N‘标识‘, (case when (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND (xtype = ‘PK‘))>0 then ‘√‘ else ‘‘ end) N‘主键‘, b.name N‘类型‘, a.length N‘占用字节数‘, COLUMNPROPERTY(a.id,a.name,‘PRECISION‘) as N‘长度‘, isnull(COLUMNPROPERTY(a.id,a.name,‘Scale‘),0) as N‘小数位数‘, (case when a.isnullable=1 then ‘√‘else ‘‘ end) N‘允许空‘, isnull(e.text,‘‘) N‘默认值‘, isnull(g.[value],‘‘) AS N‘字段说明‘ FROM syscolumns a left join systypes b on a.xtype=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 order by object_name(a.id),a.colorder--3. SQL 2005查询表的所有字段的备注说明SELECT TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N‘‘ END,TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N‘‘),Column_id=C.column_id,ColumnName=C.name,PrimaryKey=ISNULL(IDX.PrimaryKey,N‘‘),[IDENTITY]=CASE WHEN C.is_identity=1 THEN N‘√‘ELSE N‘‘ END,Computed=CASE WHEN C.is_computed=1 THEN N‘√‘ELSE N‘‘ END,Type=T.name,Length=C.max_length,Precision=C.precision,Scale=C.scale,NullAble=CASE WHEN C.is_nullable=1 THEN N‘√‘ELSE N‘‘ END,[Default]=ISNULL(D.definition,N‘‘),ColumnDesc=ISNULL(PFD.[value],N‘‘),IndexName=ISNULL(IDX.IndexName,N‘‘),IndexSort=ISNULL(IDX.Sort,N‘‘),Create_Date=O.Create_Date,Modify_Date=O.Modify_dateFROM sys.columns CINNER JOIN sys.objects OON C.[object_id]=O.[object_id]AND O.type=‘U‘AND O.is_ms_shipped=0INNER JOIN sys.types TON C.user_type_id=T.user_type_idLEFT JOIN sys.default_constraints DON C.[object_id]=D.parent_object_idAND C.column_id=D.parent_column_idAND C.default_object_id=D.[object_id]LEFT JOIN sys.extended_properties PFDON PFD.class=1 AND C.[object_id]=PFD.major_id AND C.column_id=PFD.minor_id-- AND PFD.name=‘Caption‘ -- 字段说明对应的描述名称(一个字段可以添加多个不同name的描述)LEFT JOIN sys.extended_properties PTBON PTB.class=1 AND PTB.minor_id=0 AND C.[object_id]=PTB.major_id-- AND PFD.name=‘Caption‘ -- 表说明对应的描述名称(一个表可以添加多个不同name的描述) LEFT JOIN -- 索引及主键信息(SELECT IDXC.[object_id],IDXC.column_id,Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,‘IsDescending‘)WHEN 1 THEN ‘DESC‘ WHEN 0 THEN ‘ASC‘ ELSE ‘‘ END,PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N‘√‘ELSE N‘‘ END,IndexName=IDX.NameFROM sys.indexes IDXINNER JOIN sys.index_columns IDXCON IDX.[object_id]=IDXC.[object_id]AND IDX.index_id=IDXC.index_idLEFT JOIN sys.key_constraints KCON IDX.[object_id]=KC.[parent_object_id]AND IDX.index_id=KC.unique_index_idINNER JOIN -- 对于一个列包含多个索引的情况,只显示第1个索引信息(SELECT [object_id], Column_id, index_id=MIN(index_id)FROM sys.index_columnsGROUP BY [object_id], Column_id) IDXCUQON IDXC.[object_id]=IDXCUQ.[object_id]AND IDXC.Column_id=IDXCUQ.Column_idAND IDXC.index_id=IDXCUQ.index_id) IDXON C.[object_id]=IDX.[object_id]AND C.column_id=IDX.column_id --WHERE O.name=N‘geovindu‘ -- 如果只查询指定表,加上此条件ORDER BY O.name,C.column_id
sql server: MS_Description
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。