我在参数上收到一个proc:
exec do_something [a,b,c]
我需要进行如下查询:
select * from B where b not in ("a","b","c")
如何进行此转换?
答案 0 :(得分:1)
快速/简单的解决方案是使用str_replace()
函数重新格式化输入参数,然后使用所述修改参数构建动态查询,例如:
declare @param varchar(100),
@query varchar(1000)
select @param = 'a,b,c'
select @param = '"' + str_replace(@param,',','","') + '"'
print "@param: %1!",@param
select @query = 'select * from B where b not in (' + @param + ')'
print "@query: %1!",@query
exec(@query)
go
@param: "a","b","c"
@query: select * from B where b not in ("a","b","c")
.... results of running query .....