sql批量生成showdocs数据字典的md文档
-- sql批量生成showdocs数据字典的md文档
--批量获取表备注
-- 表名,替换成需要的表名
DECLARE @TableList TABLE (ID INT IDENTITY(1,1), TableName NVARCHAR(256))
INSERT INTO @TableList(TableName)VALUES
('CompanyCertifyDatas'),
('CompanyXCXRegistrationDatas'),
('CompanyXCXRegistrationTracks'),
('CompanyXCXRegistrationWeixinLogs'),
('WeixinOpenReturnCodes')
CREATE TABLE #FieldDescripts
(
FieldName NVARCHAR(MAX),
PrimaryKey NVARCHAR(MAX),
DataType NVARCHAR(MAX),
FieldLength NVARCHAR(MAX),
NeedNull NVARCHAR(MAX),
DefaultValue NVARCHAR(MAX),
Description NVARCHAR(MAX)
)
DECLARE @Count INT=(SELECT COUNT(1) FROM @TableList)
DECLARE @ID INT=1
WHILE @ID<=@Count
BEGIN
DECLARE @TableName NVARCHAR(MAX)=(SELECT TableName FROM @TableList WHERE ID=@ID)
DECLARE @TableDescription NVARCHAR(MAX)
SELECT TOP(1) @TableDescription=ISNULL(CONVERT(NVARCHAR(MAX),g.[value]),'')
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=@TableName
INSERT INTO #FieldDescripts VALUES('# '+@TableName,'','','','','','')
INSERT INTO #FieldDescripts VALUES('- ' + @TableDescription,'','','','','','')
INSERT INTO #FieldDescripts VALUES('','','','','','','')
INSERT INTO #FieldDescripts VALUES('|字段名','|主键','| 类型','| 长度','| 允许空','| 默认值','| 字段说明|')
INSERT INTO #FieldDescripts VALUES('|:-------','|:-------','|:-------','|:-------','|:-------','|:-------','|:-------|')
INSERT INTO #FieldDescripts
SELECT
字段名 = '|' + a.name ,
主键 = '|' + case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id 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,
长度 = '|' + CONVERT(NVARCHAR(MAX),COLUMNPROPERTY(a.id,a.name,'PRECISION')),
允许空 = '|' + case when a.isnullable=1 then '√' else '×' end,
默认值 = '|' + ISNULL(e.text,''),
字段说明 = '|' + CONVERT(NVARCHAR(MAX),ISNULL(g.[value],'')) + '|'
FROM
syscolumns a left join
systypes b on a.xusertype=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
ORDER BY a.id,a.colorder
INSERT INTO #FieldDescripts VALUES('','','','','','','')
INSERT INTO #FieldDescripts VALUES('','','','','','','')
SET @ID=@ID+1
END
SELECT * FROM #FieldDescripts
DROP TABLE #FieldDescripts