生成SQL Server数据字典
1.表信息
Select *
FROM INFORMATION_SCHEMA.COLUMNS
order by Table_name;
select * from INFORMATION_SCHEMA.tables
2.数据字典
SELECT
(case when a.colorder=1 then d.name else '' end)表名,
a.colorder 字段序号,
a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) 主键,
b.name 类型,
a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,
isnull(g.[value],'') AS 字段说明
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.major_id
order by a.id,a.colorder
3.小技巧
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 cellspacing="0">'
--Get the Description of the table
--Characters 1-250
PRINT '<tr>' --Set up the Column Headers for the Table
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 and
exprop.class = 1) --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
生成SQL Server数据字典的更多相关文章
- 由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭。
错误:由于启动用户实例的进程时出错,导致无法生成SQL Server的用户实例. 原因:添加安装SQLEXPRESS时,估计装在了不同的目录下: 解决方法:关闭Sqlserver及相关的程序,删除目录 ...
- 利用PowerDesigner绘制PDM生成SQL Server数据库
PowerDesigner是个很强大的建模工具,可以利用它绘制各种图形,本文利用该工具绘制PDM,进而生成SQL Server数据库. 比如绘制一个简单的学生选课.教师授课管理系统的PDM: pk表示 ...
- 由于检索用户的本地应用程序数据路径时出错,导致无法生成 SQL Server 的用户实例
/”应用程序中的服务器错误. 由于检索用户的本地应用程序数据路径时出错,导致无法生成 SQL Server 的用户实例.请确保该用户在此计算机上有本地用户配置文件.该连接将关闭. 堆栈跟踪: [Sql ...
- CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板
C#生成sql视图的实体类 using System;using System.Text;using CodeSmith.Engine;using SchemaExplorer;using Syste ...
- Sql Server数据字典
1:添加字段属性或者表属性 execute sys.sp_addextendedproperty @name = N'MS_Description', @value = N'要添加的属性信息', @l ...
- 使用vs2010生成SQL Server 随机数据
前几天做测试数据,偶然发现vs2010中有一个生成随机数据的功能,记录下来,方便以后使用,确实非常的好用灵活快捷. 为了简单扼要的说明,下面我用一个实例来说明如何快捷使用: 在VS2010创建数据库项 ...
- 小程序原理,生成SQL SERVER 2008 数据库所有表的结构文档
作者:wide288 , 日期:2013-7-31 以前开发中,用 MYSQL 数据库,有个小程序 生成数据库结构文档.很方便,做为开发组的文档很有用. 现在开发中用到了 SQL SERVER 200 ...
- sql server 数据字典的妙用
以下提到的一些需求,你可能会说用SQL server management studio 客户端 看一下不就可以了,何必这么麻烦 ! 是的,当1 个表的时候我们可以人工检查,但当有10 个表,100个 ...
- 生成sql server 数据库 脚本的 存储过程和调用
USE [db_datadown] GO /****** Object: StoredProcedure [dbo].[GetTBScript] Script Date: 03/05/2015 09: ...
随机推荐
- org.w3c.dom.Node.getTextContent()方法编译错误-已解决
org.w3c.dom.Node.getTextContent()方法编译错误. 在项目的Java Build Path | Order and Export选项卡中,将JRE System Libr ...
- spoj 1029 Matrix Summation
题意: 对一个矩阵有2种操作: 1.把某个元素设为x. 2.查询以(x1,y1)为左上角 以(x2,y2)为右上角的矩阵中的数字的和. 思路: 二维树状数组入门题,同时对横坐标和纵坐标做前缀和就行了. ...
- spring里的事物设置
有的人说事物在spring里设置有两种,其实事物设置在spring配置文件中共有五种方式:第一种方式:每个Bean都有一个代理第二种方式:所有Bean共享一个代理基类第三种方式:使用拦截器第四种方式: ...
- pycharm中不以pytest方式运行,以普通方式运行的方法
问题: 在调试一个很简单的代码时无法输出结果,运行结果如下形式: 出现以上情况,主要是因为在运行时是以pytest方式运行的,要换回以普通方式去运行. 问题解决: 首先是红色圈圈里的白色三角形. 然后 ...
- Python 递归计算分数数列
C语言的课后习题 求数列:2/1,3/2,5/3,8/5,13/8,21/13,...前50项的和 数列规律: 第二项的分母是[前一项分子] 第二项的分子是[前一项分子与分母的和] from frac ...
- LeetCode Weekly Contest 121
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers ...
- Thinking in work
Scheduler? Realtime? sure SCI? Power supply and ECU life. how to assure? EMC?
- XUnit测试框架-Python unittest
选择 语言选择 本次个人作业我选择的语言是Python,了解学习Python有一段时间了但是一直没有练习,所以这次玩蛇,使用的版本是Python3.6. 开发工具选择 我选择的IDE是Pycharm, ...
- Mac appStore下载软件报500错误的解决方法
Step1.打开"启动台": Step2:打开"其他": Step3:打开"终端": Step4:复制如下代码,主要不要作任何更改,不要有多 ...
- COMS3200 The RUSH protocol
Part C (50 marks)The RUSH protocol (Reliable UDP Substitute for HTTP) is a HTTP-like stop-and-wait p ...