繁体   English   中英

如何在SQL Server中使用(IN)运算符

[英]How to use (IN) Operator in SQL Server

如何在SQL Server中使用IN运算符? 我正在使用一个5列的表格。 但是我想通过一个存储过程来选择列

 Select * 
 From products 
 Where proprice between @price1 and @price2 
   and categoryid in (@cID)

当我执行此存储过程时,它成功执行。 但是我将值传递给三个参数,我无法获得正确的返回数据。

数据样本:

ProductID ProductName ProductImage           ProductPrice
 20       Nike Shoe   ~/Images/NikeShoe1.jpg 5000

如果值以逗号分隔,请尝试此操作。 另请参阅有关规范化

Select * from products 
 where proprice between @price1 and @price2 and '%,'+categoryid+',%' like '%,'+@cID+'%,'

尝试这个:

确保您将参数传递为'1,2'而不是'(1,2)'

Select * 
from products 
where (proprice between @price1 and @price2) and (CHARINDEX(','+CAST(categoryid as VARCHAR(8000))+ ',', ','+@cID+',')>0)

如果您将参数传递为'(1,2)'

select @cid=REPLACE(@cid,'(','');
select @cid=REPLACE(@cid,')','');

Select * 
from products 
where (proprice between @price1 and @price2) and (CHARINDEX(','+CAST(categoryid AS VARCHAR(8000))+ ',', ','+@cID+',')>0)

暂无
暂无

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

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