查找包含具有指定名称的列的所有表-MS SQL Server


1201

是否可以查询包含列的表名

LIKE '%myName%'


12
sql server有一个了不起的插件,可以搜索所有对象类型。sql search red-gate.com/products/sql-development/sql-search
Vbp

1
@vbp:sql-search确实很棒,但是像许多工具一样,它不适用于SQL Server 2000(是的,我现在
仍然

2
@vbp ApexSQL搜索SSMS提供的功能更多
Liu

Answers:


1847

搜索表:

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;

搜索表和视图:

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

17
INFORMATION_SCHEMA SQL Server中包含的 @Revious 视图符合INFORMATION_SCHEMA的ISO标准定义。sys.columnssys.tables为Microsoft SQL Server特有的。
Tomasito

4
将其包括TABLE_SCHEMA在选择列表中可能会有所帮助。仍然+1,因为这是一个很好的答案。
2014年

1
您是否可以将@ user3185569所评论的两者之间的区别的说明添加到您的答案中?
瑞安·盖茨

1
如果您碰巧必须使用第二种方法,则第二种方法也可用于SQL Server 2000
Patrick Honorez,2016年

1
还获得一个表模式:SELECT c.name AS ColName,t.name AS TableName,SCHEMA_NAME(t.schema_id)AS SchemaName ....
SkorunkaFrantišek16年


178

SQL Server:

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

甲骨文:

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)
    我一直使用它来查找给定数据库(架构)中列名称的所有实例。

4
我真的经常复制和粘贴此SQL,谢谢@Todd_ECU
jonaglon

1
这确实应该是公认的答案。您不必进行联接,这比接受的答案容易得多
Kolob Canyon

106
select  
        s.[name]            'Schema',
        t.[name]            'Table',
        c.[name]            'Column',
        d.[name]            'Data Type',
        c.[max_length]      'Length',
        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%'

这将为您提供有关架构,表和列的一些额外信息,您可以选择也可以不选择在where子句中使用额外条件进行过滤。例如,如果您只想查看必须具有值的字段,请添加

and c.is_nullable = 0

您可以添加其他条件,我也以这种垂直方式在select子句中添加了列,因此可以轻松地根据需要重新排序,删除,重命名或添加其他条件。或者,您可以使用T.Name搜索仅表。它非常可定制。

请享用。


3
d。[max_length]似乎没用。c。[max_length]可能就是您的意思。但是仍然让我到那里来投票。
user1566694

你可以在你的查询🤫结束“ORDER BY t.name”把这个
Fuat

用户当心。上面的查询暗示对“ sys”对象的访问并不总是正确的。以我为例,该查询未返回任何内容。相反,使用'INFORMATION_SCHEMA'就像一种魅力。
OrizG

85

这应该工作:

SELECT name 
FROM sysobjects 
WHERE id IN ( SELECT id 
              FROM syscolumns 
              WHERE name like '%column_name%' )

6
我将使用sys.tables代替sysobjects(从SQL Server 2005开始不推荐使用)
marc_s

3
无效的列名“ id”
JSON

62

如果您更喜欢第三方工具,则那里有很多选择,例如:

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


46

我不知道为什么这么多建议加入的人sys.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%'

1
他们正在从sys.tables获取架构名称,也许这对您来说不是问题,但对很多人来说却是问题。
K Kimble

嗨,但仍然不需要与sys.objects
关联

很高兴您指出了INFORMATION_SCHEMA.COLUMNS,但是,如果您运行EXEC sp_helptext INFORMATION_SCHEMA.COLUMNS,您会发现它确实在做同样的事情,而您可能不需要很多其他无用的东西。
K Kimble

40

如果只需要表名,则可以运行:

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

如果还需要模式名称(在许多情况下,您将拥有很多不同的模式,并且会记住数据库中的每个表及其所属的表,那么在很多情况下您都会用到),请运行:

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

