SQLserver中获取表名及字段名的SQL语句是什么
Admin 2022-06-29 群英技术资讯 365 次浏览
本文向大家介绍了使用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
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
SQL FIRST() 函数返回指定的列中第一个记录的值。_下文的讲解详细,步骤过程清晰,对大家进一步学习和理解相关知识有一定的帮助。有这方面学习需要的朋友就继续往下看吧!
存储过程功能的优点为什么要使用存储过程?以下是存储过程技术的几大主要优点:预编译执行程序。SQL Server只需要对每一个存储过程进行一次编译,然后就可以重复使用执行计划。这个特点通过重复调用存储程序极大地提高了程序的性能。缩短客户端/服
这篇文章主要介绍了数据库SQL中having和where的用法区别的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
SQL 子查询(Sub Query)或者说内查询(Inner Query),也可以称作嵌套查询(Nested Query),是一种嵌套在其他 SQL 查询的 WHERE 子句中的查询。子查询用于为主查询返回其所需数据,或者对检索数据进行进一步的限制。子查询可以在 SELECT、INSERT、UPDATE 和 DELETE 语句中,同 =、<、>、>=、<=、IN、BETWEEN 等运算符一起使用。
SQL Server怎样做分离与附加数据库的操作,我们知道SQL Server是有提供“分离/附加”数据库的操作的,但对新手来说,可能不是很了解数据库分离与附加的方法,对此下文小编就给大家分享了SQL Server数据库分离与附加的图文教程,感兴趣的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008