Many times, we come across a need of writing different SQL SELECT queries in a stored procedure, just to seperate the WHERE clause logic depending upon if the parameter value is passed or not.
Lets consider following case -
Your stored procedure sp_GetCities has a parameter @CountryID.
Now, if the parameter is not null then you want to filter Cities based on passed CountryID.
But, if the parameter is Null then you need to return all cities irrespective of their country.
On many occasions, we write following logic in stored proc.
IF @CountryID is null // Or, IF ISNULL(@CountryID, 0) <= 0
BEGIN
SELECT ....
---No WHERE condition to filter countries-----
END
ELSE
BEGIN
SELECT ....
WHERE CountryID = @CountryID
END
There is one workaround to avoid writing multiple SELECT queries in similar situations.
Use of CASE..WHEN in SELECT statement
SELECT city_id, city_name
FROM tblCities
WHERE
1 = CASE WHEN ISNULL(@country_id, 0) <= 0 THEN 1 ELSE CASE WHEN @country_id = country_id THEN 1 ELSE 0 END END
Let us understand the WHERE clause -
If the right-hand side of the expression returns 1 then the city will be returned in resultset, otherwise it wont be.
If "@country_id" is not passed i.e. ISNULL(@country_id, 0), then obviously it will return 1. And in this case, all cities will be selected, as 1=1 is always true.
If "@country_id" is passed then if it matches with city's country_id (i.e., country_id = @country_id) then it will return 1, otherwise it will be 0.
We can use this upto any level, so we can definitely reduce number of SELECT queries in our code.
Wednesday, April 11, 2012
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2012
(49)
-
▼
April
(24)
- Get only Date part for SQL Server DateTime value
- Membership in ASP.NET MVC 4 (System.Web.Providers)
- Binding two or more types of objects (models) to s...
- Implement Custom Validation in ASP.NET MVC
- Simple client-side Validations using ASP.NET MVC
- Investigation: Values do not retain in model while...
- Data Binding to RadioButton in a View in MVC
- Investigation: SelectedIndexChanged not firing for...
- Investigation: ExecuteNonQuery() always returns -1
- Development and Deployment - Troubleshooting, Inve...
- Print Page Content to printer (or virtual print ou...
- Show long text in Tooltip
- Auto login into Team system with your authorized c...
- Visual Studio .NET - Some handy shortcuts
- Write custom events for User Controls in ASP.NET
- SQL Query to return each date of month in each dif...
- How to debug ASP.NET Windows Service
- FIX: Add a 32-bit SQL Server 2000 as a Linked serv...
- Difference between integer Cast and Convert in C#
- Optimizing SQL Code by replacing IF..ELSE with CAS...
- Check Authentication Mode in ASP.NET
- How to validate a page using Java script
- SQL Query - To search for a column (by column name...
- C#.NET - Difference between Convert.ToString(), .T...
-
▼
April
(24)
No comments:
Post a Comment
Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.
Thanks
Nirman