请注意,您还可以根据自己的情况创建一个函数:

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及更早版本,请使用+连接字符串。

自从发布此代码以来,我对proc进行了重新格式化。现在它有点先进,但是看起来更混乱(但是它在proc中,所以您永远不会看到它)并且它的格式更好。

此版本允许您将其保存在管理数据库中,然后搜索任何数据库。将默认值@db从更改'master'为任何值(注意:除非将字符串串联更改为使用+运算符,否则使用CONCAT()函数仅适用于2012+ )。

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

26
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


24
SELECT COLUMN_NAME, TABLE_NAME
  FROM INFORMATION_SCHEMA.COLUMNS    
 WHERE COLUMN_NAME LIKE '%myName%'

23

您可以通过column_name过滤器从INFORMATION_SCHEMA.COLUMNS中找到它

Select DISTINCT TABLE_NAME as TableName,COLUMN_NAME as ColumnName
     From INFORMATION_SCHEMA.COLUMNS Where column_name like '%myname%'


17

以下查询将为您提供数据库字段的确切表名,其字段名类似于'%myName'。

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

15

要获取完整的信息,请执行以下操作:列名,表名以及表的架构。

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

13

我刚刚尝试过,这很好用

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更改为您要查找的其余部分的列名,请保持原样。

希望这有所帮助


8
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

7

我将其用于相同的目的,并且有效:

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

6

希望这不是一个重复的答案,但是我想做的是在sql语句中生成一个sql语句,该语句可让我搜索要查找的值(而不仅仅是具有这些字段名称的表(通常是然后删除与我要查找的列名的ID相关的任何信息):

  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” ...然后我将“ Select * from”替换为“ Delete from”,它允许我删除对该给定ID的任何引用!将这些结果写入文件中,这样您就可以以防万一。

注意****确保在运行delete语句之前消除所有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%'

6
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;

6

像oracle一样,您可以使用以下命令找到表和列:

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

5

我想要一些不会让我流血的表格 视图

询问

SELECT
    t.TABLE_TYPE AS [Type],
    c.TABLE_NAME AS [Object],
    c.COLUMN_NAME AS [Column]
FROM
    INFORMATION_SCHEMA.COLUMNS AS c
    LEFT JOIN INFORMATION_SCHEMA.TABLES AS t ON
        t.TABLE_CATALOG = c.TABLE_CATALOG AND 
        t.TABLE_SCHEMA = c.TABLE_SCHEMA AND
        t.TABLE_NAME = c.TABLE_NAME
WHERE
    c.COLUMN_NAME LIKE '%myName%'
ORDER BY
    [Type],
    [Object],
    [Column]

结果

Type        Object   Column
----------------------------
BASE TABLE  Table1   myName1
BASE TABLE  Table2   myName2
VIEW        View1    myName1
VIEW        View2    myName2

4

为了改善上面的答案,我还包括了Views以及将Schema和Table / View串联在一起,使结果更加明显。

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];

4

您可以尝试以下查询:

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

1
看起来非常类似于blog.sqlauthority.com/2008/08/06/…如果没有其他帮助,可以帮助您进行解释。
user3428422'4

3
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

这就是我一直在寻找的,跨任何数据库的解决方案,而不仅仅是一个。
Fandango68

3

这是您问题的答案

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

0

这是Sybase数据库的有效解决方案

select 
  t.table_name, 
  c.column_name 
from 
  systab as t key join systabcol as c 
where 
   c.column_name = 'MyColumnName'

2
不工作。关键字“ key”附近的语法不正确。
Paiman Samadian


0

SQL查询以显示具有指定列名的所有表:

SELECT SCHEMA_NAME(schema_id) + '.' + t.name AS 'Table Name'
  FROM sys.tables t
 INNER JOIN sys.columns c ON c.object_id = t.object_id
 WHERE c.name like '%ColumnName%'
 ORDER BY 'Table Name'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.