我想查询包含名称列的所有表的名称?
有什么想法吗?
最简单的方法是使用INFORMATION_SCHEMA.columns
. 它已经是一个包含以下信息的视图:
select schema_name, table_name
from information_schema.columns
where column_name like '%name%';
如果要删除重复项,请添加select distinct
.
请注意,这适用于单个数据库。
使用系统视图
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name;
您可以在系统视图上使用此查询来获取表的架构和名称:
select QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) from sys.tables t
inner join sys.schemas s on t.schema_id = s.schema_id
where exists (
select * from sys.columns c
where c.object_id = t.object_id and c.name = N'id'
)
SELECT c.name AS ColName
, t.name AS TableName
FROM sys.columns c INNER JOIN sys.tables t
ON c.[object_id] = t.[object_id]
WHERE c.name LIKE '%ColumnName%' --<-- Column name you are looking for