我有这个查询
select devlocview.ENTERPRISENAME
FROM AlarmDeviceView alrmDevView
INNER JOIN devicelocationview devLocView
ON devlocview.oid=alrmdevview.deviceoid
LEFT OUTER JOIN DEVICEGROUPVIEW devGrpView
ON devlocview.oid=devgrpview.deviceoid
LEFT OUTER JOIN RackCapacityView rackCapView
ON devlocview.RACKID=rackCapView.oid
WHERE
(
alrmdevview.createddate = ( select max(alview.createddate)
from alarmdeviceview alview
WHERE alview.alarmoid=alrmdevview.alarmoid)
or alrmdevview.createddate IS NULL
)
AND devlocview.ENTERPRISENAME='Enterprise1'
AND devlocview.buildingname in (
CASE
WHEN $p{building} IS not NULL THEN $p{building}
WHEN $p{building} IS NULL THEN (select buildingname from cdmr.devicelocationview)
END
)
此部分"select buildingname from cdmr.devicelocationview"
返回多行,因为我正在获取
异常为01427. 00000 - “单行子查询返回的值超过 一行“
根据要求,我想在$p{building} is null
答案 0 :(得分:1)
CASE
是标量表达式,必须求值为标量值。您不能使用它来返回多行集。
我根据输入值是否为NULL来改变条件的最常见习语就像你的例子一样:
AND ( ( $p{building} IS NOT NULL AND devlocview.buildingname = $p{building} )
OR
( $p{building IS NULL AND devlocview.buildingname IN (SELECT buildingname FROM cdmr.devicelocationview ) )
)
答案 1 :(得分:1)
试
AND 1 = case
when $p{building} IS not NULL
and devlocview.buildingname = $p{building}
then 1
when $p{building} IS NULL
AND devlocview.buildingname in (select buildingname from cdmr.devicelocationview)
then 1
else 0
end
而不是
when devlocview.buildingname in (
CASE
WHEN $p{building} IS not NULL THEN $p{building}
WHEN $p{building} IS NULL THEN (select buildingname from cdmr.devicelocationview)
END
)