开发者

sql query for nullable number greater than parameter

开发者 https://www.devze.com 2023-01-10 03:38 出处:网络
Simple table: create table Items ( Price money null ) Now I need to create a stored procedure that accepts one paramter of type bit @ItemsWithPriceTenDollarsOrMore which:

Simple table:

create table Items
(
  Price money null
)

Now I need to create a stored procedure that accepts one paramter of type bit @ItemsWithPriceTenDollarsOrMore which:

  1. returns all items if parameter is null
  2. returns all items with Price >=10 if parameter = 1
  3. returns all开发者_StackOverflow中文版 items with Price < 10 if parameter = 0

I have difficulty expressing this filter in a single where statement (not using dynamic sql or conditional logic).


Try this one:

SELECT * FROM Items
WHERE (@ItemsWithPriceTenDollarsOrMore = 1 AND Price >=10)
OR (@ItemsWithPriceTenDollarsOrMore = 0 AND Price <10)
OR (@ItemsWithPriceTenDollarsOrMore IS NULL)
0

精彩评论

暂无评论...
验证码 换一张
取 消