简体   繁体   English

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

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

I have a CASE expression and for one of the WHEN clauses, I'm selecting data from a table and checking if it exists.我有一个CASE表达式,对于其中一个WHEN子句,我从表中选择数据并检查它是否存在。 If it exists, I want to return one of the columns from that row in the THEN expressions如果存在,我想在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

The issue I have though is SQL MS returns an error message under the b.value stating it cannot be bound.我遇到的问题是 SQL MS 在 b.value 下返回一条错误消息,说明它无法绑定。 Is this because the actual referenced table is in a when statement and how would I be able to get around this if it's possible?这是因为实际引用的表在 when 语句中,如果可能的话,我将如何解决这个问题?

The WHEN clause of the CASE expression has its own scope, so the subqueries you have executed there are inaccessible to the THEN clause. CASE表达式的WHEN子句有自己的 scope,因此您在那里执行的子查询无法访问THEN子句。

Looking at it another way, the CASE expression forms part of the SELECT list, so any field referenced in it must appear in the FROM clause.从另一个角度来看, CASE表达式 forms 是SELECT列表的一部分,因此其中引用的任何字段都必须出现在FROM子句中。

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

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