Find all tables containing column with specified name - MS SQL Server
是否可以查询包含列的表名
1 | LIKE '%myName%' |
?
搜索表:
1 2 3 4 5 6 7 | 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; |
搜索表和视图:
1 2 3 4 5 6 | SELECT COLUMN_NAME AS 'ColumnName' ,TABLE_NAME AS 'TableName' FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%MyName%' ORDER BY TableName ,ColumnName; |
我们还可以使用以下语法:
1 2 3 | SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%clientid%' ORDER BY TABLE_NAME |
SQLServer:
1 2 3 4 | SELECT TABLE_NAME, Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = 'YOUR_DATABASE' AND COLUMN_NAME LIKE '%YOUR_COLUMN%' |
神谕:
1 2 3 4 | 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)我一直使用它来查找给定数据库(模式)中列名的所有实例。
这应该有效:
1 2 3 4 5 | SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name LIKE '%column_name%' ) |
如果你更喜欢第三方工具,有很多选择,比如:
- APEXSQL搜索
- SSMS工具包
- 红门工具。
如果您的数据库包含加密对象(视图、过程、函数),那么这些方法非常有用,因为您无法使用系统表轻松搜索这些对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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%' |
这里将为您提供一些关于模式、表和列的额外信息,您可以选择在WHERE子句中使用或不使用额外条件进行筛选。例如,如果只想查看必须添加值的字段
1 | AND c.is_nullable = 0 |
您可以添加其他条件,我还以这种垂直方式在select子句中添加了列,这样就可以根据需要轻松地重新排序、删除、重命名或添加其他条件。或者,您可以使用t.name只搜索表。它非常可定制。
享受。
我不知道你们为什么这么多人建议加入
或
如果还需要架构名称:
1 2 | SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%MyName%' |
如果只需要表名,可以运行:
1 2 | SELECT object_name(object_id) FROM sys.columns WHERE name LIKE '%received_at%' |
如果您也想要模式名(在很多情况下,您会有很多不同的模式,并且除非您能记住数据库中的每个表以及它所属的位置,否则这是很有用的),请运行:
1 2 | SELECT OBJECT_SCHEMA_NAME(object_id),object_name(object_id) FROM sys.columns WHERE name LIKE '%received_at%' |
最后,如果你想要一个更好的格式(尽管这是代码(在我看来)变得过于复杂,不容易编写的地方):
1 2 | SELECT concat(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) FROM sys.columns WHERE name LIKE '%received_at%' |
注意,您还可以根据我的功能创建一个函数:
1 2 3 4 5 6 7 8 9 | 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进行了一点格式化。它现在有点高级,但看起来更凌乱(但它在一个过程中,所以你永远不会看到它),而且它的格式更好。
此版本允许您将其保存在管理数据库中,然后搜索任何数据库。将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 |
1 2 3 4 5 6 7 8 9 10 | 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; |
是皮纳尔先生的博客
您可以通过信息"schema.columns by columnu name filter"找到它。
1 2 | SELECT DISTINCT TABLE_NAME AS TableName,COLUMN_NAME AS ColumnName FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE '%myname%' |
1 2 3 | SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%myName%' |
1 2 3 4 | SELECT [TABLE_NAME] , [INFORMATION_SCHEMA].COLUMNS.COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%NAME%' ; |
下面的查询将为您提供字段名为"%myname"的数据库的确切表名。
1 2 3 | SELECT DISTINCT(TABLE_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%myName%' |
获取完整信息:列名称、表名称以及表的架构。
1 2 3 | SELECT COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%col_Name%' |
我刚试过,这个效果很好
1 2 3 4 5 6 7 8 9 | 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; |
只将rdatabasename更改为数据库,将column name更改为要查找的列名称,其余的保持不变。
希望这有帮助
1 2 3 4 5 6 7 8 9 10 | 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 |
我把它用于同样的目的,它起到了作用:
1 2 3 | SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG= 'theDatabase' AND COLUMN_NAME LIKE 'theCol%' |
1 2 3 4 5 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; |
希望这不是一个重复的答案,但是我喜欢在SQL语句中生成一个SQL语句,它允许我搜索我要查找的值(不仅仅是具有这些字段名的表(因为我通常需要删除与我要查找的列名的ID相关的任何信息):
1 2 3 4 5 | 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语句之前,请确保消除所有的备份表…
1 2 3 4 5 | 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%' |
与Oracle一样,您可以使用以下命令查找表和列:
1 2 3 4 | SELECT TABLE_NAME, column_name FROM user_tab_columns WHERE column_name LIKE '%myname%'; |
为了改进上面的答案,我还包含了视图,并将模式和表/视图连接在一起,使结果更加明显。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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]; |
您可以尝试此查询:
1 2 3 4 5 6 7 8 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 |
以下是Sybase数据库的工作解决方案
1 2 3 4 5 6 7 | SELECT t.table_name, c.column_name FROM systab AS t KEY JOIN systabcol AS c WHERE c.column_name = 'MyColumnName' |