[英]SQL query to return all rows where one or more of the fields contains a certain value
Is it possible to run a select on a table to quickly find out if any (one or more) of the fields contain a certain value? 是否可以在表上运行一次select以快速找出是否有一个 (或多个)字段包含特定值?
Or would you have to write out all of the column names in the where clause? 还是您必须在where子句中写出所有列名?
As others have said, you're likely going to have to write all the columns into your WHERE clause, either by hand or programatically. 正如其他人所说,您可能必须手动或以编程方式将所有列写入WHERE子句。 SQL does not include functionality to do it directly. SQL不包含直接执行此操作的功能。 A better question might be "why do you need to do this?". 一个更好的问题可能是“您为什么需要这样做?”。 Needing to use this type of query is possibly a good indicator that your database isn't properly normalized . 需要使用这种类型的查询可能是一个很好的信号,表明您的数据库没有被正确规范化 。 If you tell us your schema, we may be able to help with that problem too (if it's an actual problem). 如果您告诉我们您的方案,我们也可以为该问题提供帮助(如果这是实际问题)。
I think you'd need to list all the columns in the where clause. 我认为您需要在where子句中列出所有列。 I'm far from a SQL wizard though...maybe someone else knows a way. 我离SQL向导还很远……也许其他人知道一种方法。
你将不得不写出来
Of course you have to write out all columns you want to use as a criteria. 当然,您必须写出要用作条件的所有列。
If you add what programming language you are using and in what type of environment you working we can give you a clue or solution of how to do it dynamically. 如果添加您正在使用的编程语言以及在哪种类型的环境中工作,我们可以为您提供动态实现的线索或解决方案。
I think your question really was how to do this dynamically depending of what the user of your program fill in in the "search"-form.. Im right? 我认为您的问题确实是如何根据程序用户在“搜索”表单中填写的内容动态地执行此操作。
If not, then.. Give us more information. 如果没有,那么..给我们更多信息。 ;) ;)
Dig this... It will search on all the tables in the db, but you can mod it down to just one table. 挖这个...它将搜索数据库中的所有表,但是您可以将其修改为仅一个表。
/*This script will find any text value in the database*/
/*Output will be directed to the Messages window. Don't forget to look there!!!*/
SET NOCOUNT ON
DECLARE @valuetosearchfor varchar(128), @objectOwner varchar(64)
SET @valuetosearchfor = '%staff%' --should be formatted as a like search
SET @objectOwner = 'dbo'
DECLARE @potentialcolumns TABLE (id int IDENTITY, sql varchar(4000))
INSERT INTO @potentialcolumns (sql)
SELECT
('if exists (select 1 from [' +
[tabs].[table_schema] + '].[' +
[tabs].[table_name] +
'] (NOLOCK) where [' +
[cols].[column_name] +
'] like ''' + @valuetosearchfor + ''' ) print ''SELECT * FROM [' +
[tabs].[table_schema] + '].[' +
[tabs].[table_name] +
'] (NOLOCK) WHERE [' +
[cols].[column_name] +
'] LIKE ''''' + @valuetosearchfor + '''''' +
'''') as 'sql'
FROM information_schema.columns cols
INNER JOIN information_schema.tables tabs
ON cols.TABLE_CATALOG = tabs.TABLE_CATALOG
AND cols.TABLE_SCHEMA = tabs.TABLE_SCHEMA
AND cols.TABLE_NAME = tabs.TABLE_NAME
WHERE cols.data_type IN ('char', 'varchar', 'nvchar', 'nvarchar','text','ntext')
AND tabs.table_schema = @objectOwner
AND tabs.TABLE_TYPE = 'BASE TABLE'
ORDER BY tabs.table_catalog, tabs.table_name, cols.ordinal_position
DECLARE @count int
SET @count = (SELECT MAX(id) FROM @potentialcolumns)
PRINT 'Found ' + CAST(@count as varchar) + ' potential columns.'
PRINT 'Beginning scan...'
PRINT ''
PRINT 'These columns contain the values being searched for...'
PRINT ''
DECLARE @iterator int, @sql varchar(4000)
SET @iterator = 1
WHILE @iterator <= (SELECT Max(id) FROM @potentialcolumns)
BEGIN
SET @sql = (SELECT [sql] FROM @potentialcolumns where [id] = @iterator)
IF (@sql IS NOT NULL) and (RTRIM(LTRIM(@sql)) <> '')
BEGIN
--SELECT @sql --use when checking sql output
EXEC (@sql)
END
SET @iterator = @iterator + 1
END
PRINT ''
PRINT 'Scan completed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.