title: Building where clause based on null/not null parameters in sql
body: <p>I am trying to create a stored procedure which will return records based on input. If all the input parameters are null, then return the entire table, else use the parameters and return the records:</p>
  
  <pre><code> create procedure getRecords @parm1 varchar(10) = null, @parm2 varchar(10) = null, @parm3 varchar(10) = null
   as
   declare @whereClause varchar(500)
   set @whereClause = ' where 1 = 1 '
  
   if (@parm1 is null and @parm2 is null and @parm3 is null)
      select * from dummyTable
   else
      begin
        if (@parm1 is not null)
           set @whereClause += 'and parm1 = ' + '' + @parm1 + ''
        if (@parm2 is not null)
           set @whereClause += 'and parm2 = ' + '' + @parm2 + ''
        if (@parm3 is not null)
           set @whereClause += 'and parm3 = ' + '' + @parm3 + ''
  
        select * from dummyTable @whereClause  &lt;-- Error 
      end
  </code></pre>
  
  <p>Error while creating this procedure is "An expression of non-boolean type specified in a context where a condition"</p>
  
  <p>Please comment if my approach is wrong in building the where clause?</p>
  
  <p>Thanks</p>