[英]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.