Fork me on GitHub

SQL SERVER 查看表说明,字段属性

查询表字段属性,说明等:

 1 SELECT  
 2 表名=case when a.colorder=1 then d.name else '' end,
 3 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
 4 字段序号=a.colorder,
 5 字段名=a.name,
 6 标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then ''else '' end,
 7 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
 8   SELECT name FROM sysindexes WHERE indid in(
 9    SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
10    ))) then '' else '' end,
11 类型=b.name,
12 占用字节数=a.length,
13 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
14 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
15 允许空=case when a.isnullable=1 then ''else '' end,
16 默认值=isnull(e.text,''),
17 字段说明=isnull(g.[value],'')
18 FROM syscolumns a
19 left join systypes b on a.xtype=b.xusertype
20 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
21 left join syscomments e on a.cdefault=e.id
22 left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id
23 left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0
24 where d.name='表名' --表名
25 order by a.id,a.colorder
View Code

只查询表字段说明:

 1 SELECT 
 2     表名 = OBJECT_NAME(c.object_id), 
 3     字段名称 = c.name, 
 4     字段说明 = ex.value 
 5 FROM 
 6     sys.columns c 
 7 LEFT OUTER JOIN 
 8     sys.extended_properties ex 
 9 ON 
10     ex.major_id = c.object_id 
11     AND ex.minor_id = c.column_id 
12     AND ex.name = 'MS_Description' 
13 WHERE 
14     OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0 
15      AND OBJECT_NAME(c.object_id) = 'table_1'  -- 这里加上你的表名字
View Code

查询表在那个数据库:

1 exec sp_MSforeachdb @command1='USE ? if exists(SELECT 1 from sysobjects where id=object_id(''MemberInfo'')) PRINT ''?'''
View Code

查询字段在那些表:

1 select 
2 sysobjects.name as tablename,
3 syscolumns.name as columnname
4 from syscolumns
5  join sysobjects on sysobjects.id = syscolumns.id
6 where syscolumns.name like '%ProductEntryId%'
View Code

查询字段在那些存储过程中:

1 SELECT obj.Name 存储过程名,
2  sc.TEXT 存储过程内容  
3 FROM syscomments sc  
4 INNER JOIN sysobjects obj ON sc.Id = obj.ID  
5 WHERE sc.TEXT LIKE '%' + '字段名' + '%'  
6 AND TYPE = 'P'  
View Code

 

今天的分享到此结束,有需要的可以关注留言讨论

能帮朋友解决问题的记得给个关注支持一下,以后将多多分享SQL SERVER 相关知识

posted @ 2022-06-13 16:45  酒笙匿清栀  阅读(632)  评论(0编辑  收藏  举报