如何在where条件中为IN子句传递字符串变量 [英] How can I pass string variable for IN clause in where condition
本文介绍了如何在where条件中为IN子句传递字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我声明了一个字符串变量,将where条件赋予IN子句。但不行。请帮助
I have the declare a string variable to give in where condition as IN clause. But not working . Please can you help
声明depots varchar(max)
declare depots varchar(max)
set depots ='1,2,3,4,5,6,7' ;
set depots='1,2,3,4,5,6,7';
从Depots中选择*其中Id in(depots)//不在这里工作,请帮助我吗?
Select * from Depots where Id in(depots) // Not working here please can you help me ?
polachan
推荐答案
你必须使用字符串解析逻辑
you've to use a string parsing logic for that
如果你的SQL 2016或更高版本你可以使用
If its SQL 2016 or above you can use
declare @depots varchar(max)
set @depots='1,2,3,4,5,6,7';
Select d.* from Depots d
join String_Split(@depots,',') f
on f.Value = d.Id
对于早期版本,您需要为此创建一个udf
for earlier version you would require creating a udf for this
,如
https://visakhm.blogspot.com/2010/02/parsing-delimited-string.html 一>
并使用它像
declare @depots varchar(max)
set @depots='1,2,3,4,5,6,7';
Select d.* from Depots d
join dbo.ParseValues(@depots,',') f
on f.Val = d.Id
这篇关于如何在where条件中为IN子句传递字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文