SQL Server2008生成数据库字典

1.我们在开发过程中可能会遇到这样的一种情况“当我们进行维护其他人的项目时或者项目的二次开发时可能会对原始的数据表进行分析”,这里为大家介绍一种方便快捷生成数据库字典的方式。

image

我们在数据表中右键,选择属性

image

在名称这项添加一个“MS_Description”,需要注意的是必须在数据表上添加扩展数据,数据库的扩展属性添加上没用,当然有多张数据表的时候我们要对每张数据表添加这样一个值,如果数据表多了我们这样去做是不是觉得很麻烦呢。这里我们想到使用“游标的方式”给每个表添加上这样一个扩展属性。

declare testCur cursor scroll for
select name from sysobjects where xtype='u'

open testCur
declare @name varchar(100);

fetch next from testCur into @name

while @@FETCH_STATUS=0
begin
EXECUTE sp_addextendedproperty N'MS_Description', '', N'user', N'dbo', N'table', @name, NULL, NULL
fetch next from testCur into @name
end

close testCur
deallocate testCur

这里我们循环给数据库中所有的数据表添加一个“MS_Description”的扩展属性

这里我们用到了“

sp_addextendedproperty

”这个存储过程,与其相对应的还有”sp_updateextendedproperty ”,和”sp_dropextendedproperty ”,

如果有不了解的同学,可以找度娘。

如果我们想删除所有数据表的扩展属性怎么办呢,这里我们又使用到游标。

declare @omid int
declare @odid int
declare @name varchar(1000)
declare @tabname varchar(1000)
declare @colname varchar(1000)
 
declare ap scroll cursor for
  select major_id,minor_id,name from sys.extended_properties
  where major_id in (select ID from sysobjects where xtype in('U'))
 
open ap
 
fetch first from ap into @omid,@odid,@name
while(@@FETCH_STATUS <> -1)
begin
    begin
      select @tabname=name from sysobjects where ID=@omid
      print 'EXEC sys.sp_dropextendedproperty @name=''' + @name + ''',@level0type=N''SCHEMA'',@level0name=N''dbo'',@level1type=N''TABLE'',@level1name=N''' + @tabname + ''' '
      print 'go'
    end
 
  fetch next from ap into @omid,@odid,@name
end
 
close ap
deallocate ap

执行后会生成执行存储过程的脚本,我们复制下来执行就可以了。

最后,我们就到了要生成生成数据表字典的时候了,我直接贴代码吧。

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 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' 
PRINT '<html xmlns="http://www.w3.org/1999/xhtml">' 
PRINT '<head>' 
PRINT '<title>数据库字典</title>' 
PRINT '<style type="text/css">' 
PRINT 'body{margin:0; font:11pt "arial", "微软雅黑"; cursor:default;}' 
PRINT '.tableBox{margin:10px auto; padding:0px; width:1000px; height:auto; background:#FBF5E3; border:1px solid #45360A}' 
PRINT '.tableBox h3 {font-size:12pt; height:30px; line-height:30px; background:#45360A; padding:0px 0px 0px 15px; color:#FFF; margin:0px; text-align:left }' 
PRINT '.tableBox table {width:1000px; padding:0px }' 
PRINT '.tableBox th {height:25px; border-top:1px solid #FFF; border-left:1px solid #FFF; background:#F7EBC8; border-right:1px solid #E0C889; border-bottom:1px solid #E0C889 }' 
PRINT '.tableBox td {height:25px; padding-left:10px; border-top:1px solid #FFF; border-left:1px solid #FFF; border-right:1px solid #E0C889; border-bottom:1px solid #E0C889 }' 
PRINT '</style>' 
PRINT '</head>' 
PRINT '<body>' 
FETCH NEXT FROM Tbls 
INTO @TableName 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    PRINT '<div class="tableBox">' 
    Select '<h3>' + @TableName + ' : '+cast(Value as varchar(1000)) + '</h3>' 
    FROM sys.extended_properties A 
    WHERE A.major_id = OBJECT_ID(@TableName) 
    and name = 'MS_Description' and minor_id = 0 
    PRINT '<table>' 
    PRINT '<tr>' 
    PRINT '<th>字段名称</th>' 
    PRINT '<th>描述</th>' 
    PRINT '<th>主键</th>' 
    PRINT '<th>外键</th>' 
    PRINT '<th>类型</th>' 
    PRINT '<th>长度</th>' 
    PRINT '<th>数值精度</th>' 
    PRINT '<th>小数位数</th>' 
    PRINT '<th>允许为空</th>' 
    PRINT '<th>计算列</th>' 
    PRINT '<th>标识列</th>' 
    PRINT '<th>默认值</th>' 
    --Get the Table Data 
    SELECT '</tr><tr>', 
    '<td>' + CAST(clmns.name AS VARCHAR(35)) + '</td>', 
    '<td>' + isnull(cast(exprop.value as varchar(500)),'') + '</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) --I don't wand to include comments on indexes 
    ORDER BY clmns.column_id ASC 
    PRINT '</tr></table>' 
    PRINT '</div>' 
    FETCH NEXT FROM Tbls 
    INTO @TableName 
END 
PRINT '</body></HTML>' 
CLOSE Tbls 
DEALLOCATE Tbls 

这里,我要感谢http://blog.csdn.net/qq289523052/article/details/22174721

这篇博文,我也做了一点小小的改动,原博文执行下来的结果是:如果数据表中的字段没有备注的话就不会显示了。

因为我个人的需求,需要把数据表中所有的字段进行显示出来。

image

关键的地方在这,原博文多了个条件,最后说一下。

image

我们SQL Server中默认是以网状显示结果的,这里我们需要切换到文本的方式。

posted @ 2016-09-21 09:32  加号和减号  阅读(609)  评论(0编辑  收藏  举报