您的位置:首页技术文章

SQLServer导出数据库字典的详细图文教程

【字号: 日期:2023-02-22 14:52:30浏览:7作者:猪猪

SQL

直接修改带注释那行的表名称即可。

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="girlSix" --如果只查询指定表,加上此条件  
order by a.id,a.colorder

测试sql

CREATE TABLE [dbo].[girlSix] (
[id] varchar(32) NOT NULL DEFAULT (replace(newid(),"-","")) ,
[createDate] datetime NOT NULL DEFAULT (getdate()) ,
[nickName] varchar(30) NOT NULL ,
[introduce] nvarchar(200) NOT NULL 
)
 
 
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty("MS_Description", 
"SCHEMA", N"dbo", 
"TABLE", N"girlSix", 
"COLUMN", N"nickName")) > 0) 
EXEC sp_updateextendedproperty @name = N"MS_Description", @value = N"名字"
, @level0type = "SCHEMA", @level0name = N"dbo"
, @level1type = "TABLE", @level1name = N"girlSix"
, @level2type = "COLUMN", @level2name = N"nickName"
ELSE
EXEC sp_addextendedproperty @name = N"MS_Description", @value = N"名字"
, @level0type = "SCHEMA", @level0name = N"dbo"
, @level1type = "TABLE", @level1name = N"girlSix"
, @level2type = "COLUMN", @level2name = N"nickName"
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty("MS_Description", 
"SCHEMA", N"dbo", 
"TABLE", N"girlSix", 
"COLUMN", N"introduce")) > 0) 
EXEC sp_updateextendedproperty @name = N"MS_Description", @value = N"介绍"
, @level0type = "SCHEMA", @level0name = N"dbo"
, @level1type = "TABLE", @level1name = N"girlSix"
, @level2type = "COLUMN", @level2name = N"introduce"
ELSE
EXEC sp_addextendedproperty @name = N"MS_Description", @value = N"介绍"
, @level0type = "SCHEMA", @level0name = N"dbo"
, @level1type = "TABLE", @level1name = N"girlSix"
, @level2type = "COLUMN", @level2name = N"introduce"
GO
 
-- ----------------------------
-- Records of girlSix
-- ----------------------------
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"04e3d962adcb4a5b8fefaf8b46995e85", N"2020-05-27 09:05:52.000", N"董新颖", N"郭老师关门弟子之一。");
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"568fc305930347d3bec1ddd08c71ad29", N"2020-05-27 09:01:09.000", N"王笑涵", N"北方有佳人,绝世而独立。");
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"972ec358089042e0bf24fd9efca47bde", N"2020-05-27 08:59:49.000", N"牛龙珠", N"笑若桃花三月开,清风徐徐醉颜来。");
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"BDFFC6A36A53408281EB8CA242C0E7A3", N"2020-05-27 08:42:31.000", N"闫春娜", N"珠缨旋转星宿摇,花蔓抖擞龙蛇动。");
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"efb0ca854dac456b9d8c42d4c4b1bce0", N"2020-05-27 09:03:30.000", N"刘梓佳", N"明眸善睐,辅靥承权,瑰姿艳逸,怡静体闲,端的是好一个花王,富贵的牡丹。");
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N"f839343b980e45caafaa9d2c9797294b", N"2020-05-27 09:04:53.000", N"魏慧娟", N"脉脉眼中波,盈盈花盛处。");
GO
 
-- ----------------------------
-- Indexes structure for table girlSix
-- ----------------------------
 
-- ----------------------------
-- Primary Key structure for table [dbo].[girlSix]
-- ----------------------------
ALTER TABLE [dbo].[girlSix] ADD PRIMARY KEY ([id])
GO

执行效果

导出结果:

点击导出向导后会出现以下提示,我们选择【Excel文件(2007或以上版本)(*.xlsx)】格式进行导出,单选选择后点击下一步。

这里的样式提示只有鼠标放在上面的时候才会提示,这个是导出文件的放置位置,我们直接点击【...】设置存储位置即可。

选择位置,我这里就放在桌面了,名字给一个就行,可以看到对应的保存格式是Excel2007的表格格式,我这个版本比较老的工具,可以使用最新的能导出更新的一些版本格式。

看到对应的路径输入成功,我们直接点击下一步即可。

这里勾选一下【包含列的标题】,再点击【下一步】。

点击开始

完成后点击关闭即可。

我们到桌面看看是否存在。

查看效果:

可以看到除字段说明外都是正常的,我们字段说明的时候可以采用中文,因为导出数据无论是【GBK】还是【UTF-8】都无法正常输出中文,我们就不用麻烦了,需要使用的话可以自己处理一下最后一列的字段说明。

这里我介绍一下【Chinese_PRC_CI_AS】,因为我们创建数据库默认的数据库编码格式就是这个。

指UNICODE字符集,Chinese_PRC_指针对中国简体字UNICODE的排序规则。

虽然是中文的编码格式,但是导出依然有问题。

期待以后都通用中文编码格式,免得各种格式错误。

总结

到此这篇关于SQLServer导出数据库字典的文章就介绍到这了,更多相关SQLServer导出数据库字典内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

标签: MsSQL