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:25

Here's a working solution for a Sybase database

select 
  t.table_name, 
  c.column_name 
from 
  systab as t key join systabcol as c 
where 
   c.column_name = 'MyColumnName'
查看更多
刘海飞了
3楼-- · 2018-12-31 23:26

I don't know why so many of you suggesting Joining with sys.table with sys.columns you can simply use below code:

Select object_name(object_id) as TableName,* from SYS.columns where name LIKE '%MyName%'

or

If you want schema name as well:

Select * from  INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME LIKE '%MyName%'
查看更多
初与友歌
4楼-- · 2018-12-31 23:28
USE AdventureWorks

GO

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

It is from Pinal Sir Blog

查看更多
谁念西风独自凉
5楼-- · 2018-12-31 23:29

Following query will give you the exact table names of the database having field name like '%myName'.

SELECT distinct(TABLE_NAME)
  FROM INFORMATION_SCHEMA.COLUMNS    
 WHERE COLUMN_NAME LIKE '%myName%'
查看更多
荒废的爱情
6楼-- · 2018-12-31 23:29

I used this for the same purpose and it worked:

  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_CATALOG= 'theDatabase'
  and COLUMN_NAME like 'theCol%'
查看更多
荒废的爱情
7楼-- · 2018-12-31 23:29

You can use [INFORMATION_SCHEMA].[COLUMNS] table to find columns Such as

Select * From [INFORMATION_SCHEMA].[COLUMNS] Where COLUMN_NAME like '%Column%'

For getting Table and Column information for given SQL statement visit http://www.w3hattrick.com/2016/05/getting-table-and-column-information.html

查看更多
登录 后发表回答