I have a variable coming into a stored procedure. This variable can either have a value or be null.
- If the variable is null, I need to select all the rows in the table (some with NULL values, some with actual data).
- If the variable is not null, I only need to select the rows where the variable matches a column.
I created this conditional statement to help explain what I would like to achieve:
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
This is what I came up with (well one of them):
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))
The only problem is that if bookStatus.status_desc
is NULL, then it will not select that row (when @status is null)
I'm so confused, I tried looking up Coalesce too which seemed to prioritize the values, but ... I don't know what to do.
Should I just create a huge CASE in the stored procedure and have two select statements?