title: T-SQL Where int=@var / @var=null?
body: <p>I am having a situation where I call a stored procedure and pass in some variables.  In some cases i will be passing in null and I want it to return everything.  Take the following (age is an Integer column):  </p>
  
  <pre><code>[Table]  
  [Name] | [Age]  
  Mike   | 22  
  Fred   | 18  
  Bob    | 22 
  
  
  
  SELECT * FROM [table] WHERE [Age]=@AgeVar
  </code></pre>
  
  <p>Now if I pass in 22 I will get Mike and Bob. Similarly passing in 18 will get me Fred.  However, What if I want all 3 rows when I pass in null/don't set @AgeVar?  This is my issue on a very simple scale.</p>
  
  <p>Edit: The Sproc is pretty complex and i don't really want to double all the sections up by having to but them twice as in one of the answers below. Are there any other methods?</p>