项目要做到可持续发展,需要有个好的文档体系,把数据库文档维护在数据库中是最合适不过的了。。。
这里提供2个存储过程,方便快捷查阅
-- 查看功能模块对应的表
-- 用法:ft Product
-- 执行后会输出所有含有Product的表名与表描述
CREATE PROCEDURE [dbo].[ft]
(
@tableName AS VARCHAR(100) = ''
)
AS
BEGIN
IF @tableName = ''
BEGIN
SELECT 表名 = d.name ,
表说明 = ISNULL(f.value, '')
FROM syscolumns a
INNER JOIN sysobjects d ON a.id = d.id
AND d.xtype = 'U'
AND d.name <> 'dtproperties'
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
WHERE a.colorder = 1
ORDER BY d.name;
END;
ELSE IF LEFT(@tableName,2) <> 't_'
BEGIN
SELECT 表名 = d.name ,
表说明 = ISNULL(f.value, '')
FROM syscolumns a
INNER JOIN sysobjects d ON a.id = d.id
AND d.xtype = 'U'
AND d.name <> 'dtproperties'
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
WHERE a.colorder = 1 AND d.name LIKE '%'+@tableName+'%'
ORDER BY d.name;
END;
ELSE
BEGIN
SELECT 表名 = CASE WHEN a.colorder = 1 THEN d.name
ELSE ''
END ,
表说明 = CASE WHEN a.colorder = 1 THEN ISNULL(f.value, '')
ELSE ''
END ,
字段序号 = a.colorder ,
字段名 = a.name ,
标识 = 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, '') ,
字段说明 = ISNULL(g.[value], '')
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.minor_id
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
WHERE d.name = @tableName
OR @tableName = ''
ORDER BY a.id ,
a.colorder;
END;
END;
GO
-- 查看表对应的字段
-- 用法:tb t_Product_Detail
-- 执行后会输出 t_Product_Detail 表下的字段属性详情和描述
CREATE PROCEDURE [dbo].[tb]
(
@tableName AS VARCHAR(100) = ''
)
AS
BEGIN
IF @tableName = ''
BEGIN
SELECT 表名 = d.name ,
表说明 = ISNULL(f.value, '')
FROM syscolumns a
INNER JOIN sysobjects d ON a.id = d.id
AND d.xtype = 'U'
AND d.name <> 'dtproperties'
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
WHERE a.colorder = 1
ORDER BY d.name;
END;
ELSE
BEGIN
SELECT 表名 = CASE WHEN a.colorder = 1 THEN d.name
ELSE ''
END ,
表说明 = CASE WHEN a.colorder = 1 THEN ISNULL(f.value, '')
ELSE ''
END ,
字段序号 = a.colorder ,
字段名 = a.name ,
标识 = 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, '') ,
字段说明 = ISNULL(g.[value], '')
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.minor_id
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
WHERE d.name = @tableName
OR @tableName = ''
ORDER BY a.id ,
a.colorder;
END;
END;
GO