MSSQL sql脚本生成数据字典(表结构文档)的方法

select 
    [表名称]=c.Name,
    [表说明内容]=isnull(f.[value],''),
    [列名称]=a.Name,
    [列顺序号]=a.Column_id,
    [是否标识别]=case when is_identity=1 then '是' else '' end,
    [是否主鍵]=case when exists(select 1 from sys.objects x join sys.indexes y on x.Type=N'PK' and x.Name=y.Name 
                        join sysindexkeys z on z.ID=a.Object_id and x.parent_object_id = z.id and z.indid=y.index_id and z.Colid=a.Column_id)
                    then '是' else '' end,
    [列类型]=b.Name,
    [列长度]=case when ColumnProperty(a.object_id,a.Name,'Precision')=-1 then '2^31-1'
                else rtrim(ColumnProperty(a.object_id,a.Name,'Precision')) end,
    [列精度]=isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0),
    [可null标志]=case when a.is_nullable=1 then '√' else '' end,
    [列相关说明]=isnull(e.[value],''),
    [列默认值]=isnull(d.text,'')    
from 
    sys.columns a(nolocK)
left join
    sys.types b(nolocK) on a.user_type_id=b.user_type_id
inner join
    sys.objects c(nolocK) on a.object_id=c.object_id and c.Type='U'
left join
    syscomments d(nolocK) on a.default_object_id=d.ID
left join
    sys.extended_properties e(nolocK) on e.major_id=c.object_id and e.minor_id=a.Column_id and e.class=1 
left join
    sys.extended_properties f(nolocK) on f.major_id=c.object_id and f.minor_id=0 and f.class=1