@ a1ex07 gave the answer for this as one request. Using NULL and checking them in each condition.
WHERE table.x = CASE WHEN @x IS NULL THEN table.x ELSE @x END
or...
WHERE (@x IS NULL OR table.x = @x)
or...
WHERE table.x = COALESCE(@x, table.x)
etc. etc.
However, there is one warning; How convenient it is to make one request, to do all this, all these answers are suboptimal. Often they are boring.
When you write ONE request, only one execution plan is created. And this ONE execution plan should be suitable for ALL possible combinations of values. But this fixes which indexes are being looked up, what order they are looking for, etc. This gives the worst plan for requesting one size for all.
Instead, you better add conditions as needed. You still parameterize them, but you do not include the condition if you know that the parameter is NULL.
This is a good link explaining this further, it is specifically for MS SQL Server, but it is usually applied to any DBMS that caches plans after compiling SQL.
http://www.sommarskog.se/dyn-search.html
source share