SQL server 生成数据字典

 

Set nocount on
DECLARE @TableName nvarchar(35), @TableComment nvarchar(200)
DECLARE Tbls CURSOR
FOR
Select distinct b.TABLE_NAME,convert(nvarchar(200),isnull(g.[value],'-')) AS TABLE_COMMENT
    FROM INFORMATION_SCHEMA.COLUMNS b
    inner join sys.tables a on a.name = b.TABLE_NAME
    left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0)
    --put any exclusions here
    --where table_name not like '%old'
    where b.table_name like '%dbo_%' ------此处是要生成数据字典的表的范围
    order by b.Table_name
OPEN Tbls
PRINT ''
PRINT ''
PRINT ''
PRINT '数据库字典'
PRINT ''
PRINT ''
PRINT ''
PRINT ''
FETCH NEXT FROM Tbls
INTO @TableName,@TableComment
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT '
' PRINT '

' + @TableName + ' '+@TableComment+'

' PRINT ''--Get the Description of the table--Characters 1-250PRINT''--Set up the Column Headers for the TablePRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''PRINT''--Get the Table DataSELECT'', '', '', '', '', '' , '', '', '', '' , '' , '' , ''FROM sys.tables AS tbl INNERJOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_idLEFTOUTERJOIN sys.indexes AS idx ON idx.object_id= clmns.object_idAND1=idx.is_primary_key LEFTOUTERJOIN sys.index_columns AS idxcol ON idxcol.index_id = idx.index_id AND idxcol.column_id = clmns.column_id AND idxcol.object_id= clmns.object_idAND0= idxcol.is_included_column LEFTOUTERJOIN sys.types AS udt ON udt.user_type_id = clmns.user_type_id LEFTOUTERJOIN sys.types AS typ ON typ.user_type_id = clmns.system_type_id AND typ.user_type_id = typ.system_type_id LEFTJOIN sys.default_constraints AS cnstr ON cnstr.object_id=clmns.default_object_id LEFTOUTERJOIN sys.extended_properties exprop ON exprop.major_id = clmns.object_idAND exprop.minor_id = clmns.column_id AND exprop.name ='MS_Description'WHERE (tbl.name =@TableNameand exprop.class =1) --I don't wand to include comments on indexesORDERBY clmns.column_id ASCPRINT'
字段名称 描述 主键 外键 类型 长度 数值精度 小数位数 允许为空 计算列 标识列 默认值
' + CAST(clmns.name AS VARCHAR(35)) + '' + ISNULL(CAST(exprop.value AS VARCHAR(500)),'') + '' + CAST(ISNULL(idxcol.index_column_id, 0)AS VARCHAR(20)) + '' + CAST(ISNULL( (SELECT TOP 1 1 FROM sys.foreign_key_columns AS fkclmn WHERE fkclmn.parent_column_id = clmns.column_id AND fkclmn.parent_object_id = clmns.object_id ), 0) AS VARCHAR(20)) + '' + CAST(udt.name AS CHAR(15)) + '' + CAST(CAST(CASE WHEN typ.name IN (N'nchar', N'nvarchar') AND clmns.max_length <> -1 THEN clmns.max_length/2 ELSE clmns.max_length END AS INT) AS VARCHAR(20)) + '' + CAST(CAST(clmns.precision AS INT) AS VARCHAR(20)) + '' + CAST(CAST(clmns.scale AS INT) AS VARCHAR(20)) + '' + CAST(clmns.is_nullable AS VARCHAR(20)) + '' + CAST(clmns.is_computed AS VARCHAR(20)) + '' + CAST(clmns.is_identity AS VARCHAR(20)) + '' + isnull(CAST(cnstr.definition AS VARCHAR(20)),'') + '
' PRINT '
' FETCH NEXT FROM Tbls INTO @TableName,@TableComment END PRINT '' CLOSE Tbls DEALLOCATE Tbls

 

 

转载于:https://www.cnblogs.com/dyhao/p/9213800.html

你可能感兴趣的:(SQL server 生成数据字典)