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

时间:2011-01-31 10:12:26

标签: sql sql-server tsql system-tables

是否可以查询包含

列的表名
LIKE '%myName%'

31 个答案:

答案 0 :(得分:1579)

搜索表:

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;

答案 1 :(得分:267)

我们还可以使用以下语法: -

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

答案 2 :(得分:126)

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

答案 3 :(得分:71)

这应该有效:

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

答案 4 :(得分:61)

如果您更多地使用第三方工具,那里有很多选项,例如:

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

答案 5 :(得分:50)

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子句中使用额外条件进行筛选。例如,如果您只想查看必须具有值add

的字段
and c.is_nullable = 0

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

享受。

答案 6 :(得分:42)

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

答案 7 :(得分:25)

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

答案 8 :(得分:23)

这是您问题的答案

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

答案 9 :(得分:22)

您可以通过column_name filter

从INFORMATION_SCHEMA.COLUMNS中找到它
var search = "May";

var indexOfCurrentMonthSwiper;

var currentMonthSwiper = $(".swiper-slide").filter(function(i){
    if ($(this).find("h3").html() == search){
      indexOfCurrentMonthSwiper = i;
      return true;
    }
    return false;
});

console.log(currentMonthSwiper, indexOfCurrentMonthSwiper);

答案 10 :(得分:20)

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

答案 11 :(得分:20)

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

答案 12 :(得分:16)

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

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

答案 13 :(得分:14)

获取完整信息:列名,表名以及表的架构..

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

答案 14 :(得分:13)

在MS SQL Server数据库中,使用此查询来获取包含输入文本的表和相应的列名称:

SELECT t.name AS tableName, c.name AS columnName 
FROM sys.tables as t 
INNER JOIN sys.columns AS c ON t.object_id=c.object_id 
WHERE c.name LIKE '%<your_search_string>%'

答案 15 :(得分:12)

我刚尝试过,这完美无缺

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

希望这有所帮助

答案 16 :(得分:12)

我想要一些桌子 视图,这些东西不会让我流血。

查询

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

答案 17 :(得分: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

答案 18 :(得分:7)

      SELECT   TABLE_SCHEMA +'.'+ TABLE_NAME, COLUMN_NAME   
      FROM [yourdatabase].INFORMATION_SCHEMA.COLUMNS
      WHERE COLUMN_NAME LIKE '%myName%'

这将为您提供需要查找的列的表名。

答案 19 :(得分:7)

我出于同样的目的使用它,它起作用了:

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

答案 20 :(得分: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;

答案 21 :(得分:6)

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

  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的任何引用!将这些结果写入文件,所以你有他们以防万一。

注意****确保在运行删除声明之前删除任何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%'

答案 22 :(得分:5)

像oracle一样,你可以找到表格和列:

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

答案 23 :(得分:3)

您可以尝试此查询:

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

答案 24 :(得分:3)

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'

答案 25 :(得分:3)

为了改进上面的答案,我还包括了视图,并将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];

答案 26 :(得分:2)

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

答案 27 :(得分:2)

我们可以使用sp_columns

sp_columns 'table name', null, null, '%column name%'

答案 28 :(得分:2)

在MS SQL中,您可以编写以下行来检查特定表的列名:

sp_help your_tablename

或者,您可以通过选择表名来按以下键:

Alt + F1

答案 29 :(得分:0)

这是Sybase数据库的可行解决方案

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

答案 30 :(得分:0)

此外,您还可以找到具有指定架构的列名。

 SELECT 'DBName' as DBName, COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA
    FROM  DBName.INFORMATION_SCHEMA.COLUMNS  
    WHERE COLUMN_NAME LIKE '%YourColumnName%' and TABLE_SCHEMA IN ('YourSchemaName')

您还可以在多个数据库中找到相同的列。

SELECT 'DBName1' as DB, COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA
FROM DBName1.INFORMATION_SCHEMA.COLUMNS    
WHERE COLUMN_NAME LIKE '%YourColumnName%' 
 UNION 
SELECT 'DBName2' as DB,  COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA
FROM DBName2.INFORMATION_SCHEMA.COLUMNS    
WHERE COLUMN_NAME LIKE '%YourColumnName%'