繁体   English   中英

尝试在表达式作为结果的情况下使用条件

[英]Trying to use the condition in a case when expression as the result

我有一个CASE表达式,对于其中一个WHEN子句,我从表中选择数据并检查它是否存在。 如果存在,我想在THEN表达式中返回该行中的一列

case 
    --a few when statements
    when name = 'aiden'
    THEN
        case
            when (select top 1 * from tableA a where [list of conditions]) = 1 and  (select top 1 value from tableB b where [list of conditions]) = 1
            THEN b.value -- this lines throwing "the multi-part identifier "b.value" could not be bound."
        else
            --do some existing logic
    Else
    --other logic
end as data

我遇到的问题是 SQL MS 在 b.value 下返回一条错误消息,说明它无法绑定。 这是因为实际引用的表在 when 语句中,如果可能的话,我将如何解决这个问题?

CASE表达式的WHEN子句有自己的 scope,因此您在那里执行的子查询无法访问THEN子句。

从另一个角度来看, CASE表达式 forms 是SELECT列表的一部分,因此其中引用的任何字段都必须出现在FROM子句中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM