Passing delimited string to stored procedure to search database

前端 未结 2 1988
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 23:36

How can i pass a string delimited by space or comma to stored procedure and filter result? I\'m trying to do something like -

Parameter      Value
----------         


        
2条回答
  •  抹茶落季
    2021-01-07 00:27

    You could try something like:

    select firstname, lastname from @test t1
    inner join persondata t on t.firstname like '%' + t1.x + '%' or t.lastname like '%' + t1.x + '%'
    group by firstname, lastname
    having count(distinct x) = (select count(*) from @test)
    

    where @test is the table with results of your split. If you have lots of columns in persondata, you might want to just return an ID from this query, and use it as a subquery for the one that actually returns data, so you don't have to group by so many columns.

    Edit: you could also use a cursor and another temp table/table variable, but I have kind of an allergic reaction to cursors in SPs.

提交回复
热议问题