查找包含具有指定名称的列的所有表

是否可以查询包含名称列的所有表的名称?

Like '%myName%' 

search表格:

 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 '%MyName%' ORDER BY TableName ,ColumnName; 

search表和视图:

 SELECT COLUMN_NAME AS 'ColumnName' ,TABLE_NAME AS 'TableName' FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%MyName%' ORDER BY TableName ,ColumnName; 

我们也可以使用下面的语法:

 select * from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME like '%clientid%' order by TABLE_NAME 

如果你更喜欢第三方工具,那里有很多select,比如:

  • ApexSQLsearch
  • SSMS工具包
  • 红门工具 。

如果您的数据库包含encryption对象(视图,过程,函数),这些非常方便,因为您无法使用系统表轻松search这些对象。

这应该工作:

 SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name like '%column_name%' ) 
 select s.[name] 'Schema', t.[name] 'Table', c.[name] 'Column', d.[name] 'Data Type', d.[max_length] 'Max Length', d.[precision] 'Precision', c.[is_identity] 'Is Id', c.[is_nullable] 'Is Nullable', c.[is_computed] 'Is Computed', d.[is_user_defined] 'Is UserDefined', t.[modify_date] 'Date Modified', t.[create_date] 'Date created' from sys.schemas s inner join sys.tables t on s.schema_id = t.schema_id inner join sys.columns c on t.object_id = c.object_id inner join sys.types d on c.user_type_id = d.user_type_id where c.name like '%ColumnName%' 

这里给你一些关于模式,表和列的额外信息,你可能会或可能不会select在where子句中使用额外的条件来进行过滤。 例如,如果您只想查看必须添加值的字段

 and c.is_nullable = 0 

您可以添加其他条件,我还以这种垂直方式在select子句中添加了列,以便根据需要轻松地重新sorting,删除,重命名或添加其他条件。 或者,您可以使用T.Name来search表格。 它非常可定制。

请享用。

我不知道为什么这么多人build议sys.table with sys.columnsjoinsys.table with sys.columns你可以简单地使用下面的代码:

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

要么

如果您还想要模式名称:

 Select * from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME LIKE '%MyName%' 

如果你只是想要表名,你可以运行:

 select object_name(object_id) from sys.columns where name like '%received_at%' 

如果你需要Schema Name(在很多情况下你会这样做,因为你会有很多不同的模式,除非你能记住数据库中的每个表以及它的属性,这可能是有用的)运行:

 select OBJECT_SCHEMA_NAME(object_id),object_name(object_id) from sys.columns where name like '%received_at%' 

最后如果你想要一个更好的格式(虽然这是代码(在我看来)变得太复杂,容易写作):

 select concat(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) from sys.columns where name like '%received_at%' 

注意你也可以创build一个基于我所拥有的function:

 CREATE PROCEDURE usp_tablecheck --Scan through all tables to identify all tables with columns that have the provided string --Stephen B @name nvarchar(200) AS SELECT CONCAT(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) AS [Table Name], name AS [Column] FROM sys.columns WHERE name LIKE CONCAT('%',@name,'%') ORDER BY [Table Name] ASC, [Column] ASC GO 

值得注意的是,concat特性是在2012年添加的。对于2008r2和更早的版本,使用+来连接string。

自从我发布这个以来,我重新格式化了一下proc。 现在有点高级了,但是看起来好多了(但是它在proc中,所以你永远不会看到它),而且格式化得更好。

