【问题标题】:using a searched case in where condition在 where 条件下使用搜索过的案例
【发布时间】:2014-11-04 22:00:58
【问题描述】:

我最近得到了这个存储过程,它使用了一个名为@marketCode 的变量。应用程序正在为此变量传递 South 或 null。

在我的选择语句中,如果应用程序通过 null,我想检索除 South 以外的所有市场。

and marketCode =! 'South'

如果应用程序传递了一个非空值,那么我将使用那个值。

and marketCode = 'North'

如何将这两个合并为一个案例?

 AND marketCode = (case @marketCode when null then <..not equal to 'South'..>
                                            else <..equal to 'South'..> end )

【问题讨论】:

    标签: sql tsql case


    【解决方案1】:

    您应该可以使用OR 条件来执行此操作:

    ...
    AND (
        (@marketCode IS NULL AND marketCode <> 'South')
      OR
        (@marketCode IS NOT NULL AND marketCode = @marketCode)
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      相关资源
      最近更新 更多