title: SQL Server stored procedure dynamic SQL in WHERE clause
body: <p>I have written a stored procedure that accepts one parameter.  </p>
  
  <pre><code>Request_ID int = 0
  </code></pre>
  
  <p>Later, in my <code>WHERE</code> clause, I need to accomplish the following.  </p>
  
  <p>I don't know how to describe it, but you can see what I'm trying to do with this completely made up  pseudo-code:</p>
  
  <pre><code>WHERE isNull(a.Target_Department, '') &lt;&gt; ''
    AND isNull(a.Resolved_Date, '') = ''
    AND isNull(c.Request_Archived, '') &lt;&gt; 'Y'
    AND IF @Request_ID = 0 
          THEN Request_ID &gt; 0 
          ELSE Request_ID = @Request_ID
      END
  </code></pre>
  
  <p>Basically, if a specific <code>request_ID</code> is supplied, I want the <code>WHERE</code> clause to narrow to only that specific <code>Request_ID</code>. Otherwise, I want all of them.</p>
  
  <p>Any ideas on how to accomplish this?</p>