SQL提取数据库表名及字段名等信息代码示例

前端之家收集整理的这篇文章主要介绍了SQL提取数据库表名及字段名等信息代码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文向大家介绍了使用sql语句提取数据库所有表的表名、字段名的实例代码,在sqlserver 中进行了测试,具体内容如下:

sql;"> --查询所有用户表所有字段的特征 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中测试

0 then '√' else '' end) as 主键,--查询主键END b.name as 类型,a.length as 占用字节数,COLUMNPROPERTY(a.id,'PRECISION' ) as 长度,isnull(COLUMNPROPERTY(a.id,'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

------提取某个表的字段名

0 then '√' else '' end) N'主键',b.name N'类型',a.length N'占用字节数','PRECISION') as N'长度','Scale'),0) as N'小数位数',(case when a.isnullable=1 then '√'else '' end) N'允许空','') 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

总结

以上就是本文关于sql提取数据库表名及字段名等信息代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:MysqL变量等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持

原文链接:https://www.f2er.com/mssql/62573.html

猜你在找的MsSQL相关文章