title: JOIN with table only if parameter condition is met
body: <p>I need to do a join with table B only if a parameter @param is not null or if it has a specific value.</p>
  
  <p>I don't want to add IF/ELSE because the store is really big for doing that.</p>
  
  <p>My store return over 50 parameters and makes joins with 10 tables.</p>
  
  <p>I just want to skip one of those joins if a declared @param condition is meet.</p>
  
  <p>I hope someone can help me.
  Thanks in advance!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>DECLARE @param INT = NULL;
  
  SELECT 
      /* HERE GOES A LOOT OF PARAMETERS */
  FROM 
      T0 T
      INNER JOIN  T1 B ON T.attr1 = B.attr1
      LEFT JOIN   T2 TFD ON T.attr2 = TFD.attr2
      LEFT JOIN   T3 TTT ON (TTT.attr3 = TFD.attr3)
      INNER JOIN  T4 U ON B.attr4 = U.attr5 AND U.attr6 = @attr6
      -- The following commented section is the one that i want to skip if @param is NULL
      /*LEFT JOIN @tvp P ON  (
                              (P.attr11= TFD.attr9 AND T.attr8 = P.attr8 AND P.attr7 = 1) OR 
                              (P.attr11 = TFD.attr9 AND T.attr8 = P.attr8 AND P.attr7 = 0 AND B.attr4 = @attr5) OR 
                              (TFD.attr9 IS NULL AND P.attr8 = T.attr8)
                                      )*/
      LEFT JOIN   T4 FTB ON FTB.attr1 = B.attr1
      LEFT JOIN   T6 S ON S.attr10 = T.attr10
      LEFT JOIN   @splittedElementsIds SETI ON SETI.item = T.attr9
      WHERE
          (@attr9 IS NULL OR T.attr9 = SETI.item)
      GROUP BY
          /* LOOTS OF GROUP BY PARAMETERS */
  </code></pre>