这部分内容和VB6的关系不大,但是确是困扰我的一个问题。
这几天在整理数据字典,以前的办法是用Excel来制作,一个一个的填写特别麻烦,制作到好说,关键就是一旦结果变更了,改来改去的麻烦死了。
后来改用数据库关系图来制作打印出来,看着还是那么回事情,但是表格的排序和查找太麻烦了,到底有没有好的办法呢?
当然有了,实际上在SqlServer中利用Sql语句就可以实现数据字典,但是就怕不知道。
代码如下:
--数据库字典生成脚本
USE DBNAME
SELECT
表名 = CASE WHEN a.colorder=1 THEN d.name ELSE '' END,
--字段序号=a.colorder,
字段名=a.name,
字段说明=ISNULL(g.[value],''),
--标识=CASE WHEN COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 THEN '√' ELSE '' END,
主键=CASE WHEN EXISTS (
SELECT 1 FROM sysobjects WHERE xtype='PK' AND name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid )))
THEN '√' ELSE '' END,
类型=b.name,
--占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=CASE WHEN a.isnullable=1 THEN '√' ELSE '' END,
默认值=ISNULL(e.text,'')
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 sysproperties g on a.id=g.id
AND a.colid=g.smallid
ORDER BY d.name,a.id,a.colorder
GO
USE DBNAME
SELECT
表名 = CASE WHEN a.colorder=1 THEN d.name ELSE '' END,
--字段序号=a.colorder,
字段名=a.name,
字段说明=ISNULL(g.[value],''),
--标识=CASE WHEN COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 THEN '√' ELSE '' END,
主键=CASE WHEN EXISTS (
SELECT 1 FROM sysobjects WHERE xtype='PK' AND name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid )))
THEN '√' ELSE '' END,
类型=b.name,
--占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=CASE WHEN a.isnullable=1 THEN '√' ELSE '' END,
默认值=ISNULL(e.text,'')
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 sysproperties g on a.id=g.id
AND a.colid=g.smallid
ORDER BY d.name,a.id,a.colorder
GO
呵呵,一切搞定。
相关资料: