是否可以查询包含以下列的表名
LIKE '%myName%'
?
是否可以查询包含以下列的表名
LIKE '%myName%'
?
当前回答
此外,还可以找到具有指定模式的列名。
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%'
其他回答
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博客
希望这不是一个重复的答案,但我想做的是在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%'
如果你更喜欢第三方工具,有很多选择,比如:
ApexSQL搜索SSMS工具包红门工具。
如果您的数据库包含加密对象(视图、过程、函数),则这些对象非常有用,因为您无法使用系统表轻松搜索这些对象。
我不知道为什么很多人建议使用sys.columns加入sys.table。
您可以使用以下代码:
SELECT Object_name(object_id) AS TableName,
*
FROM sys.columns
WHERE NAME LIKE '%MyName%'
or
如果还需要架构名称:
SELECT *
FROM information_schema.columns
WHERE column_name LIKE '%MyName%'
您可以尝试以下查询:
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%'