title: Parameters table and conditions
body: <p>In my current project there is a query where a set of parameters is given and I need to check those parameters against another table. Each of these parameters can be NULL and in this case has to be ignored. What I currently do is the following:</p>
  
  <pre><code>SELECT  t.col1,
          t.col2,
          t.col3,
          t.col4,
          t.col5,
          t.col6,
          t.col7,
          t.col8
  FROM table1 t
  INNER JOIN #parameters p ON     (p.col1 IS NULL OR p.col1 = t.col1)
                              AND (p.col2 IS NULL OR p.col2 = t.col2)
                              AND (p.col3 IS NULL OR p.col3 = t.col3) 
                              AND (p.col4 IS NULL OR p.col4 = t.col4)
                              AND (p.col5 IS NULL OR p.col5 = t.col5)
                              AND (p.col6 IS NULL OR p.col6 = t.col6)
                              AND (p.col7 IS NULL OR b.col7 &gt;= t.col7)
                              AND (p.col8 IS NULL OR b.col8 &lt;= t.col8)
  </code></pre>
  
  <p>This means if the column in the parameters table is NULL it will be ignored otherwise it will be compared to the corresponding column in table1. This works but unfortunately is VERY slow. Does anybody know a better solution (other then concatenating a string query)?</p>