title: SQL Server : Where clause inside IF .. ELSE
body: <p>Like the title already explained, I'm struggling with my <code>WHERE</code> clause in my SQL stored procedure.</p>
  
  <p>I got a <code>SELECT</code> query which joins multiple tables well and at the end if got a <code>WHERE</code> clause that give specific values to search for.</p>
  
  <p>My problem is that I want to expand this stored procedure for 2 different <code>WHERE</code> clauses, but I can't get my <code>IF ELSE</code> correct to parse the query..</p>
  
  <p>For example:</p>
  
  <pre><code>SELECT ....... 
  FROM TABLE_X
  INNER JOIN TABLE_Y.....
  WHERE 
      man.Klant_ID = @Klant
      AND (@ManID = 0 OR man.ID = @ManID)
      AND .... (which continues like the rule above)
  </code></pre>
  
  <p>Here I want to get something like this:</p>
  
  <pre><code>SELECT ....... 
  FROM TABLE_X
  INNER JOIN TABLE_Y.....
  
  IF @TEMPVAR = ''
      WHERE man.Klant_ID=@Klant
      AND (@ManID = 0 OR man.ID = @ManID)
      AND... 
  ELSE
      WHERE TABLE_X.ID IN (@TEMPVAR)
  </code></pre>
  
  <p>(and <code>@tempvar</code> should contain comma separated id's like <code>10001,10002,10003</code>)</p>
  
  <p>I'm struggling with the syntax and searched for some while but can't seem to find a right solution.</p>
  
  <p>Thanks in advance!</p>