【问题标题】:How to make a variable in the where condition represent all values?如何使 where 条件下的变量代表所有值?
【发布时间】:2015-04-27 20:42:24
【问题描述】:

代码:

declare @fruit varchar(70)
set @fruit='apple'
select * from supermarket where fruitType=@fruit

如果变量@fruit 设置为'ALL',我该怎么做才能使select 语句检索表中的所有值?

【问题讨论】:

    标签: sql birt


    【解决方案1】:

    如果你这样写查询:

    select * 
    from supermarket 
    where fruitType=@fruit OR @fruit = 'ALL'
    

    然后,如果@fruit 等于'ALL'WHERE 子句的计算结果为真。

    【讨论】:

      【解决方案2】:

      您可以明确检查ALL 值:

      SELECT * 
      FROM   supermarket 
      WHERE  @fruit IN ('ALL', fruitType)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2021-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多