SQLServer2008/2005 生成数据字典SQL语句

在网上版本的基础上调整成按表名排序,且排除数据库关系图相关的表sysdiagrams。

表头:表名 表说明 字段序号 字段名 标识 主键 类型 占用字节数 长度 小数位数 允许空 默认值 字段说明

发现的问题:字段序号可能不连续。

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.xtype=b.xusertype 
inner join sysobjects d on a.id=d.id and d.xtype='u' and d.name<>'dtproperties' and d.name<>'sysdiagrams'  
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='要查询的表' --如果只查询指定表,加上此条件 
order by d.name,a.id,a.colorder

 分别将“表头”和查询结果贴到Excel,双击调整Excel列宽即可得到一份完整的数据字典Excel文件。

参考:http://www.cnblogs.com/SoulStore/archive/2011/04/14/2015639.html

posted @ 2013-03-01 16:31  志在必得Shaun  阅读(279)  评论(0编辑  收藏  举报