代码之家  ›  专栏  ›  技术社区  ›  Valentin V

大于参数的可空数的sql查询

  •  3
  • Valentin V  · 技术社区  · 15 年前

    简单表格:

    create table Items
    (
      Price money null
    )
    

    一点 @ItemsWithPriceTenDollarsOrMore

    1. 如果参数为null,则返回所有项
    2. 返回所有价格>=参数=1时为10
    3. 返回包含价格的所有项目<如果参数=0,则为10

    我很难在一个where语句中表达这个过滤器(不使用动态sql或条件逻辑)。

    1 回复  |  直到 15 年前
        1
  •  5
  •   Pavel Morshenyuk    15 年前

    试试这个:

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