贫道2018年1月正式出道,可以说在IT界我就是个菜鸟。但我有着一颗不服输的心,我相信我会在这条路走上巅峰之道的。下面我来写我的第一份学习笔记:
介绍:大多数公司都有自己的数据文档,估计大多数都是用PowerDesigner、T4等写的吧,不知道有没有用线上的。在这我用SQL+MVC 写了个连接数据库的数据文档:
分为两个:
一、只读模式
主用sql语句:
查询数据库所有表:select name as tableName from sys.tables order by name
查询数据库表的描述:select a.name AS tname,isnull(g.[value],'-') AS destb from sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0) where a.name='表名'
查询表的字段详情说明等:
SELECT
col.name AS name ,
ISNULL(ep.[value], '') AS descript,
t.name AS datatype ,
CASE WHEN EXISTS ( SELECT 1
FROM dbo.sysindexes si
INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
AND si.indid = sik.indid
INNER JOIN dbo.syscolumns sc ON sc.id = sik.id
AND sc.colid = sik.colid
INNER JOIN dbo.sysobjects so ON so.name = si.name
AND so.xtype = 'PK'
WHERE sc.id = col.id
AND sc.colid = col.colid ) THEN '1'
ELSE ''
END AS iskey ,
CASE WHEN col.isnullable = 1 THEN '1'
ELSE ''
END AS isempty ,
ISNULL(comm.text, '') AS defult
FROM dbo.syscolumns col
LEFT JOIN dbo.systypes t ON col.xtype = t.xusertype
inner JOIN dbo.sysobjects obj ON col.id = obj.id
AND obj.xtype = 'U'
AND obj.status >= 0
LEFT JOIN dbo.syscomments comm ON col.cdefault = comm.id
LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id
AND col.colid = ep.minor_id
AND ep.name = 'MS_Description'
LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id
AND epTwo.minor_id = 0
AND epTwo.name = 'MS_Description'
WHERE obj.name = '{0}'--表名
ORDER BY col.colorder ;
二:可更改表及字段的说明详情 (有权限者拥有)
主用的sql:
新增表说明:EXECUTE sp_addextendedproperty N'MS_Description', N'描述', N'user', N'dbo', N'table', N'表名', NULL, NULL
修改表说明:EXECUTE sp_updateextendedproperty N'MS_Description', N'描述', N'user', N'dbo', N'table', N'名', NULL, NULL
新增字段说明:EXECUTE sp_addextendedproperty N'MS_Description', N'描述', N'user', N'dbo', N'table', N'表名', N'column', N'字段名'
修改字段说明:sp_updateextendedproperty N'MS_Description', N'描述', N'user', N'dbo', N'table', N'表名', N'column', N'字段名'
代码十分简单,操作也是十分的简单,只需要配置一下连接即可,如下简单配置
Of course, if you have a better and easier way to share it with me.
This is my first note, thank you !