oracle PL / SQL存储函数ORA-01422和ORA-06512

时间:2017-04-28 06:01:00

标签: oracle plsql user-defined-types

我想编写一个PL / SQL存储函数,该函数将驱动程序的员工编号作为参数,并返回驱动程序的全名,他访问过的城市以及他访问过该城市的次数。嵌套表。

我写了这个函数并且编译成功了。以下是嵌套表的代码:

    CREATE OR REPLACE TYPE C_V 
AS OBJECT
(   FULLNAME VARCHAR(150),
    CITIES_VISITED VARCHAR(30),
    TOT_VISITS NUMBER(3)
);

CREATE OR REPLACE TYPE D_V_C
IS TABLE OF C_V;

这是功能:

CREATE OR REPLACE FUNCTION DRIVERVISITEDCITIES ( D_E# NUMBER)
RETURN D_V_C
IS
  D_FULLNAME VARCHAR(150);
  CITIES_VISITED_BY VARCHAR (30);
  TOTAL_VISITS NUMBER(3);

CITY_VIS_DETAIL D_V_C := D_V_C();

BEGIN

  CITY_VIS_DETAIL.EXTEND();
  SELECT DISTINCT EMPLOYEE.FNAME || EMPLOYEE.INITIALS || EMPLOYEE.LNAME AS FULLNAME,
       UPPER(TRIPLEG.DESTINATION),
       COUNT(TRIPLEG.DESTINATION)
  INTO 
        D_FULLNAME,
        CITIES_VISITED_BY,
        TOTAL_VISITS
FROM EMPLOYEE
INNER JOIN DRIVER
ON DRIVER.E# = EMPLOYEE.E#
INNER JOIN TRIP
ON TRIP.L# = DRIVER.L#
INNER JOIN TRIPLEG
ON TRIPLEG.T# = TRIP.T#
WHERE EMPLOYEE.E# = D_E#
GROUP BY EMPLOYEE.FNAME||EMPLOYEE.INITIALS||EMPLOYEE.LNAME, TRIPLEG.DESTINATION
ORDER BY COUNT(TRIPLEG.DESTINATION) DESC;
RETURN CITY_VIS_DETAIL;
END;

但是,当我尝试测试它显示的功能时:

Error starting at line 1 in command:
SELECT DRIVERVISITEDCITIES(1) FROM DUAL
Error report:
SQL Error: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYS.DRIVERVISITEDCITIES", line 13
01422. 00000 -  "exact fetch returns more than requested number of rows"
*Cause:    The number specified in exact fetch is less than the rows returned.
*Action:   Rewrite the query or change number of rows requested

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

您已定义了一个集合变量,但您没有填充它。相反,你选择标量变量。很明显,您的查询返回多行(因为一个驱动程序已经多次执行),这就是您获得TOO_MANY_ROWS异常的原因。

您需要选择该集合。最简单的方法是使用BULK COLLECT:

package.appxmanifest

答案 1 :(得分:0)

  SELECT DISTINCT EMPLOYEE.FNAME || EMPLOYEE.INITIALS || EMPLOYEE.LNAME AS FULLNAME,
       UPPER(TRIPLEG.DESTINATION),
       COUNT(TRIPLEG.DESTINATION)
  INTO 
        D_FULLNAME,
        CITIES_VISITED_BY,
        TOTAL_VISITS
FROM EMPLOYEE

您的查询返回多个值并将其存储为标量变量。用对象类型变量替换标量变量并使用批量收集。