使用扩展属性快速创建SQL Server数据字典
2008-09-20 10:05:40 来源:WEB开发网还可以通过右键单击SSMS中的对象并选择属性来查看扩展属性,如下图所示:
如果你的数据库在扩展属性中有数据,那么你可以运行查询来提取这个数据。在SQL Server管理套件中,选择Tools | Options,并在Results to Text中不选“Include column headers in the result set”(在结果集中包含字段头)选项。这将使显示在每个字段名称下面的结果集都不包含字段头。
HTML格式的数据字典
运行下面的脚本来生成数据字典,并保存结果到一个扩展名为“.htm”的文件中。
生成数据字典的示例T-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 '<HTML><body>'
FETCH NEXT FROM Tbls
INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT '</br>'
PRINT '<table border="1">'
Print '<B>' + @TableName + '</B>'
PRINT '</br>'
--Get the Description of the table
--Characters 1-250
Select substring(cast(Value as varchar(1000)),1,250) FROM
sys.extended_properties A
WHERE A.major_id = OBJECT_ID(@TableName)
and name = 'MS_Description' and minor_id = 0
--Characters 251-500
Select substring(cast(Value as varchar(1000)),251, 250) FROM
sys.extended_properties A
WHERE A.major_id = OBJECT_ID(@TableName)
and name = 'MS_Description' and minor_id = 0
PRINT '<tr><b>'
--Set up the Column Headers for the Table
PRINT '<td><b>Column Name</b></td>'
PRINT '<td><b>Description</b></td>'
PRINT '<td><b>InPrimaryKey</b></td>'
PRINT '<td><b>IsForeignKey</b></td>'
PRINT '<td><b>DataType</b></td>'
PRINT '<td><b>Length</b></td>'
PRINT '<td><b>Numeric Precision</b></td>'
PRINT '<td><b>Numeric Scale</b></td>'
PRINT '<td><b>Nullable</b></td>'
PRINT '<td><b>Computed</b></td>'
PRINT '<td><b>Identity</b></td>'
PRINT '<td><b>Default Value</b></td>'
--Get the Table Data
SELECT '</b></tr>',
'<tr>',
'<td>' + CAST(clmns.name AS VARCHAR(35)) + '</td>',
'<td>' + substring(ISNULL(CAST(exprop.value AS VARCHAR(255)),''),1,250),
substring(ISNULL(CAST(exprop.value AS VARCHAR(500)),''),251,250) + '</td>',
'<td>' + CAST(ISNULL(idxcol.index_column_id, 0)AS VARCHAR(20)) + '</td>',
'<td>' + 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)) + '</td>',
'<td>' + CAST(udt.name AS CHAR(15)) + '</td>' ,
'<td>' + 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)) + '</td>',
'<td>' + CAST(CAST(clmns.precision AS INT) AS VARCHAR(20)) + '</td>',
'<td>' + CAST(CAST(clmns.scale AS INT) AS VARCHAR(20)) + '</td>',
'<td>' + CAST(clmns.is_nullable AS VARCHAR(20)) + '</td>' ,
'<td>' + CAST(clmns.is_computed AS VARCHAR(20)) + '</td>' ,
'<td>' + CAST(clmns.is_identity AS VARCHAR(20)) + '</td>' ,
'<td>' + isnull(CAST(cnstr.definition AS VARCHAR(20)),'') + '</td>'
FROM sys.tables AS tbl
INNER JOIN sys.all_columns AS clmns
ON clmns.object_id=tbl.object_id
LEFT OUTER JOIN sys.indexes AS idx
ON idx.object_id = clmns.object_id
AND 1 =idx.is_primary_key
LEFT OUTER JOIN 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
AND 0 = idxcol.is_included_column
LEFT OUTER JOIN sys.types AS udt
ON udt.user_type_id = clmns.user_type_id
LEFT OUTER JOIN sys.types AS typ
ON typ.user_type_id = clmns.system_type_id
AND typ.user_type_id = typ.system_type_id
LEFT JOIN sys.default_constraints AS cnstr
ON cnstr.object_id=clmns.default_object_id
LEFT OUTER JOIN 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 '</tr></table>'
FETCH NEXT FROM Tbls
INTO @TableName
END
PRINT '</body></HTML>'
CLOSE Tbls
DEALLOCATE Tbls
这个脚本将被安排作为一个工作来运行,所以你不需要担心要手动更新文档。
因为你将文档存储在数据库中,所以你不必担心拥有多个拷贝并指出哪个是最新的。它还会与数据库一起备份。
这个脚本不会添加扩展属性,但是显然你将丢失这些描述,所以花些时间将这些信息添加到你的环境中。
更多精彩
赞助商链接