How can I search all columns of a table in SQL Server?
如何在SQL Server中搜索表的所有列?
2 个解决方案
#1
15
SELECT ...
FROM yourtable
WHERE 'val' IN (field1, field2, field3, field4, ...)
if you're looking for exact full-field matches. If you're looking for substring matches, you'll have to go about it the long way:
如果你正在寻找确切的全场比赛。如果你正在寻找子串匹配,你将不得不走很远的路:
WHERE field1 LIKE '%val%' or field2 LIKE '%val%' etc....
#2
2
I don't believe there is any shortcut for this, you have to specify out the list of columns you wish to search. I would argue that if you find yourself trying to do this alot, you probably could improve upon the DB design.
我不相信有任何快捷方式,您必须指定要搜索的列的列表。我认为,如果你发现自己试图做很多,你可能会改进数据库设计。
SELECT *
FROM MyTable
WHERE Col1 LIKE '%foo%' OR
Col2 LIKE '%foo%' OR
Col3 LIKE '%foo%' OR
Col4 LIKE '%foo%' OR
Col5 LIKE '%foo%' OR
Col6 LIKE '%foo%'
#1
15
SELECT ...
FROM yourtable
WHERE 'val' IN (field1, field2, field3, field4, ...)
if you're looking for exact full-field matches. If you're looking for substring matches, you'll have to go about it the long way:
如果你正在寻找确切的全场比赛。如果你正在寻找子串匹配,你将不得不走很远的路:
WHERE field1 LIKE '%val%' or field2 LIKE '%val%' etc....
#2
2
I don't believe there is any shortcut for this, you have to specify out the list of columns you wish to search. I would argue that if you find yourself trying to do this alot, you probably could improve upon the DB design.
我不相信有任何快捷方式,您必须指定要搜索的列的列表。我认为,如果你发现自己试图做很多,你可能会改进数据库设计。
SELECT *
FROM MyTable
WHERE Col1 LIKE '%foo%' OR
Col2 LIKE '%foo%' OR
Col3 LIKE '%foo%' OR
Col4 LIKE '%foo%' OR
Col5 LIKE '%foo%' OR
Col6 LIKE '%foo%'