代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MSSQL
】
自动生成数据库字典(sql2008)
作者:
carbe
/ 发布于
2015/7/22
/
990
每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Carbe> -- Create date: <2014-09-19> -- Description: <生成数据库字典> -- ============================================= CREATE PROCEDURE [dbo].[CreateDatabaseDictionarie] AS 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>KC管理系统-数据库字典</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 Select @htmls = ' <h3>' + @TableName + ' : '+ CAST(Value as varchar(1000)) + '</h3>' FROM sys.extended_properties AS A WHERE A.major_id = OBJECT_ID(@TableName) and name = 'MS_Description' and minor_id = 0 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>' + 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(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 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 ' <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 当然这些通过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; } }
试试其它关键字
数据库字典
同语言下
.
SQL查询 多列合并成一行用逗号隔开
.
查看存储过程修改时间,最近执行时间
.
设置手动批量删除数据库相关进程
.
获取某个表中特定字段的所有字符串形式
.
SQL 如何去除重复的字符串
.
怎么去掉一个字段中的重复数据
.
String 去除空格 回车 换行 水平制表符
.
SQL查询和替换含有回车,空格,TAB
.
SQL SERVER 查询每日新增用户数量、次留数量
.
判断两个字符串是否存在相同的内容
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
carbe
贡献的其它代码
(
2
)
.
Android万能使用WebServices
.
自动生成数据库字典(sql2008)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3