I want to show all tables that have specified column name

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

How can I get a list of all the tables that have a specific column name?

回答1:

Pretty simple on a per database level

Use DatabaseName Select * From INFORMATION_SCHEMA.COLUMNS Where column_name = 'ColName' 


回答2:

select table_name from information_schema.columns where COLUMN_NAME = 'MyColumn' 


回答3:

You can use the information schema views:

SELECT DISTINCT TABLE_SCHEMA, TABLE_NAME FROM Information_Schema.Columns WHERE COLUMN_NAME = 'ID' 

Here's the MSDN reference for the "Columns" view: http://msdn.microsoft.com/en-us/library/ms188348.aspx



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

In depth article by SQL Authority



回答5:

If you're trying to query an Oracle database, you might want to use

select owner, table_name  from all_tab_columns where column_name = 'ColName'; 


回答6:

SELECT      T.TABLE_NAME, C.COLUMN_NAME FROM        INFORMATION_SCHEMA.COLUMNS C             INNER JOIN INFORMATION_SCHEMA.TABLES T ON T.TABLE_NAME = C.TABLE_NAME WHERE       TABLE_TYPE = 'BASE TABLE'             AND COLUMN_NAME = 'ColName' 

This returns tables only and ignores views for anyone who is interested!



回答7:

--get tables that contains selected columnName

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

its worked...



回答8:

You can find what you're looking for in the information schema: SQL Server 2005 System Tables and Views I think you need SQL Server 2005 or higher to use the approach described in this article, but a similar method can be used for earlier versions.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!