单行子查询返回多个具有嵌套选择和子行的行更新

时间:2012-12-28 00:17:44

标签: oracle select nested subquery substring

我正在尝试为需要根据某些参数更新某些记录的表编写查询。我有几个嵌套的选择和几个嵌套的子串,我想是什么让我很难排除故障。我解决了其中几个 ORA-01427:单行子查询已经返回多个行问题,但我似乎无法找到这个问题。本质上,脚本正在做的是从记录中获取两列,并根据许多不同的标准,根据包含在不同表中的另一条记录来更新它们。以下是我正在使用的当前代码:

UPDATE CMC_SBEL_ELIG_ENT p
SET (p.CSPI_ID, p.SBEL_EFF_DT) =
(SELECT co.new_plan, co.ch_dt
   FROM sbsb_plan_conv co, cmc_sbel_elig_ent p
   WHERE co.ch_dt > p.sbel_eff_dt
     and co.ch_dt < current_date
     AND co.new_plan <> p.CSPI_ID
     AND co.sbsb_ck = p.sbsb_ck
     AND p.cspi_id IN co.OLD_PLAN
     and p.SBEL_ELIG_TYPE IN ('tm','ce','TM','CE')
     )
WHERE (p.cspd_cat IN (
   select unique substr(o.old_plan, 1, 1) 
      from facets_ws.sbsb_plan_conv o
      where 
        substr(o.old_plan, 1, 1) IN (
            select substr(y.new_plan, 1, 1)   
            from sbsb_plan_conv y, cmc_sbel_elig_ent u
            where y.sbsb_ck = u.sbsb_ck
              AND (p.SBEL_ELIG_TYPE IN ('tm','ce','TM','CE'))

              and ((substr(y.new_plan, 1, 1) = 'M'
                and substr(y.new_plan, 1, 1) != 'R'
                and substr(y.new_plan, 1, 1) != 'D') 

                or (substr(y.new_plan, 1, 1) = 'R'
                and substr(y.new_plan, 1, 1) != 'M'
                and substr(y.new_plan, 1, 1) != 'D')

                or (substr(y.new_plan, 1, 1) = 'D'
                and substr(y.new_plan, 1, 1) != 'R'
                and substr(y.new_plan, 1, 1) != 'M'))

            and o.sbsb_ck = p.sbsb_ck)
      )
  );

现在,当我对两个select语句运行各个查询时,它们都返回明确的唯一值。所以我很确定他们不是问题。

1 个答案:

答案 0 :(得分:0)

尝试执行以下查询: -

SELECT count(co.new_plan)
  FROM sbsb_plan_conv co, cmc_sbel_elig_ent p
WHERE co.ch_dt > p.sbel_eff_dt
 and co.ch_dt < current_date
 AND co.new_plan <> p.CSPI_ID
 AND co.sbsb_ck = p.sbsb_ck
 AND p.cspi_id IN co.OLD_PLAN
 and p.SBEL_ELIG_TYPE IN ('tm','ce','TM','CE')
 group by co.new_plan;

如果上面的内容返回多行或者返回的计数多于一行,那么您可能需要将查询更改为DISTINCT / MIN /使用ROWNUM或其他技术。