当前位置: 代码迷 >> Sql Server >> 怎么在整个数据库里查询出包含特定字段名的表名
  详细解决方案

怎么在整个数据库里查询出包含特定字段名的表名

热度:100   发布时间:2016-04-24 20:32:54.0
求助:如何在整个数据库里查询出包含特定字段名的表名?
现有MSSQL数据库名为“test”,该库有数百个表,需要实现的目的为:在该数据的所有表中查询出字段名“成绩”的表名,请问该如何写,谢谢。

------解决方案--------------------

select a.name as 表名
from sysobjects a
left join syscolumns b on a.id=b.id
where a.xtype='U'
EXCEPT
select a.name as 表名
from sysobjects a
left join syscolumns b on a.id=b.id
where a.xtype='U' and b.name like '%成绩%'

------解决方案--------------------
select a.name as 表名
from sysobjects a
where a.xtype='U' and not exists(select 1 from syscolumns b where a.id=b.id and b.name<>'成绩')
  相关解决方案