我在写这个查询时有很大的时间问题。如何根据sql中的输入参数过滤行?
如果我的样本数据看起来像这样...
id Fruit name Group Type
-----------------------------------------
1 Orange Citrus null
2 Mango Sweet null
3 Red chilly Hot null
4 Green chilly Hot null
5 Green chilly Hot Organic 1
6 Green chilly Hot Organic 2
我想创建它接受@FoodType
参数的存储过程。
- 当
@FoodType
作为NULL
通过,SP应该返回行1, 2, 3
和4
。 - 如果
@FoodType
是Organic 2
,那么SP应该返回1, 2, 3
和6
。
在我的桌子,FruitName
和Type
列可以潜在地使复合唯一键,但我没有,虽然创建一个。
写这种查询的最佳方法是什么?
更新:
当“有机2”通过时,因为类型没有为4行所定义,行6的优先级高于第4行
也许'NULL'可以是任何类型的。 – Ambrose 2012-07-23 05:24:58
我认为他想在'有机2'通过时返回1,2,3,4和6 .. – 2012-07-23 05:25:15
@marc_s - 如果他想要得到与他们输入相匹配的所有东西并且否则为空,这是有道理的 – dfb 2012-07-23 05:35:20