博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动生成数据库字典(sql2008)
阅读量:5304 次
发布时间:2019-06-14

本文共 5531 字,大约阅读时间需要 18 分钟。

每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码。

存储过程:

SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author:        
-- Create date: <2014-09-19>-- Description:
<生成数据库字典>
-- =============================================CREATE PROCEDURE [dbo].[CreateDatabaseDictionarie]ASBEGIN 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 '' PRINT '' PRINT ' ' PRINT '
KC管理系统-数据库字典' PRINT '
' PRINT ' ' PRINT ' ' FETCH NEXT FROM Tbls INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN Select @htmls = '

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

' FROM sys.extended_properties AS A WHERE A.major_id = OBJECT_ID(@TableName) and name = 'MS_Description' and minor_id = 0 PRINT '
' PRINT @htmls PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' PRINT '
' DECLARE TRows CURSOR FOR SELECT '
', '
' , '
', '
', '
', '
', '
' , '
' , '
' , '
' 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 OPEN TRows FETCH NEXT FROM TRows INTO @字段名称,@类型,@长度,@数值精度,@小数位数,@默认值,@允许为空,@外键,@主键,@描述 WHILE @@FETCH_STATUS = 0 BEGIN PRINT '
' PRINT @字段名称 PRINT @类型 PRINT @长度 PRINT @数值精度 PRINT @小数位数 PRINT @默认值 PRINT @允许为空 PRINT @外键 PRINT @主键 PRINT @描述 PRINT '
' FETCH NEXT FROM TRows INTO @字段名称,@类型,@长度,@数值精度,@小数位数,@默认值,@允许为空,@外键,@主键,@描述 END CLOSE TRows DEALLOCATE TRows PRINT '
字段名称 类型 长度 数值精度 小数位数 默认值 允许为空 外键 主键 描述
' + CAST(clmns.name AS VARCHAR(35)) + ' ' + 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)) + ' ' + isnull(CAST(cnstr.definition 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(exprop.value AS VARCHAR(500)),'') + '
' PRINT '
' FETCH NEXT FROM Tbls INTO @TableName END PRINT ' ' PRINT '' CLOSE Tbls DEALLOCATE TblsEND

当然这些通过PRING出来的代码使用传统的方式是调用不到的,通过查找资料,终于在国外一个XXX网站找到了解决方案。

private static string message = "";        public static string ExecuteNonQuery(string connextionString, CommandType commandType, string commandText, bool outputMsg)        {            if (connextionString == null || connextionString.Length == 0) throw new ArgumentNullException("connectionString");            // Create & open a SqlConnection, and dispose of it after we are done            using (SqlConnection connection = new SqlConnection(connextionString))            {                message = "";                connection.Open();                connection.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)                {                    message += "\n" + e.Message;                };                // Call the overload that takes a connection in place of the connection string                if (connection == null) throw new ArgumentNullException("connection");                // Create a command and prepare it for execution                SqlCommand cmd = new SqlCommand(commandText, connection); ;                cmd.CommandType = commandType;                // Finally, execute the command                int retval = cmd.ExecuteNonQuery();                // Detach the SqlParameters from the command object, so they can be used again                cmd.Parameters.Clear();                connection.Close();                return message;            }        }

调用就不用写了嘛。一切就这么简单,生成的是一份标准的htm代码,可直接放到HTML里面,当然也可以直接从数据库读取出来显示。

转载于:https://www.cnblogs.com/carbe/p/3981563.html

你可能感兴趣的文章
Oracle事务
查看>>
String类中的equals方法总结(转载)
查看>>
属性动画
查看>>
标识符
查看>>
给大家分享一张CSS选择器优选级图谱 !
查看>>
Win7中不能调试windows service
查看>>
通过httplib2 探索的学习的最佳方式
查看>>
快来熟练使用 Mac 编程
查看>>
Node.js 入门:Express + Mongoose 基础使用
查看>>
一步步教你轻松学奇异值分解SVD降维算法
查看>>
使用pager进行分页
查看>>
UVA - 1592 Database
查看>>
Fine Uploader文件上传组件
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
consonant combination
查看>>
驱动的本质
查看>>
Swift的高级分享 - Swift中的逻辑控制器
查看>>
Swagger简单介绍
查看>>