ORA-01427:单行子查询返回多行(exists语句)

时间:2013-06-30 11:55:36

标签: sql oracle

我正在尝试执行下一个查询:

 SELECT  
   PROMO_PLAN.promo_plan_id,  
   PROMO_PLAN.promo_plan_name,  
   PROMO.promo_category_code,  
   PROMO.promo_id,  
   PROMO.allow_plan_code,  
   PROMO_PLAN.event_type_code,  
   ( SELECT  
     NVL(PROMO_MAX_TIMING_DURATION_VW.duration, 900) 
    FROM  
   promo_max_timing_duration_vw 
    WHERE  
   PROMO_PLAN.promo_plan_id = PROMO_MAX_TIMING_DURATION_VW.promo_plan_id) 
  AS duration,  
   PROMO_PLAN.start_date,  
   PROMO_PLAN.end_date,  
   PROMO_PLAN.day_type_id 
  FROM  
   promo_plan,  
   promo 

 WHERE  
   PROMO_PLAN.promo_plan_id <> -1 
    AND PROMO_PLAN.promo_id = PROMO.promo_id 
    AND PROMO_PLAN.promo_plan_id = 18150
   AND '27/09/2012' BETWEEN PROMO_PLAN.start_date AND PROMO_PLAN.end_date
   AND (PROMO_PLAN.day_type_id = 1307001 

   OR PROMO_PLAN.day_type_id = -1)
   AND (PROMO_PLAN.promo_target_id = -1 
    OR  EXISTS ( 
      SELECT  
       1 
      FROM  
       promo_plan_channel 
      WHERE  
       PROMO_PLAN_CHANNEL.promo_plan_id = PROMO_PLAN.promo_plan_id 
        AND PROMO_PLAN_CHANNEL.channel_id = 10829))

我收到错误消息:ORA-01427:单行子查询返回的值超过 一行

我尝试更改最后一个子查询,但没有改变任何内容。

有人可以告诉我如何解决它吗?

感谢

3 个答案:

答案 0 :(得分:4)

很可能是你的问题

 ( SELECT  
     NVL(PROMO_MAX_TIMING_DURATION_VW.duration, 900) 
    FROM  
   promo_max_timing_duration_vw 
    WHERE  
   PROMO_PLAN.promo_plan_id = PROMO_MAX_TIMING_DURATION_VW.promo_plan_id) 

返回多行。

答案 1 :(得分:2)

如果删除此部分,查询是否会运行?

   ( SELECT  
     NVL(PROMO_MAX_TIMING_DURATION_VW.duration, 900) 
    FROM  
   promo_max_timing_duration_vw 
    WHERE  
   PROMO_PLAN.promo_plan_id = PROMO_MAX_TIMING_DURATION_VW.promo_plan_id) 

如果是这样,你就知道罪魁祸首。修改此子查询以返回最多一行。

答案 2 :(得分:0)

尝试这样:

SELECT  
   PROMO_PLAN.promo_plan_id,  
   PROMO_PLAN.promo_plan_name,  
   PROMO.promo_category_code,  
   PROMO.promo_id,  
   PROMO.allow_plan_code,  
   PROMO_PLAN.event_type_code,  
   p.d AS duration,  
   PROMO_PLAN.start_date,  
   PROMO_PLAN.end_date,  
   PROMO_PLAN.day_type_id 
  FROM  
   promo_plan,  
   promo, 
   ( SELECT NVL(PROMO_MAX_TIMING_DURATION_VW.duration, 900) d, promo_plan_id
    FROM promo_max_timing_duration_vw 
   ) p
 WHERE  
   PROMO_PLAN.promo_plan_id <> -1 
    AND PROMO_PLAN.promo_id = PROMO.promo_id 
    AND PROMO_PLAN.promo_plan_id = 18150
    and PROMO_PLAN.promo_plan_id = p.promo_plan_id
   AND '27/09/2012' BETWEEN PROMO_PLAN.start_date AND PROMO_PLAN.end_date
   AND (PROMO_PLAN.day_type_id = 1307001 

   OR PROMO_PLAN.day_type_id = -1)
   AND (PROMO_PLAN.promo_target_id = -1 
    OR  EXISTS ( 
      SELECT  
       1 
      FROM  
       promo_plan_channel 
      WHERE  
       PROMO_PLAN_CHANNEL.promo_plan_id = PROMO_PLAN.promo_plan_id 
        AND PROMO_PLAN_CHANNEL.channel_id = 10829))