T-SQL - How to write a conditional join

后端 未结 9 1955
轻奢々
轻奢々 2021-01-30 13:07

I have a stored procedure with a number of parameters. I would like to write my query so that it joins with certain tables but only if a particular parameter has a value. Take

9条回答
  •  误落风尘
    2021-01-30 13:43

    Yes, it's very simple. Do left joins on the address and groups. Then in the where clause...

    (@group_id is null or g.group_id = @group_id)
    and (@address_id is null or a.address_id = @address_id)
    

提交回复
热议问题