编译PLSQL函数时出现“找不到数据”错误

时间:2019-08-31 01:30:35

标签: oracle function plsql

当尝试编译以执行两个函数(@directory)时,我在第48行(或该函数的第13行)上始终没有出现数据错误。这是我收到的错误。注意:SYSTEM是我连接的服务器。

ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SYSTEM.DEPTPROJECT", line 13
ORA-06512: at line 4
ORA-06512: at line 4

这些是我的职责

SET ECHO ON;
SET FEEDBACK ON;
SET LINESIZE 100;
SET PAGESIZE 100;
SET SERVEROUTPUT ON;


CREATE OR REPLACE FUNCTION DEPTPROJECT(departmentNumber IN NUMBER)

RETURN VARCHAR IS
dep# DEPARTMENT.D#%TYPE;
depName DEPARTMENT.DName%TYPE;
counter NUMBER(10,0);
empNumber CHAR(5);
empName VARCHAR2(30);
result VARCHAR2(600);


BEGIN
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;

result := result || 'Department'|| dep# || '' || depName || chr (10);


FOR i IN(SELECT  P# , PTitle , Budget FROM PROJECT WHERE PROJECT.D# = dep# ORDER BY BUDGET DESC)
LOOP
result:= result || chr(9)|| 'Project: ' || i.P# || '' ||  i.PTitle || i.Budget || chr(10); 

FOR j IN(SELECT  EMPLOYEE.Name, EMPLOYEE.E# FROM WORKSON INNER JOIN EMPLOYEE ON EMPLOYEE.E# = WORKSON.E# WHERE WORKSON.P# = i.P# ORDER BY EMPLOYEE.NAME ASC  )
LOOP
result:= result || chr(10) || j.E# || '' || j.Name || chr(10); 



END LOOP;
END LOOP;
RETURN result;
END;
/

BEGIN 
FOR x IN(SELECT D# FROM DEPARTMENT)
LOOP
DBMS_OUTPUT.PUT_LINE(DEPTPROJECT(x.D#));
END LOOP;
END;
/

3 个答案:

答案 0 :(得分:2)

当您选择INTO变量并且没有返回记录时,您将收到“无数据发现”错误。我相信编写上述代码的正确方法是使用其自己的BEGIN / EXCEPTION / END块包装SELECT语句。

示例:

SET ECHO ON;
SET FEEDBACK ON;
SET LINESIZE 100;
SET PAGESIZE 100;
SET SERVEROUTPUT ON;


CREATE OR REPLACE FUNCTION DEPTPROJECT(departmentNumber IN NUMBER)

RETURN VARCHAR IS
dep# DEPARTMENT.D#%TYPE;
depName DEPARTMENT.DName%TYPE;
counter NUMBER(10,0);
empNumber CHAR(5);
empName VARCHAR2(30);
result VARCHAR2(600);


BEGIN
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = departmentNumber;

result := result || 'Department'|| dep# || '' || depName || chr (10);


FOR i IN(SELECT  P# , PTitle , Budget FROM PROJECT WHERE PROJECT.D# = dep# ORDER BY BUDGET DESC)
LOOP
result:= result || chr(9)|| 'Project: ' || i.P# || '' ||  i.PTitle || i.Budget || chr(10); 

FOR j IN(SELECT  EMPLOYEE.Name, EMPLOYEE.E# FROM WORKSON INNER JOIN EMPLOYEE ON EMPLOYEE.E# = WORKSON.E# WHERE WORKSON.P# = i.P# ORDER BY EMPLOYEE.NAME ASC  )
LOOP
result:= result || chr(10) || j.E# || '' || j.Name || chr(10); 

END LOOP;
END LOOP;
EXCEPTION
      WHEN NO_DATA_FOUND THEN
        result := 'Record Not Found';
RETURN result;
END;
/

BEGIN 
FOR x IN(SELECT D# FROM DEPARTMENT)
LOOP
DBMS_OUTPUT.PUT_LINE(DEPTPROJECT(x.D#));
END LOOP;
END;
/

答案 1 :(得分:1)

我猜第13行应该使用传递给函数的变量:

SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;

应该是:

SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = departmentNumber;

答案 2 :(得分:1)

您正在使用以下查询来获取dep#depName,但是where子句是WHERE DEPARTMENT.D# = dep#

SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;

dep#是您已声明但尚未初始化的变量。

所以dep#null,与NULL的比较将始终导致false,这意味着您的查询不返回任何记录。

在查询中使用INTO时,Oracle正在从该查询中查找一条记录,但没有返回任何记录。因此,您正面临这个问题。

根据您的逻辑,可以更改WHERE子句以在比较中包括departmentNumber,或者在查询中使用dep#之前为其指定适当的值。

另外,请确保将SELECT .. INTO查询包装在BEGIN..EXCEPTION..END中,以避免发生这种动态异常。

干杯!