Implementing Dynamic WHERE-Clause in Static SQL
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 speed and performance where as the static SQL is checked for syntax error, parsed, compiled and the execution plan is stored in the SQL Server's cache for subsequent execution. As I mentioned in my previous article as a special note, Dynamic SQL Queries in a variable are not compiled, parsed, checked for errors until they are executed. It is impossible for the SQL Server to reuse the execution plans produced by the dynamic SQL statement. When performance is the top priority, then one should avoid using Dynamic SQL statement. Speed, performance and reusability are the known issues with Dynamic SQL. Here I would like to show few ways of Implementing Dynamic WHERE -Clause in Static SQL. I take pleasure in writing this article which would force you to think of a better alternative rather jumping-in to write Dynamic SQL. And to those who read this, I suggest you to have a look at my previous article for better understanding. It's always an advantage to know the details of both the methods which will help you to choose a more efficient way.
Implementing Dynamic WHERE-Clause
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 WHERE -clause. Before we get into an example, Let me explain how this " COALESCE Function" works.
Basic Syntax: COALESCE()
COALESCE function accepts multiple expressions as arguments and processes the expression list from left to right. This function returns the first Non-Null expression in the expression list. If all arguments are NULL. it returns NULL.
Note. All expressions must be of the same type or must be implicitly convertible to the same type.
Let us take an example - Employee table with common fields and wrap the statements in a stored procedure. Following Transact-SQL CREATE TABLE statement is to create an Employees table within your database.
The following INSERT statements insert some sample records into the tblEmployee table:
The below written stored procedure (Example 2.1) can be used to search for the following details in the Employee ( tblEmployees ) table.
- Search for specific Employee detail with the Name .
- List of Employees in a specific Department .
- List of Employees in a specific Designation .
- List of Employees joined the organization last year.
- List of Employees whose Salary >= some specific Amount.
- Any of these conditions listed above or all of these.
The SELECT query in the stored procedure dynamically implements the WHERE -Clause using COALESCE function to get the desired result.
Example 2.1 - Using COALESCE
In the above stored procedure, for each condition in the WHERE -clause, the COALESCE function picks the first non-null value and uses it for the comparison operation. If the input parameter value is NULL. then the coalesce function returns the actual value which equals itself. The causes the particular row to be returned for that operation.
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()
Note. replacement_value must have the same type as check_expresssion.
Let's take the above example and write the stored procedure that builds the WHERE -clause dynamically using the ISNULL function.
Example 2.2 - Using ISNULL
You can see in example 2.2 - WHERE -clause is built dynamically using the ISNULL function. It evaluates the expression and checks whether the parameter value is NULL or not. When this check expression returns a Non-Null value, it uses the parameter value in the comparison operation. When the check expression returns null. it uses the current value which equals itself and that causes all the rows to be returned for that operation.
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 are two types of CASE functions:
- Simple CASE
- Searched CASE
The simple CASE function compares the input_expression with the when_expression to get the desired result_expression. The searched CASE function evaluates a set of Boolean expression to get the desired result_expression. Let's take the same example and write the stored procedure that builds the WHERE -clause dynamically using the CASE function.
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.Source: www.codeproject.com