開發一個項目時都會有一個蛋疼的問題——寫資料庫需求文檔,然後根據這個文檔來建資料庫,如果後來需求改了,要改資料庫還要改文檔,有時忙著忙著就忘改了,導致文檔是過期的。那麼我們自己寫個腳本在資料庫運行直接生產數據字典,這樣只要改資料庫就行了。目前在網上搜了下,發現sqlServer只有2005的生成工具 ...
開發一個項目時都會有一個蛋疼的問題——寫資料庫需求文檔,然後根據這個文檔來建資料庫,如果後來需求改了,要改資料庫還要改文檔,有時忙著忙著就忘改了,導致文檔是過期的。那麼我們自己寫個腳本在資料庫運行直接生產數據字典,這樣只要改資料庫就行了。目前在網上搜了下,發現sqlServer只有2005的生成工具,沒有08的,存儲過程倒是有,不過下載運行一遍到處是坑,寫的也太差了,於是對腳本進行改進。
-- ============================================= -- Author: <marlon> -- Create date: <2017-11-13> -- Description: <生成資料庫字典> -- ============================================= BEGIN DECLARE @TableName nvarchar(35),@htmls varchar(8000) DECLARE @欄位名稱 VARCHAR(200) DECLARE @類型 VARCHAR(200) DECLARE @長度 VARCHAR(200) DECLARE @數值精度 VARCHAR(200) DECLARE @小數位數 VARCHAR(200) DECLARE @預設值 VARCHAR(200) DECLARE @允許為空 VARCHAR(200) DECLARE @外鍵 VARCHAR(200) DECLARE @主鍵 VARCHAR(200) DECLARE @描述 VARCHAR(200) SET NOCOUNT ON; DECLARE Tbls CURSOR FOR Select distinct Table_name FROM INFORMATION_SCHEMA.COLUMNS 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>SqlServer數據字典</title>' PRINT ' <style type="text/css">' PRINT ' body{margin:0; font:11pt "arial", "微軟雅黑"; cursor:default;}' PRINT ' .titleHead{margin:0 auto;text-align:center;}' 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 ' .tableBox td {text-align:center}' PRINT ' </style>' PRINT ' </head>' PRINT ' <body>' PRINT ' <div class="titleHead">' PRINT ' <h1>'+DB_NAME()+'數據字典</h1>' PRINT ' </div>' FETCH NEXT FROM Tbls INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN set @htmls = '' Select @htmls = ' <h3>' + @TableName + ' : '+ CAST(Value as varchar(1000)) + '</h3>' FROM sys.extended_properties AS A WHERE A.major_id = OBJECT_ID(@TableName) and minor_id = 0 if @htmls is null or DATALENGTH (@htmls)=0 begin set @htmls = ' <h3>' + @TableName + '</h3>' end PRINT ' <div class="tableBox">' PRINT @htmls PRINT ' <table cellspacing="0">' 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 ' </tr>' DECLARE TRows CURSOR FOR SELECT ' <td>' + CAST(clmns.name AS VARCHAR(35)) + '</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>' + isnull(CAST(cnstr.definition AS VARCHAR(20)),'') + '</td>', ' <td>' + (case when clmns.is_nullable > 0 then '是' else '否' end) + '</td>' , ' <td>' + (case when clmns.is_computed > 0 then '是' else '否' end) + '</td>' , ' <td>' + (case when clmns.is_identity > 0 then '是' else '否' end) + '</td>' , ' <td>' + ISNULL(CAST(exprop.value AS VARCHAR(500)),'') + '</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) ORDER BY clmns.column_id ASC OPEN TRows FETCH NEXT FROM TRows INTO @欄位名稱,@類型,@長度,@數值精度,@小數位數,@預設值,@允許為空,@外鍵,@主鍵,@描述 WHILE @@FETCH_STATUS = 0 BEGIN PRINT ' <tr>' PRINT @欄位名稱 PRINT @類型 PRINT @長度 PRINT @數值精度 PRINT @小數位數 PRINT @預設值 PRINT @允許為空 PRINT @外鍵 PRINT @主鍵 PRINT @描述 PRINT ' </tr>' FETCH NEXT FROM TRows INTO @欄位名稱,@類型,@長度,@數值精度,@小數位數,@預設值,@允許為空,@外鍵,@主鍵,@描述 END CLOSE TRows DEALLOCATE TRows PRINT ' </table>' PRINT ' </div>' FETCH NEXT FROM Tbls INTO @TableName END PRINT ' </body>' PRINT '</html>' CLOSE Tbls DEALLOCATE Tbls END
註意事項:
1、以上腳本直接在SQL SERVER運行,以文本格式顯示,設置如下:
紅框的不要勾選
2、如果要加註釋,表的註釋在這裡加
欄位的註釋:
3、生成後保存為HTML文件即可
效果展示: