sqlserver导出表结构到excel
程序员文章站
2024-03-21 08:41:40
...
查询某张表,首先要进入到要查询表如 :h_ContractBaseInfo表
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,
字段说明 = 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 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,
占用字节数 = 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.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='h_ContractBaseInfo' --如果只查询指定表,加上此条件
Order By
A.id,A.colorder
每个表的含义查看http://www.cnblogs.com/asdcer/archive/2007/05/14/746377.html 这个地址
对应给每个表和字段添加注释
1 – 表加注释
2 EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’注释内容’ , @level0type=N’SCHEMA’,@level0name=N’dbo’, @level1type=N’TABLE’,@level1name=N’表名’
3 --例如:
4 EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’系统设置表’ , @level0type=N’SCHEMA’,@level0name=N’dbo’, @level1type=N’TABLE’,@level1name=N’CM01_SYSTEM’
顺便也记录一下使用SQL给表的字段添加注释的方法,方法如下:
1 – 字段加注释
2 EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’注释内容’ , @level0type=N’SCHEMA’,@level0name=N’dbo’, @level1type=N’TABLE’,@level1name=N’表名’, @level2type=N’COLUMN’,@level2name=N’字段名’