我是 sql server 的新手,有如下需求,
需要从数据库中选择记录列表,使用 case when 语句。例如;
select * from test where test.id in (case when @id = '' then test.id else @id end);
这里@id 将是这样的逗号分隔值 (123,124,125,126)。
如果我只传递一个值,即。@id = 123。它工作正常,但在传递元素数组时失败。任何人都可以提出一些解决方案。
尝试以下方法。
select * from test where @id = '' OR ',' + @id LIKE '%,' + test.id + '%'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句