简体   繁体   English

在sql server中使用%wildcard和'in'运算符

[英]use % wildcard with ' in ' operator in sql server

i have a stored procedure and im passing a list of comma separated values as the @At1 我有一个存储过程,我传递一个逗号分隔值列表@ At1

Create spGetProducts @At1 VARCHAR(200)
begin
select * from tblProducts where Category1 IN (SELECT * FROM CSVToTable(@At2))
end

the function CSVToTable basically takes the comma separated values and puts them in a table. 函数CSVToTable基本上采用逗号分隔值并将它们放在表中。

problem is i would like to use a LIKE wildcard with the IN operator however that does not work. 问题是我想在IN运算符中使用LIKE通配符,但这不起作用。

So if i have a in row Category1 a test and the CSVTOTABLE would return a value of 'es', then it would select the row just like if i had %es%. 因此,如果我在一行中排序Class1进行测试并且CSVTOTABLE将返回值'es',那么它将选择该行,就像我有%es%。 Basically i just want to have percentage signs in the comma separated values while using the in operator. 基本上我只想在使用in运算符时在逗号分隔值中包含百分号。

im using sql server 2012 即时通讯使用sql server 2012

EDIT 编辑

my CSVTABLE return one column table with each row having a comma separated value. 我的CSVTABLE返回一个列表,每行都有逗号分隔值。

Rewrite the IN to EXISTS which is more general: IN重写为更通用的EXISTS

select *
from tblProducts p
where exists (
 select *
 from CSVToTable(@At2)
 where Category1 LIKE (SomeExpressionInvolvingTheResultFromCSV)
)

You might want to pull the results from CSVToTable into a table variable or temp table first for caching reasons. 出于缓存原因,您可能希望首先将CSVToTable的结果拉入表变量或临时表中。

Replace IN with a JOIN ON 用JOIN ON替换IN

  select distinct t.* from tblProducts t 
     inner join CSVToTable(@At2) f  
        on t.category1 like f.field1

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

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