How do I use a comma separated list of values as a filter in T-SQL?

后端 未结 10 1512
無奈伤痛
無奈伤痛 2021-02-09 14:03

I have a basic SQL query, starting with:

SELECT top 20 application_id, [name], location_id FROM apps

Now, I would like to finish it so that it

10条回答
  •  孤城傲影
    2021-02-09 14:23

    You are asking different questions here. This is the answer to your original question (about the ternary operator, but as you can see, you don't need anything like a ternary operator for this):

    SELECT top 20 application_id, [name], location_id 
    FROM apps
    WHERE @lid = 0 OR location_id IN (@lid)
    

    But that was before we knew that @lid was a varchar and could contain different comma separated values.

    Well, this (about the csv) is another question which has been asked here before:

    Passing an "in" list via stored procedure
    T-SQL stored procedure that accepts multiple Id values

提交回复
热议问题