将充满逗号分隔值的varchar传递给SQLServer IN函数我有一个SQLServer存储过程,我想在其中传递一个varchar中充满逗号分隔的值。IN功能。例如:DECLARE @Ids varchar(50);SET @Ids = '1,2,3,5,4,6,7,98,234';SELECT * FROM sometable
WHERE tableid IN (@Ids);当然,这是行不通的。我知道错误:将varchar值‘1,2,3,5,4,6,7,98,234’转换为int数据类型时,转换失败。我如何才能做到这一点(或类似的事情)而不诉诸于构建动态SQL?
3 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
Declare @Ids varchar(50) Set @Ids = ',1,2,3,5,4,6,7,98,234,'Select * from sometable where Charindex(','+cast(tableid as varchar(8000))+',', @Ids) > 0
跃然一笑
TA贡献1826条经验 获得超6个赞
没有表,没有函数,没有循环
Declare @Ids varchar(50)Set @Ids = ‘1,2,3,5,4,6,7,98,234’DECLARE @XML XMLSET @XML = CAST('<i>' + REPLACE(@Ids, ',', '</i><i>') + '</i>' AS XML)SELECT * FROM SomeTable INNER JOIN @XML.nodes('i') x(i) ON SomeTable .Id = x.i.value('.', 'VARCHAR(MAX)')
添加回答
举报
0/150
提交
取消