从数据库中的多个表中查找特定的列(字段)名称

时间:2012-09-04 09:49:31

标签: sql sql-server sql-server-2008

我的数据库中有大量的表。我正在搜索名为Country的列,但不知道哪个表包含该列。是否有特定的查询可以帮助我找到包含此列的表的名称?

5 个答案:

答案 0 :(得分:6)

是的,您可以使用INFORMATION_SCHEMA.COLUMNS

SELECT DISTINCT 
   TABLE_NAME 
FROM 
   INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Country'

答案 1 :(得分:4)

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 '%ColumnName%'
ORDER BY schema_name, table_name;

ColumnName替换为您的实际列名

答案 2 :(得分:2)

select distinct table_schema, table_name from information_schema.columns where table_name = 'Country';

答案 3 :(得分:1)

您可以在INFORMATION_SCHEMA.COLUMNS

中找到此类信息
USE YourDBName;
SELECT DISTINCT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Country';

答案 4 :(得分:0)

如果我们想在多个表中找到具有like运算符的列名(例如:3个表),那么请查看以下查询:

select COLUMN_NAME, TABLE_NAME 
from ALL_TAB_COLUMNS 
where TABLE_NAME in ('SIL_RLS_UPLOAD_TMP','SIL_CUSTOMER_MST','SIL_PERSONAL_CUSTOMER_MST')
      and upper(column_name) like upper('%loan%');

如果我们想在所有表中找到带有like运算符的列名,请转到下面的查询:

select * 
from all_tab_columns 
where upper(column_name) like upper('%card%');