Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all tables / views with a column name that matches a particular pattern

Tags:

teradata

How do I find all tables or views that have a column name that matches a pattern.

The pattern is a simple LIKE %abcd% pattern and not a regex.

The query or queries should return both views and tables.

like image 325
cammil Avatar asked Sep 10 '14 13:09

cammil


People also ask

How do I match a column name in SQL?

Lets write a below SQL query to compare column name between these two tables. SELECT * into #tblA FROM information_schema. columns WHERE table_Schema ='dbo' and table_name = 'TableA' ; SELECT * into #tblB FROM information_schema. columns WHERE table_Schema ='dbo' and table_name = 'TableB' ; SELECT COALESCE(A.

How do I find a particular column name in all tables in MySQL?

How to list all tables that contain a specific column name in MySQL? You want to look for tables using the name of columns in them. SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA. COLUMNS WHERE COLUMN_NAME IN('column1', 'column2') AND TABLE_SCHEMA = 'schema_name';

How do I find a particular column name in all tables in Oracle?

select table_name from all_tab_columns where column_name = 'PICK_COLUMN'; If you've got DBA privileges, you can try this command instead: select table_name from dba_tab_columns where column_name = 'PICK_COLUMN'; Now if you're like me, you may not even know what the column you're searching for is really named.


1 Answers

dbc.ColumnsV stores column information:

SELECT DatabaseName, TableName, ColumnName
FROM dbc.ColumnsV
WHERE ColumnName LIKE '%abcd%'
;

This might also return Stored Prodedures or Macros, so you might better join to dbc.TablesV:

SELECT t.DatabaseName, t.TableName, t.TableKind, ColumnName
FROM dbc.TablesV AS t JOIN dbc.ColumnsV AS c
  ON t.DatabaseName = c.DatabaseName
 AND t.TableName = c.TableName 
WHERE ColumnName LIKE '%abcd%'
AND TableKind in ('T','V') 
;
like image 169
dnoeth Avatar answered Jan 03 '23 19:01

dnoeth