提问人:gruber 提问时间:1/31/2011 最后编辑:Rand Randomgruber 更新时间:10/16/2023 访问量:3830211
查找包含指定名称列的所有表 - MS SQL Server
Find all tables containing column with specified name - MS SQL Server
问:
是否可以查询包含列的表名
LIKE '%myName%'
?
答:
这应该有效:
SELECT name
FROM sysobjects
WHERE id IN ( SELECT id
FROM syscolumns
WHERE name like '%column_name%' )
评论
sys.tables
sysobjects
搜索表:
SELECT c.name AS 'ColumnName'
,(SCHEMA_NAME(t.schema_id) + '.' + 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;
评论
INFORMATION_SCHEMA
sys.columns
sys.tables
TABLE_SCHEMA
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%myName%'
我们还可以使用以下语法:-
select * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%clientid%'
order by TABLE_NAME
评论
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 博客
以下查询将为您提供字段名称为“%myName”的数据库的确切表名。
SELECT distinct(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%myName%'
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 仅搜索表。它非常可定制。
享受。
评论
如果您更喜欢第三方工具,那里有很多选择,例如:
如果您的数据库包含加密对象(视图、过程、函数),则这些会非常方便,因为您无法使用系统表轻松搜索这些对象。
SELECT [TABLE_NAME] ,
[INFORMATION_SCHEMA].COLUMNS.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%NAME%' ;
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
希望这不是一个重复的答案,但我喜欢做的是在 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%'
我刚刚尝试过,效果很好
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 更改为您要查找的列名,其余的保持原样。
希望这有帮助
我不知道为什么你们中的许多人建议加入.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%'
评论
我将其用于相同的目的,并且它起作用了:
select * from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG= 'theDatabase'
and COLUMN_NAME like 'theCol%'
为了改进上面的答案,我还包含了视图,并将架构和表/视图连接在一起,使结果更加明显。
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 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;
如果你只想要表名,你可以运行:
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 及更早版本,请使用 + 连接字符串。
自从我发布这个以来,我已经重新格式化了过程。它现在更高级一些,但看起来更混乱(但它在一个过程中,所以你永远不会看到它),而且它的格式更好。
此版本允许您将其保存在管理数据库中,然后搜索任何数据库。将 from 的递减更改为您希望默认数据库为的任何一个(注意:使用 CONCAT() 函数仅适用于 2012+,除非您更改字符串连接以使用运算符)。@db
'master'
+
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
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)
我一直使用它来查找给定数据库(架构)中列名的所有实例。
像 oracle 一样,您可以使用以下命令找到表和列:
select table_name, column_name
from user_tab_columns
where column_name
like '%myname%';
您可以从INFORMATION_SCHEMA找到它。按column_name过滤器划分的 COLUMNS
Select DISTINCT TABLE_NAME as TableName,COLUMN_NAME as ColumnName
From INFORMATION_SCHEMA.COLUMNS Where column_name like '%myname%'
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 COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%col_Name%'
您可以尝试以下查询:
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%'
评论
下面是 Sybase 数据库的工作解决方案
select
t.table_name,
c.column_name
from
systab as t key join systabcol as c
where
c.column_name = 'MyColumnName'
评论
我们可以为此目的使用。sp_columns
sp_columns 'table name', null, null, '%column name%'
评论
用于显示指定列名的所有表的 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'
我想要一些不会让我的眼睛流血的桌子和景观。
查询
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
这是您问题的答案
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%';
在 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>%'
在 MS SQL 中,您可以编写以下行来检查特定表的列名:
sp_help your_tablename
或者,您可以先在查询窗口中选择表名称(突出显示架构和表名称),然后按下面的组合键:
Alt + F1
评论
此外,还可以找到具有指定架构的列名称。
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%'
SELECT table_schema + '.' + table_name,
column_name
FROM [yourdatabase].information_schema.columns
WHERE column_name LIKE '%myName%'
这将为您提供需要查找的列的表名。
这个简单的查询对我有用。
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'schemaName'
AND column_name like '%model%';
评论
可以在 SQL Server 中使用以下查询。
SELECT column_name AS 'ColumnName',
table_name AS 'TableName'
FROM information_schema.columns
WHERE column_name LIKE '%columnname%'
AND table_schema = 'schema'
ORDER BY tablename,
columnname;
它将返回 table_name , schema_name , column_name
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 '%colName%'
ORDER BY schema_name,
table_name;
若要在 MS SQL Server 中查找包含具有指定名称的列的所有表,可以查询系统目录视图。具体而言,可以查询 sys.tables 和 sys.columns 视图。下面是一个示例查询:
SELECT t.name AS table_name
FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name = 'column_name';
替换为要搜索的列的名称。此查询根据 object_id 列联接 sys.tables 和 sys.columns 视图,并筛选结果以检索列名与指定值匹配的表名。column_name
在 dbForge Studio for SQL Server 中,您可以使用 SQL 编辑器执行上述查询。它提供语法高亮显示、代码完成和结果查看等功能,可以更轻松地高效编写和执行 SQL 查询。
评论