How to pass lists as variables into where in clause (SQL Server)

混江龙づ霸主 提交于 2020-01-16 08:57:08

问题


I am trying to make a query like this dynamic using variables in SQL Server.

Original Query (returns results)

select
*
from items
where [key] in ('material', 'type') 
and value in ('nylon/latex', 'general purpose')

New Query (returns empty set)

declare @keys nvarchar(max) = 'material, type'
declare @values nvarchar(max) = 'nylon/latex, general purpose'

select
*
from items
where [key] in (@keys) 
and value in (@values)

How can I pass CSV data into these in clauses dynamically?

来源:https://stackoverflow.com/questions/58774178/how-to-pass-lists-as-variables-into-where-in-clause-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!