Find all tables containing column with specified n

2018-12-31 23:09发布

Is it possible to query for table names which contain columns being

LIKE '%myName%'

?

29条回答
谁念西风独自凉
2楼-- · 2018-12-31 23:39
SELECT  [TABLE_NAME] ,
        [INFORMATION_SCHEMA].COLUMNS.COLUMN_NAME
FROM    INFORMATION_SCHEMA.COLUMNS
WHERE   INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%NAME%' ;
查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 23:39

For Oracle, with normal user permissions:

select owner, table_name, column_name
from all_tab_columns 
where column_name 
like '%myname%';
查看更多
余生无你
4楼-- · 2018-12-31 23:40
SQL Server:

SELECT Table_Name, Column_Name 
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YOUR_DATABASE'
AND   COLUMN_NAME LIKE '%YOUR_COLUMN%'

Oracle:

SELECT owner, table_name, column_name 
FROM all_tab_columns 
WHERE column_name like '%YOUR_COLUMN_NAME%'
AND OWNER in ('YOUR_SCHEMA_NAME');
  • SIMPLE AS THAT!! (SQL, PL/SQL) I use it ALL the time to find ALL instances of a column name in a given database (schema).
查看更多
与风俱净
5楼-- · 2018-12-31 23:44

Hopefully this isn't a duplicate answer, but what I like to do is generate a sql statement within a sql statement that will allow me to search for the values I am looking for (not just the tables with those field names ( as it's usually necessary for me to then delete any info related to the id of the column name I am looking for):

  SELECT  'Select * from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRun
  FROM sys.columns c, c.name as ColName, t.name as TableName
  JOIN sys.tables t 
     ON c.object_id = t.object_id
  WHERE c.name LIKE '%ProjectID%'

Then I can copy and paste run my 1st column "SQLToRun"... then I replace the "Select * from ' with 'Delete from ' and it allows me to delete any references to that given ID! Write these results to file so you have them just in case.

NOTE**** Make sure you eliminate any bakup tables prior to running your your delete statement...

  SELECT  'Delete from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRun
  FROM sys.columns c, c.name as ColName, t.name as TableName
  JOIN sys.tables t 
     ON c.object_id = t.object_id
  WHERE c.name LIKE '%ProjectID%'
查看更多
明月照影归
6楼-- · 2018-12-31 23:44
SELECT col.Name AS ColumnName, tab.Name AS TableName 
FROM sys.columns col  
     JOIN sys.tables tab   
ON col.Object_id = tab.Object_id   
WHERE col.Name LIKE '%MyName%'
查看更多
登录 后发表回答