如果您必须multiple patterns
搜索,那么最好创建derived table
或temporary table
存储patterns
DECLARE @patterns TABLE (
pattern VARCHAR(20)
);
INSERT INTO @patterns VALUES ('%$%'), ('%[%]%'), ('%ac cd%');
SELECT a.* FROM table_name a JOIN @patterns p ON (a.Description LIKE p.pattern);
如果您有多个要比较的列,只需在ON
子句中添加表达式
SELECT a.* FROM table_name a JOIN @patterns p ON (a.Description LIKE p.pattern)
OR (a.Column2 like p.pattern);
更新:使用视图
CREATE VIEW PatternSearch
AS
WITH Patterns (Patterns)
AS
(
Select '%Tool%'
union all
Select '%Sale%'
union all
Select '%ac cd%'
)
SELECT a.* FROM table_name a JOIN Patterns p ON (a.Description LIKE p.pattern)
GO
未经测试,但你可以这样做
来源