SQL Server 导出数据字典

如何在 SQL Server 中导出数据库对应的数据字典

  1. 设置中,SQL 查询结果 - 在结果集中显示标题 反勾选
    SQL Server 导出数据字典_第1张图片
  2. 执行下列SQL查询,复制输出结果
Set nocount on
DECLARE @TableName nvarchar(35)
DECLARE Tbls CURSOR
FOR
    Select distinct Table_name
    FROM INFORMATION_SCHEMA.COLUMNS
    --put any exclusions here
    --where table_name not like '%old'
    order by Table_name
OPEN Tbls
PRINT ''
PRINT ''
PRINT ''
PRINT ''
PRINT '数据库字典'
PRINT ''
PRINT ''
PRINT ''
FETCH NEXT FROM Tbls
INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT '
' Select '

' + @TableName + ' : '+cast(Value as varchar(1000)) + '

'
FROM sys.extended_properties A WHERE A.major_id = OBJECT_ID(@TableName) and name = 'MS_Description' and minor_id = 0 PRINT '' --Get the Description of the table --Characters 1-250 PRINT '' --Set up the Column Headers for the Table PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' PRINT '' --Get the Table Data SELECT'', '', '', '', '', '' , '', '', '', '' , '' , '' , ''FROM sys.tables AS tbl INNERJOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_id LEFTOUTERJOIN sys.indexes AS idx ON idx.object_id = clmns.object_id AND1 =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_id AND0 = 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_id AND exprop.minor_id = clmns.column_id AND exprop.name = 'MS_Description'WHERE (tbl.name = @TableName and exprop.class = 1) --I don't wand to include comments on indexes ORDER BY clmns.column_id ASC PRINT 'table>' PRINT '' FETCH NEXT FROM Tbls INTO @TableName END PRINT '' CLOSE Tbls DEALLOCATE Tbls

3.输出结果,导入HTML
SQL Server 导出数据字典_第2张图片

许久没更新博客,刷刷存在感

你可能感兴趣的:(SqlServer)

字段名称描述主键外键类型长度数值精度小数位数允许为空计算列标识列默认值
' + 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)),'') + '