简单表格:
create table Items
(
Price money null
)
现在我需要创建一个存储过程,它接受一个类型为 bit @ItemsWithPriceTenDollarsOrMore
的参数,其中:
- 如果参数为空则返回所有项目
- 如果参数 = 1,则返回价格 >=10 的所有项目
- 如果参数 = 0,则返回价格 < 10 的所有项目
我很难在单个 where 语句中表达这个过滤器(不使用动态 sql 或条件逻辑)。
最佳答案
试试这个:
SELECT * FROM Items
WHERE (@ItemsWithPriceTenDollarsOrMore = 1 AND Price >=10)
OR (@ItemsWithPriceTenDollarsOrMore = 0 AND Price <10)
OR (@ItemsWithPriceTenDollarsOrMore IS NULL)
关于大于参数的可空数字的sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3414820/