Implementing Dynamic
WHERE-Clause in Static SQL
John-ph, 8 Nov 2007
CPOL
Contents
Introduction
Implementing Dynamic WHERE-Clause
Using COALESCE
Using ISNULL
Using CASE
Alternative
Conclusion
References
Introduction
My previous article was about Building Dynamic SQL In a
Stored Procedure. I explained how to write and execute
Dynamic SQL using sp_executesql command. Well, when
we take a look at the query execution plan of these two
methods (static and dynamic); there is a huge difference in the
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
1/9
12/11/2014
Implementing Dynamic
WHERE-Clause
Using COALESCE
The COALESCE Function in SQL Server serves a better
purpose in handling columns with NULL values. It helps us to
take a more efficient approach in building dynamic WHEREclause. Before we get into an example, Let me explain how
this "COALESCE Function" works.
Basic Syntax: COALESCE()
COALESCE ( expression1 , expression2, expression3....n)
2/9
12/11/2014
3/9
12/11/2014
Set NoCount ON
Using ISNULL
ISNULL is a T-SQL System function used to handle NULL
values, it takes two argument, the first one is to check for an
expression and the second argument is for the replacement
value if the check expression value is NULL. We can say that
ISNULL is equivalent to COALESCE function with two
arguments.
Basic Syntax : ISNULL()
ISNULL ( check_expression , replacement_value )
4/9
12/11/2014
check_expresssion.
Let's take the above example and write the stored procedure
that builds the WHERE-clause dynamically using the ISNULL
function.
Using CASE
CASE function is equivalent to the COALESCE function in SQL
Server. It evaluates a list of conditions and returns one result
expression from multiple possible result expressions. There
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
5/9
12/11/2014
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
6/9
12/11/2014
Alternative
Here is an alternative suggested by a CodeProject member in
the article discussion of my first article. This alternative uses
neither COALESCE nor CASE function to build Dynamic
WHERE-clause but a logic equivalent to it - worth using it.
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
7/9
12/11/2014
Conclusion
Hope you learned how to implement dynamic WHERE-Clause
in static SQL. In all the examples, I showed how to handle the
input parameter when it is NULL. Even if its a different NonNull value to be used for multiple comparisons, you can still
play around with simple CASE and searched CASE-functions
more efficiently. Hence I conclude that static SQLs are faster,
safer and in most cases, we don't need to use dynamic SQLstatement.
Reference
MSDN (T-Sql)- Coalesce, IsNull, CASE
History
8th November, 2007: Initial post
License
This article, along with any associated source code and files, is
licensed under The Code Project Open License (CPOL)
Share
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
8/9
12/11/2014
John-ph
Software Developer (Senior)
India
Select Language
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL?display=Print
9/9