SQL提取数据库表名及字段名等信息代码示例
作者:这都不是事 时间:2024-01-26 23:18:54
本文向大家介绍了使用SQL语句提取数据库所有表的表名、字段名的实例代码,在SQLserver 中进行了测试,具体内容如下:
--查询所有用户表所有字段的特征
SELECT D.Name as TableName, A.colorder AS ColOrder, A.name AS Name,
COLUMNPROPERTY(A.ID,A.Name, 'IsIdentity') AS IsIdentity,
CASE WHEN EXISTS
(SELECT 1
FROM dbo.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 1 ELSE 0 END AS 主键,
B.name AS 类型,
A.length AS [长度],
A.xprec AS [精度],
A.xscale AS [小数],
CASE WHEN A.isnullable = 1 THEN 1 ELSE 0 END AS [可为空],
ISNULL(E.text, ' ') AS [默认值],
ISNULL(G.[value], ' ') AS [说明]
FROM dbo.syscolumns A LEFT OUTER JOIN
dbo.systypes B ON A.xtype = B.xusertype INNER JOIN
dbo.sysobjects D ON A.id = D.id AND D.xtype = 'U' AND
D.name <> 'dtproperties' LEFT OUTER JOIN
dbo.syscomments E ON A.cdefault = E.id
LEFT OUTER JOIN dbo.sysproperties G ON A.id = G.id AND A.colid = G.smallid
--WHERE D.Name='tablename' --如果找指定表,把注释去掉
ORDER BY 1, 2
------在SQLserver 2005中测试
--查询所有用户表所有字段的特征
SELECT
(case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空
a.colorder as 字段序号,
a.name as 字段名,
(case when COLUMNPROPERTY( a.id,a.name, 'IsIdentity' )=1 then '√' else '' end) as 标识,
(case when (SELECT count(*) FROM sysobjects--查询主键
WHERE (name in
(SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns
WHERE (id = a.id) AND (name = a.name))
)))))
AND (xtype = 'PK' ))>0 then '√' else '' end) as 主键,--查询主键END
b.name as 类型,
a.length as 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION' ) as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale' ),0) as 小数位数,
(case when a.isnullable=1 then '√' else '' end) as 允许空,
isnull(e.text,'' ) as 默认值,
isnull(g.[value],'' ) AS 字段说明
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
--where d.name='PE_U_ValliMessage' --所要查询的表
order by a.id,a.colorder
------提取某个表的字段名
SELECT
(case when a.colorder=1 then d.name else '' end) N'表名',
a.colorder N'字段序号',
a.name N'字段名',
isnull(g.[value],'') AS N'字段说明',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else ''
end) N'标识',
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) N'主键',
b.name N'类型',
a.length N'占用字节数',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
(case when a.isnullable=1 then '√'else '' end) N'允许空',
isnull(e.text,'') N'默认值'
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
--where条件输入表名
where d.name='ConsumeBillBody'
order by object_name(a.id),a.colorder
来源:http://www.cnblogs.com/H2921306656/p/6929605.html
标签:sql,字段名
0
投稿
猜你喜欢
distinct 多列问题结合group by的解决方法
2024-01-21 11:18:23
在PYQT5中QscrollArea(滚动条)的使用方法
2023-03-07 06:29:51
以SortedList为例详解Python的defaultdict对象使用自定义类型的方法
2022-04-07 02:32:28
高考考python编程是真的吗
2023-11-13 04:48:47
python中翻译功能translate模块实现方法
2021-05-09 17:02:33
python pyheatmap包绘制热力图
2021-02-18 21:35:46
Deepin20安装开发环境的超详细教程
2023-12-16 20:05:41
Python3.5实现的罗马数字转换成整数功能示例
2023-02-18 21:17:32
Ubuntu Server 16.04下mysql8.0安装配置图文教程
2024-01-21 21:54:37
SQL Server查询某个字段在哪些表中存在
2024-01-27 08:02:18
深入string理解Golang是怎样实现的
2024-02-07 06:45:24
Linux mysql安装修改root密码服务
2010-10-14 13:59:00
MySQL中的用户创建与权限管理
2024-01-17 00:49:06
Python WEB应用部署的实现方法
2022-02-18 07:34:35
Oracle19c 创建表空间遇到的坑
2024-01-15 14:18:24
Python中的super用法详解
2022-06-29 16:43:04
Python条件语句与循环语句
2023-01-03 05:07:21
SQL语句实现删除ACCESS重复记录的两种方法
2024-01-24 21:43:25
python高手之路python处理excel文件(方法汇总)
2021-02-17 10:33:15
MYSQL GROUP BY用法详解
2024-01-29 10:22:05