这个版本允许你在一个pipe理数据库中find它,然后search任何数据库。 (注意:使用CONCAT()函数只能在2012+之前工作,除非你改变string连接以使用+运算符)。

 CREATE PROCEDURE [dbo].[usp_tablecheck] --Scan through all tables to identify all tables in the specified database with columns that have the provided string --Stephen B @name nvarchar(200) ,@db nvarchar(200) = 'master' AS DECLARE @sql nvarchar(4000) = CONCAT(' SELECT concat(OBJECT_SCHEMA_NAME(col.object_id,DB_ID(''',@db,''')),''.'',object_name(col.object_id,DB_ID(''',@db,'''))) AS [Table Name] ,col.name AS [Column] FROM ',@db,'.sys.columns col LEFT JOIN ',@db,'.sys.objects ob ON ob.object_id = col.object_id WHERE col.name LIKE CONCAT(''%'',''',@name,''',''%'') AND ob.type =''U'' ORDER BY [Table Name] ASC ,[Column] ASC') EXECUTE (@sql) GO 
 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; 

这是来自Pinal Sir Blog

 SELECT [TABLE_NAME] , [INFORMATION_SCHEMA].COLUMNS.COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%NAME%' ; 

您可以通过column_namefilter从INFORMATION_SCHEMA.COLUMNS中find它

 Select DISTINCT TABLE_NAME as TableName,COLUMN_NAME as ColumnName From INFORMATION_SCHEMA.COLUMNS Where column_name like '%myname%' 
 SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%myName%' 
 SQL Server: SELECT * 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'); 
  • 就那么简单!! (SQL,PL / SQL)我一直使用它来查找给定数据库(模式)中列名的所有实例。

以下查询将为您提供具有字段名称如'%myName'的数据库的确切表名称。

 SELECT distinct(TABLE_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%myName%' 

要获得完整的信息:列名,表名以及表的模式

 SELECT COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%col_Name%' 

我刚刚尝试过,这完美的作品

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

只需将YourDatbaseName更改为您的数据库,将YourcolumnName更改为您正在查找其余列的名称即可。

希望这有助于

 DECLARE @columnName as varchar(100) SET @columnName = 'ColumnName' SELECT t.name AS Table, c.name AS Column, ty.name AS Type, c.max_length AS Length FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id WHERE c.name LIKE @columnName ORDER BY t.name, c.name 

我用这个为相同的目的,它的工作原理:

  select * from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG= 'theDatabase' and COLUMN_NAME like 'theCol%' 

希望这不是一个重复的答案,但我喜欢做的是在sql语句内生成一个sql语句,这将允许我search我正在寻找的值(不只是与这些字段名称的表(因为它通常我需要删除任何与我正在寻找的列名相关的信息):

  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%' 

然后我可以复制和粘贴运行我的第一列“SQLToRun”…然后我用“删除”replace“select*从',它允许我删除任何给定ID的引用!将这些结果写入文件,所以你让他们以防万一。

注意****确保在运行你的删除语句之前删除任何bakup表

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

像甲骨文你可以find这样的表和列:

 select table_name, column_name from user_tab_columns where column_name like '%myname%'; 

你可以试试这个查询:

 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 '%myName%' 

对于Oracle,具有正常的用户权限:

 select owner, table_name, column_name from all_tab_columns where column_name like '%myname%'; 

只是为了改善上面的答案,我也包含了视图,并将模式和表/视图连接在一起,使结果更加明显。

 DECLARE @COLUMNNAME AS VARCHAR(100); SET @COLUMNNAME = '%Absence%'; SELECT CASE WHEN [T].[NAME] IS NULL THEN 'View' WHEN [T].[NAME] = '' THEN 'View' ELSE 'Table' END AS [TYPE], '[' + [S].[NAME] + '].' + '[' + CASE WHEN [T].[NAME] IS NULL THEN [V].[NAME] WHEN [T].[NAME] = '' THEN [V].[NAME] ELSE [T].[NAME] END + ']' AS [TABLE], [C].[NAME] AS [COLUMN] FROM [SYS].[SCHEMAS] AS [S] LEFT JOIN [SYS].[TABLES] AS [T] ON [S].SCHEMA_ID = [T].SCHEMA_ID LEFT JOIN [SYS].[VIEWS] AS [V] ON [S].SCHEMA_ID = [V].SCHEMA_ID INNER JOIN [SYS].[COLUMNS] AS [C] ON [T].OBJECT_ID = [C].OBJECT_ID OR [V].OBJECT_ID = [C].OBJECT_ID INNER JOIN [SYS].[TYPES] AS [TY] ON [C].[SYSTEM_TYPE_ID] = [TY].[SYSTEM_TYPE_ID] WHERE [C].[NAME] LIKE @COLUMNNAME GROUP BY '[' + [S].[NAME] + '].' + '[' + CASE WHEN [T].[NAME] IS NULL THEN [V].[NAME] WHEN [T].[NAME] = '' THEN [V].[NAME] ELSE [T].[NAME] END + ']', [T].[NAME], [C].[NAME], [S].[NAME] ORDER BY '[' + [S].[NAME] + '].' + '[' + CASE WHEN [T].[NAME] IS NULL THEN [V].[NAME] WHEN [T].[NAME] = '' THEN [V].[NAME] ELSE [T].[NAME] END + ']', CASE WHEN [T].[NAME] IS NULL THEN 'View' WHEN [T].[NAME] = '' THEN 'View' ELSE 'Table' END, [T].[NAME], [C].[NAME]; 
 select table_name, column_name from user_tab_columns where column_name like '%myname%'; 

有用

您可以使用[INFORMATION_SCHEMA].[COLUMNS]表来查找列如

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

要获得给定SQL语句的表和列信息,请访问http://www.w3hattrick.com/2016/05/getting-table-and-column-information.html

 Create table #yourcolumndetails( DBaseName varchar(100), TableSchema varchar(50), TableName varchar(100), ColumnName varchar(100), DataType varchar(100), CharMaxLength varchar(100)) EXEC sp_MSForEachDB @command1='USE [?]; INSERT INTO #yourcolumndetails SELECT Table_Catalog ,Table_Schema ,Table_Name ,Column_Name ,Data_Type ,Character_Maximum_Length FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like ''origin''' select * from #yourcolumndetails Drop table #yourcolumndetails 
 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%' 
 declare @ColumnName nvarchar(max)='QbId' Select b.Name as TableName from Sys.Columns a Join Sys.Tables b on a.Object_Id=b.Object_Id where a.Name=@ColumnName