查找具有X列名称的所有表

时间:2014-05-28 16:00:16

标签: sql sql-server

有没有办法在Y数据库中找到所有具有X列名称的表?

所以

如果Y.Database中存在X.Column 使用x.column打印all.tables

由于

6 个答案:

答案 0 :(得分:2)

大多数(但不是全部)数据库都支持information_schema表。如果是这样,你可以这样做:

select table_name
from information_schema.columns t
where column_name = YOURCOLUMNNAME;

如果您的数据库不支持information_schema视图,那么任何合理的数据库都有另一种获取此信息的方法。

您可能需要指定数据库名称,但这取决于数据库。它可能是:

select table_name
from YOURDATABASENAME.information_schema.columns t
where column_name = YOURCOLUMNNAME;

select table_name
from YOURDATABASENAME.information_schema.columns t
where column_name = YOURCOLUMNNAME and schema_name = YOURDATABASENAME;

答案 1 :(得分:2)

对于sql server,试试这个......

SELECT t.name 
FROM sys.tables t INNER JOIN sys.columns c
ON t.[object_id] = c.[object_id]
WHERE c.name LIKE '%ColumnName%' 

答案 2 :(得分:2)

在Postgresql 中,它只是:

SELECT table_name 
FROM information_schema.columns 
WHERE column_name = 'some_column_name';"

在Mysql中

SELECT DISTINCT table_name 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name='column'
AND table_schema='database_name';

在MSSQL中

SELECT columns.name AS column, 
t.name AS table
FROM sys.columns columns
JOIN sys.tables t ON columns.object_id = table.object_id
WHERE columns.name = 'column_name';

答案 3 :(得分:0)

对于SQL Server:

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

答案 4 :(得分:0)

SELECT c.name  AS 'ColumnName' ,t.name AS 'TableName'
FROM sys.columns c
JOIN sys.tables  t   ON c.object_id = t.object_id
WHERE c.name LIKE 'ColoumnName%'

答案 5 :(得分:0)

在您的数据库中打开一个编辑器并执行此脚本。记住将“last_name”更改为您想要的列名。

> select t.table_schema,
>        t.table_name from information_schema.tables t inner join information_schema.columns c on c.table_name = t.table_name 
>                                 and c.table_schema = t.table_schema where c.column_name = 'last_name'
>       and t.table_schema not in ('information_schema', 'pg_catalog')
>       and t.table_type = 'BASE TABLE' order by t.table_schema;

感谢 https://dataedo.com/kb/query/postgresql/find-tables-with-specific-column-name