【问题标题】:ORA-01427: single-row subquery returns more than one row in Update StatementORA-01427: 单行子查询在更新语句中返回多于一行
【发布时间】:2025-11-22 05:45:02
【问题描述】:

ORA-01427: 单行子查询在更新语句中返回多于一行

嗨。对于以下查询,出现 ORA-01427 错误。

我正在尝试从 country_population 获取生日并将其设置为 city_population 中的 fave_date。不需要其他更简单的方法来执行此操作,我只需要一些建议如何消除错误并让查询工作。 谢谢你:)

    DECLARE
            CURSOR custCur IS
                     SELECT name, age, weight, height, birthday FROM country_population;
            CNTR number(9) := 0;
    BEGIN
            FOR curRec IN custCur LOOP

                    UPDATE city_population srvagr
                    SET     srvagr.date1 = SYSDATE,
                            srvagr.fave_date =
                                            (select curRec.birthday from country_population curRec
                                            where srvagr.name=curRec.name
                                            and srvagr.age=curRec.age
                                            and srvagr.weight=curRec.weight
                                            and srvagr.height=curRec.height),
                            srvagr.date2 = SYSDATE,
                            srvagr.date3 = SYSDATE,
                            srvagr.status = 'visitor',
                            srvagr.note = 'Noted'
                    WHERE
                            srvagr.name = curRec.name
                            and srvagr.age = curRec.age
                            and srvagr.weight = curRec.weight
                            and srvagr.height = curRec.height
                            and srvagr.status != 'visitor'
                            and srvagr.fave_date is null;

                    CNTR := CNTR + 1;
                    if CNTR = 1000
                            then
                                    COMMIT;
                                    CNTR := 0;
                    end if;

            END LOOP;

            COMMIT;

【问题讨论】:

  • 错误比较明显,子查询返回的条件超过 1 行 - 您需要进一步细化这些条件,或者在多行的情况下做出关于选择什么

标签: sql oracle for-loop update-statement


【解决方案1】:

查询

(select curRec.birthday from country_population curRec
                      where srvagr.name=curRec.name
                      and srvagr.age=curRec.age
                      and srvagr.weight=curRec.weight
                      and srvagr.height=curRec.height) 

返回不止一行,所以你不能使用相等的分配..

在这种情况下,您可以添加更多选择性 where 条件以仅获取行结果

或者,如果没有一个结果,则只能使用聚合函数来减少结果,例如:min() 或 max()

(select min(curRec.birthday)  from country_population curRec
                      where srvagr.name=curRec.name
                      and srvagr.age=curRec.age
                      and srvagr.weight=curRec.weight
                      and srvagr.height=curRec.height
                      ) 

或对行结果使用限制

(select min(curRec.birthday)  from country_population curRec
                      where srvagr.name=curRec.name
                      and srvagr.age=curRec.age
                      and srvagr.weight=curRec.weight
                      and srvagr.height=curRec.height
                      and rownum = 1) 

【讨论】:

  • 如何确定 min() 和/或 rownum=1 是否返回正确的生日?实际上,使用 where 子句中的 4 个条件,我期望只有 1 个生日分配给 1 个姓名/年龄/身高/体重(其中姓名每行唯一)。
  • 更好地解释你的评论 .. 不清楚 .. .. 你应该知道返回正确值的标准,你应该在几个建议的查询中选择适当的查询
  • 我的意思是,如果子查询返回许多行(例如 Jan1、Feb1、Mar1),那么如何保证为每个姓名/年龄/身高/体重检索到正确的值?会不会基于update语句的where子句?
  • 您正在处理的值是子查询与您正在处理的更新值相同的地方..这是数据库引擎的责任..
  • 嗨@scaisEdge - 你的建议奏效了。谢谢你:)