0

我有这个查询

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

4

2 回答 2

1

ACASE是一个标量表达式,它必须计算为一个标量值。您不能使用它来返回多行集。

我见过的根据输入值是否为 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 ) )
    )
于 2012-08-01T12:10:07.303 回答
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
)
于 2012-08-01T12:43:53.447 回答