SQL do inner join if condition met

前端 未结 3 2088
渐次进展
渐次进展 2020-12-17 15:53

i want a good way to improve my sql code, i have to use inner join when condition is met. I am currently replicates the code:

@SystemMerge bit

if (@SystemMe         


        
3条回答
  •  醉酒成梦
    2020-12-17 16:33

    This should (approxmately) do the same thing:

    SELECT
         .......
      FROM myTable
      INNER JOIN table ON table.param1=myTable.param1
      LEFT JOIN systemTable on systemTable.param2=myTable.param2 and @SystemMerge = 1
      WHERE (@SystemMerge = 0 OR systemTable.NonNullableColumn IS NOT NULL)
    

    Of course, this also means that any other references to columns within systemTable must be written to expect such columns to be NULL.

提交回复
热议问题