为什么出现错误“确切的获取返回的行数超过了请求的行数?”

问题描述 投票:-2回答:1
    1. create or replace procedure cities (
    2. vname in varchar2,
    3. vuni out varchar2
    4. ) as
    5. cursor c is
    6. select city_name, uni_name from uni ;
    7. cx c%rowtype;
    8. begin
    9. select uni_name into vuni from uni; 
    10. open c;
    11. loop
    12. fetch c into cx;
    13. exit when c%notfound;
    14. if (vname = 'Almaty') then
    15. select distinct cx.uni_name into vuni from uni where  cx.city_name = vname;
    16. elsif (vname = 'Nur-Sultan') then
    17. select distinct cx.uni_name into vuni from uni where cx.city_name = vname;
    18. elsif (vname = 'Aktau') then
    19. select distinct cx.uni_name into vuni from uni where cx.city_name = vname;
    20. else
    21. select distinct cx.uni_name into vuni from uni where cx.city_name = vname;
    22. end if;
    23. end loop;
    24. close c;
    25. end;
oracle if-statement plsql cursor procedure
1个回答
0
投票

检查光标之外的所有SELECT语句。

最可疑的看上去是第9行的第一个:

select uni_name into vuni from uni; 

除非UNI表仅包含一行,否则将返回too_many_rows

除此之外,还有多个SELECT DISTINCT语句。如果DISTINCT不起作用,则说明您的数据错误或代码错误。也许您需要在WHERE子句中添加另一个条件。

哪个语句返回错误,最简单的选择是使用聚合函数之一,例如

select max(cx.uni_name) ...

因为它只会返回一个值,但是-这很可能是您应该使用的最后一个选项。


[编辑]

等一下;是的,您遇到了各种各样的错误,但是-更仔细地看代码,这没有多大意义。首先,它应该是function,而不是过程:

create or replace function f_cities (par_vname in varchar2)
  return uni.uni_name%type
is
  retval uni.uni_name%type;
begin
  select u.uni_name
    into retval
    from u.uni
    where u.city_name = par_vname;

  return retval;
end;

如果必须是一个过程,那么

create or replace procedure cities
  (par_vname in varchar2,
   par_vuni out varchar2
  )
is
begin
  select u.uni_name
    into par_vuni
    from u.uni
    where u.city_name = par_vname;
end;

截至您的代码:

  • 不需要您使用的光标
  • select语句是如此奇怪;您正在OUT参数中选择一个游标变量值,从同一表中,游标的select基于
  • if s没用。用城市说的是in参数;您不必对这些值进行硬编码,因为如果表中有更多引用,那么该过程可能会变成一个真正的怪兽。

不久,我建议您使用上面发布的功能。

© www.soinside.com 2019 - 2024. All rights reserved.