How to ignore a parameter in stored procedure if its value is null

后端 未结 3 1360
遥遥无期
遥遥无期 2021-01-17 03:24

I have a stored procedure in which I have to join 10 tables and use WHERE condition to filter the records based on the parameters passed in the stored procedure

3条回答
  •  执念已碎
    2021-01-17 04:08

    You just need to give the parameters which are being used.

    If only value for Var1 needs to be passed.

    proc1 @Var1 = Var1Value
    

    If the value for Var6 needs to be passed.

    Proc1 @Var6 = Var6Value
    

    Please use case in this like

     where case @var1 when null then 1 else  a.id end =  case @var1 when null then 1 else  @var1 end 
    

    Hope this will help you Thanks

提交回复
热议问题