SELECT c.name AS 'ColumnName',t.name AS 'TableName'FROM sys.columns cJOIN sys.tables t ON c.object_id = t.object_idWHERE c.name LIKE '%MyName%'ORDER BY TableName,ColumnName;
搜索表和视图:
SELECT COLUMN_NAME AS 'ColumnName',TABLE_NAME AS 'TableName'FROM INFORMATION_SCHEMA.COLUMNSWHERE COLUMN_NAME LIKE '%MyName%'ORDER BY TableName,ColumnName;
USE AdventureWorks
GO
SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDWHERE c.name LIKE '%EmployeeID%'ORDER BY schema_name, table_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 LengthFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDINNER JOIN sys.types ty ON c.system_type_id = ty.system_type_idWHERE c.name LIKE @columnNameORDER BY t.name, c.name
SELECT 'Select * from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRunFROM sys.columns c, c.name as ColName, t.name as TableNameJOIN sys.tables tON c.object_id = t.object_idWHERE c.name LIKE '%ProjectID%'
SELECT 'Delete from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRunFROM sys.columns c, c.name as ColName, t.name as TableNameJOIN sys.tables tON c.object_id = t.object_idWHERE c.name LIKE '%ProjectID%'
USE YourDatabseNameGOSELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDWHERE c.name LIKE '%YourColumnName%'ORDER BY schema_name, table_name;
SELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDWHERE c.name LIKE '%Label%'ORDER BY schema_name, table_name;
select OBJECT_SCHEMA_NAME(object_id),object_name(object_id) from sys.columnswhere name like '%received_at%'
最后,如果你想要更好的格式(尽管这是代码(在我看来)变得太复杂而无法轻松编写的地方):
select concat(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) from sys.columnswhere 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)ASSELECT CONCAT(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) AS [Table Name], name AS [Column] FROM sys.columnsWHERE name LIKE CONCAT('%',@name,'%')ORDER BY [Table Name] ASC, [Column] ASCGO
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'ASDECLARE @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 colLEFT JOIN ',@db,'.sys.objects obON ob.object_id = col.object_idWHEREcol.name LIKE CONCAT(''%'',''',@name,''',''%'')AND ob.type =''U''ORDER BY [Table Name] ASC,[Column] ASC')EXECUTE (@sql)GO
USE AdventureWorksGOSELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDWHERE c.name LIKE '%myName%'
SELECT SCHEMA_NAME(schema_id) + '.' + t.name AS 'Table Name'FROM sys.tables tINNER JOIN sys.columns c ON c.object_id = t.object_idWHERE c.name like '%ColumnName%'ORDER BY 'Table Name'
SELECTt.TABLE_TYPE AS [Type],c.TABLE_NAME AS [Object],c.COLUMN_NAME AS [Column]FROMINFORMATION_SCHEMA.COLUMNS AS cLEFT JOIN INFORMATION_SCHEMA.TABLES AS t ONt.TABLE_CATALOG = c.TABLE_CATALOG ANDt.TABLE_SCHEMA = c.TABLE_SCHEMA ANDt.TABLE_NAME = c.TABLE_NAMEWHEREc.COLUMN_NAME LIKE '%myName%'ORDER BY[Type],[Object],[Column]
SELECT t.name AS tableName, c.name AS columnNameFROM sys.tables as tINNER JOIN sys.columns AS c ON t.object_id=c.object_idWHERE c.name LIKE '%<your_search_string>%'
SELECT 'DBName' AS DBName,column_name,table_name,table_schemaFROM dbname.information_schema.columnsWHERE column_name LIKE '%YourColumnName%'AND table_schema IN ( 'YourSchemaName' )
您还可以在多个数据库中找到相同的列。
SELECT 'DBName1' AS DB,column_name,table_name,table_schemaFROM dbname1.information_schema.columnsWHERE column_name LIKE '%YourColumnName%'UNIONSELECT 'DBName2' AS DB,column_name,table_name,table_schemaFROM dbname2.information_schema.columnsWHERE column_name LIKE '%YourColumnName%'
SELECT column_name AS 'ColumnName',table_name AS 'TableName'FROM information_schema.columnsWHERE column_name LIKE '%columnname%'AND table_schema = 'schema'ORDER BY tablename,columnname;
SELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns cON t.object_id = c.object_idWHERE c.name LIKE '%colName%'ORDER BY schema_name,table_name;