title: SQL: select all rows if parameter is null, else only select matching rows
body: <p>I have a variable coming into a stored procedure. This variable can either have a value or be null. </p>
  
  <ul>
  <li>If the variable is null, I need to select all the rows in the table (some with NULL values, some with actual data).</li>
  <li>If the variable is not null, I only need to select the rows where the variable matches a column. </li>
  </ul>
  
  <p>I created this conditional statement to help explain what I would like to achieve:</p>
  
  <pre><code>if 
  
  @status is null, select all the rows (rows with NULL for table.status, and rows with actual data for table.status)
  
  else
  
  select the rows where @status equals table.status
  </code></pre>
  
  <p>This is what I came up with (well one of them):</p>
  
  <pre><code>WHERE 
     book.book_nme LIKE @search_input AND
     book.book_desc LIKE @search_input AND 
     (book.author LIKE ISNULL(@author, book.author)) AND
     (bookStatus.status_desc LIKE ISNULL(@status, bookStatus.status_desc))
  </code></pre>
  
  <p>The only problem is that if <code>bookStatus.status_desc</code> is NULL, then it will not select that row (when @status is null)</p>
  
  <p>I'm so confused, I tried looking up Coalesce too which seemed to prioritize the values, but ... I don't know what to do. </p>
  
  <p>Should I just create a huge CASE in the stored procedure and have two select statements?</p>