I've researched this scenario for a few hours now but cannot seem to find anything that addresses it directly.
I have lots of optional parameters passed into a stored proc. I want to return a recordset that is filtered by none, one or more of the optional params passed into the stored proc.
I'm trying to produce a single query to do this.
Here's my pseudo code (SQL Server 2005):
declare @a varchar(10); set @a = null;
declare @b varchar(10); set @b = 'comm%';
select * from x
where (if @a is not null then col1 like @a)
and (if @b is not null then col2 like @b)
As you can see I only want to filter on col1 if my param (@a) has data. Likewise I only want to filter on col2 if my param (@b) has data. If @a and @b are both NULL then the query should return all rows.
I've found several threads that come close to what I want, but none of them address using the LIKE clause.