| 
	
 | 
 Posted by Jeff Mason on 04/27/06 18:10 
Hi, 
 
I'm trying to construct a query (in a stored procedure) which will have a number of 
selection criteria based on input parameters.  There are a number of these parameters 
whose selection conditions they represent which all have to be true for a row to be 
returned in the resultset. 
 
The basic query is: 
 
	SELECT Store, StoreNumber 
	  FROM Stores 
	 WHERE ... 
 
I'm trying to come up with the WHERE clause. 
 
For example, I want to define a parameter named @ExcludeSpecialties which if it has 
the value 1, means to return all stores but exclude stores whose StoreNumber is in 
the list (800, 802, 804).  If the parameter has the value 0, then it means "don't 
care" and all StoreNumbers should be returned. 
 
One could certainly argue that there probably should have been an column in the 
Stores row to indicate the store is a specialty store, rather than using a hard-wired 
list of numbers.  But the current data schema cannot be easily changed.  Besides, the 
list never changes. 
 
Indeed, there is a Franchise bit column in the row which is selected by another 
parameter called @ExcludeFranchise whose WHERE predicate could be written as: 
 
	WHERE Franchise = CASE WHEN @ExcludeFranchise = 1 THEN 0 ELSE Franchise END 
 
and if all the parameters were like this, I wouldn't be posting.  Sadly, for the 
Specialties test I'm stuck with a NOT IN list. 
 
This is easy enough to do in an IF/ELSE block, but there are several such similar 
parameters whose values may be specified in any combination.  This, I think, makes 
IF/ELSE impractical as the number of IF/ELSE statements to handle all possible 
combinations would grow very quickly. 
 
I'm hoping there is a simple solution to this NOT IN list, and it's just that I can't 
see it. 
 
Can anyone help? 
 
Thanks, 
 
  -- Jeff
 
  
Navigation:
[Reply to this message] 
 |