Sunday, August 9, 2009

Optional Parameter Query

I've been working on a SQL Query with optional parameters that returns all records if no parameter(s) are sent, or narrows the query by the parameter(s) if sent.

The key line is in the where clause and looks like:

WHERE COALESCE(MT.Name, '') like COALESCE(@ProductName, MT.Name, '')

Which has the effect : If @ProductName not null then match MT.Name to itself(return all records) and if MT.Name is null match '' to itself ( because Null = Null does not return true in sql)

No comments:

Post a Comment