oracle 单行子查询在sql中返回多行

oxosxuxt  于 2023-08-04  发布在  Oracle
关注(0)|答案(2)|浏览(197)
select A.REGION  ,
       E.APPLY_ID    ,
       E.JOB_ID      , 
       E.LOGIN_ID    ,
       J.TITLE      
  from EMPLOYEE_JOBS E  ,EMPLOYEE_JOB_LIST  J ,EMPLOYEE_APP A ,COUNTRY C , LOV_MISC L ,LOV_MISC LL 
  where E.JOB_ID = J.JOB_ID
  AND   E.LOGIN_ID = A.LOGIN_ID
  AND   A.COUNTRY_UID = C.COUNTRY_ID
  AND   L.LOV_GRP ='EMP_HIRING'
  AND   L.LOV_CD  = E.STATUS
  AND   E.STATUS = '1'
  AND   LL.LOV_GRP ='EMP_SHIFT'
  AND   NVL(A.SHIFT_TIME,5) = LL.LOV_CD 
  AND   UPPER(A.gender) = decode(&V_GENDER,'BOTH',UPPER(A.gender), &V_GENDER )
  AND   A.REGION IN (case when &P_REGION IS NOT NULL  then (select column_value  from table (DOY_FN_STR_TO_TBL(&P_REGION)))
                          else A.REGION  end) ;

字符串
我们正在尝试使用case条件获取一组记录,但发现以下错误
ORA-01427单行子查询返回多行

hl0ma9xz

hl0ma9xz1#

CASE WHEN语句中不能使用SELECT,因此需要重写它。
CASE WHEN只能返回标量值,因此需要重写它
另外,JOIN作为SQL标准已经存在了30年,所以是时候使用它了。

select A.REGION  ,
       E.APPLY_ID    ,
       E.JOB_ID      , 
       E.LOGIN_ID    ,
       J.TITLE      
  from EMPLOYEE_JOBS E  
  INNER JOIN EMPLOYEE_JOB_LIST  J ON E.JOB_ID = J.JOB_ID
  INNER JOIN EMPLOYEE_APP A  ON E.LOGIN_ID = A.LOGIN_ID
  INNER JOIN COUNTRY C ON A.COUNTRY_UID = C.COUNTRY_ID
  INNER JOIN LOV_MISC L ON L.LOV_CD  = E.STATUS
  INNER JOIN LOV_MISC LL ON NVL(A.SHIFT_TIME,5) = LL.LOV_CD 
  where 
     L.LOV_GRP ='EMP_HIRING'   
  AND   E.STATUS = '1'
  AND   LL.LOV_GRP ='EMP_SHIFT'
  AND   UPPER(A.gender) = decode(&V_GENDER,'BOTH',UPPER(A.gender), &V_GENDER )
  AND   case when &P_REGION IS NOT NULL  then A.REGION IN ( select column_value  from table (DOY_FN_STR_TO_TBL(&P_REGION)))
                          else A.REGION = A.REGION end ;

字符串

vddsk6oq

vddsk6oq2#

select column_value  from table (DOY_FN_STR_TO_TBL(&P_REGION))

字符串
是问题中唯一的子查询;正如错误所述,它必须返回多行。
您需要限制子查询,使其仅返回单行:

select column_value
from   table (DOY_FN_STR_TO_TBL(&P_REGION))
where  rownum = 1


否则,重写查询以删除CASE表达式并使用OR

select A.REGION  ,
       E.APPLY_ID    ,
       E.JOB_ID      , 
       E.LOGIN_ID    ,
       J.TITLE      
from   EMPLOYEE_JOBS E
       INNER JOIN EMPLOYEE_JOB_LIST J
       ON (E.JOB_ID = J.JOB_ID)
       INNER JOIN EMPLOYEE_APP A
       ON (E.LOGIN_ID = A.LOGIN_ID)
       INNER JOIN COUNTRY C
       ON (A.COUNTRY_UID = C.COUNTRY_ID)
       INNER JOIN LOV_MISC L
       ON (L.LOV_CD  = E.STATUS)
       INNER JOIN LOV_MISC LL
       ON (NVL(A.SHIFT_TIME,5) = LL.LOV_CD)
where  L.LOV_GRP ='EMP_HIRING'
AND    E.STATUS = '1'
AND    LL.LOV_GRP ='EMP_SHIFT'
AND    (  &V_GENDER = 'BOTH'
       OR UPPER(A.gender) = &V_GENDER )
AND    (  A.REGION IN (select column_value  from table (DOY_FN_STR_TO_TBL(&P_REGION)))
       OR &P_REGION IS NULL );

相关问题