Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
soulice
Starting Member
4 Posts |
Posted - 2012-08-17 : 19:01:11
|
I have a nasty query, some cursors and the like. In the where clause is a "WHERE ... and status = @status" which is passed in as an INT parameter. A drop down on a search page allows for this status choice. Now my users want an "ALL STATUSES" option in that drop down. I considered a dynamic sql query bu the idea of a 5 different string for queries running though a cursor and following table building is, well, not sunshiny. Thoughts!-Soulice |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-08-17 : 19:57:33
|
These do get nasty and they do so quickly. I can easily imagine the end users asking to be able to check a few options; not just one or all.If it truly is a "one or all" you could modify your WHERE clause to be "where (@status = status or @Status is null)". If performance is impacted by the OR condition, you you could add an IF logic to your routine, a la[CODE]if @status is not NULLbegin <original select and where clause>endelsebegin <original select statement but without the status test>end[/CODE]If your users do want to have the option of checking multiple statuses, you might pass them in as a CSV string that gets split and loaded into a temp table. From there you can either JOIN to that table or use it as part of your IN predicate.HTH=================================================We are far more concerned about the desecration of the flag than we are about the desecration of our land. -Wendell Berry |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-17 : 20:01:57
|
or use a catch all approach. set a default value like 'All' for all statuses and send it to query change the where condition likeWHERE ... and (status = @status OR @status='All')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
soulice
Starting Member
4 Posts |
Posted - 2012-08-17 : 21:27:46
|
Would this be valid where (status = @status or @status = 0) with 0 denoting "all" (since @status is an int) ??-Soulice |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-17 : 21:29:06
|
quote: Originally posted by soulice Would this be valid where (status = @status or @status = 0) with 0 denoting "all" (since @status is an int) ??-Soulice
yep..thats also an option. only thing is you need to set 0 as value for all statuses option in application code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
soulice
Starting Member
4 Posts |
Posted - 2012-08-19 : 18:40:08
|
thanks to both Bustaz Kool and visakh16...worked like a charm!-Soulice |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-19 : 21:30:57
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|