简体   繁体   English

在数据库的所有列和所有表中搜索一个值

[英]Search for a value in all column and all tables of a database

I want to find the column name that contain the value "Commerciale", but i do not know the column name or the table so I need to search in the whole database.我想找到包含值“Commerciale”的列名,但我不知道列名或表,所以我需要在整个数据库中搜索。 How can i do that with a query?我如何通过查询来做到这一点?

I'm using SQL SERVER我正在使用 SQL 服务器

you can use system tables:您可以使用系统表:

SELECT 
    c.name ColumnName
 , t.name TableName
FROM sys.columns AS c
JOIN sys.tables AS t
ON c.object_id = t.object_id
WHERE c.name like '%Commerciale%'

If you are looking for columns where the name is Commerciale then you can simply use the sys objects:如果您正在寻找名称为Commerciale的列,那么您可以简单地使用sys对象:

SELECT s.[name] AS SchemaName,
       t.[name] AS TableName,
       c.[name] AS ColumnName
FROM sys.schemas s
     JOIN sys.tables t ON s.schema_id = t.schema_id
     JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.[name] = N'Commerciale';

If, however, you need to search the contents of the values in the rows, you'll need to use dynamic SQL.但是,如果您需要搜索行中值的内容,则需要使用动态 SQL。 This will return a dataset for every table in your database which has at least 1 string type column, and will return any rows where the value of one of those columns has the value 'Commerciale' .这将为数据库中至少有 1 个字符串类型列的每个表返回一个数据集,并将返回其中一个列的值为'Commerciale'任何行。 If it needs to contain the value, change the WHERE to use a LIKE in it's clauses instead (note the query will be horrifically slow with that):如果它需要包含该值,请将WHERE更改为在其子句中使用LIKE (请注意,查询将非常缓慢):

DECLARE @SQL nvarchar(MAX),
        @CRLF nchar(2) = NCHAR(13) + NCHAR(10);

SET @SQL = STUFF((SELECT @CRLF +
                         N'SELECT N' + QUOTENAME(s.[name],'''') + N' AS SchemaName,' + @CRLF +
                         N'       N' + QUOTENAME(t.[name],'''') + N' AS TableName,' + @CRLF +
                         N'       *' + @CRLF +
                         N'FROM ' + QUOTENAME(s.[name]) + N'.' + QUOTENAME(t.[name]) + @CRLF + 
                         N'WHERE ' +
                         STUFF((SELECT @CRLF +
                                       N'  AND ' + QUOTENAME(c.[name]) + N' = ''Commerciale'''
                                FROM sys.columns c
                                     JOIN sys.types ct ON c.system_type_id = ct.system_type_id
                                WHERE c.object_id = t.object_id
                                  AND ct.[name] IN (N'char',N'varchar',N'nchar',N'nvarchar')
                                FOR XML PATH(''),TYPE).value('(./text())[1]','nvarchar(MAX)'),1,8,N'') + N';'
                  FROM sys.schemas s
                       JOIN sys.tables t ON s.schema_id = t.schema_id
                  FOR XML PATH(''),TYPE).value('(./text())[1]','nvarchar(MAX)'),1,2,N'');

--PRINT @SQL; --YOu best friend

EXEC sp_executesql @SQL;

This won't tell you what column has the value, you'll need to use your own eyes to do that, but I wasn't entertaining writing a dynamic table dynamic pivot.这不会告诉您哪一列具有价值,您需要用自己的眼睛来做到这一点,但我不喜欢编写动态表动态 pivot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM