如何纠正“ PLS-00302:必须声明组件”错误

时间:2019-06-18 15:43:05

标签: oracle plsql

我正在尝试编译以下代码,但出现错误:

  

“ PLS-00302:必须声明组件'DEPT_ID'”。

DECLARE
DEPT_ID DEPARTMENTS.DID%TYPE;
DEPT_NAME DEPARTMENTS.DEPARTMENT_NAME%TYPE;
D_MID DEPARTMENTS.MANAGER_ID%TYPE;
D_LID DEPARTMENTS.LOCATION_ID%TYPE;
CURSOR C2 IS SELECT * FROM DEPARTMENTS WHERE DID=1;
D_ROW C2%ROWTYPE;
BEGIN
OPEN C2;
LOOP
FETCH C2 INTO D_ROW;
EXIT WHEN C2%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(D_ROW.DEPT_ID||' | '||D_ROW.DEPT_NAME||' | '||D_ROW.D_MID||' | '||D_ROW.D_LID);
END LOOP;
CLOSE C2;
END;

2 个答案:

答案 0 :(得分:2)

似乎您需要转换为:

DECLARE
-- DEPT_ID DEPARTMENTS.DID%TYPE;
-- DEPT_NAME DEPARTMENTS.DEPARTMENT_NAME%TYPE;
-- D_MID DEPARTMENTS.MANAGER_ID%TYPE;
-- D_LID DEPARTMENTS.LOCATION_ID%TYPE;
   CURSOR C2 IS SELECT * FROM DEPARTMENTS WHERE DID=1;
   D_ROW C2%ROWTYPE;
BEGIN
   OPEN C2;
 LOOP
   FETCH C2 INTO D_ROW;
  EXIT WHEN C2%NOTFOUND;
  DBMS_OUTPUT.PUT_LINE(D_ROW.DID||' | '||D_ROW.DEPARTMENT_NAME||' | '||
                       D_ROW.MANAGER_ID||' | '||D_ROW.LOCATION_ID);
 END LOOP;
 CLOSE C2;
END;
/

不需要这些局部变量,因为它们的相关列的引用已经在C2%rowtype中存在,并且应该由D_ROW用列名进行调用。由于使用局部变量而不是前缀D_ROW.之后的那些列而引起该错误。例如

DBMS_OUTPUT.PUT_LINE(D_ROW.DEPT_ID||' | '||D_ROW.DEPT_NAME||' | '|| 
                     D_ROW.D_MID||' | '||D_ROW.D_LID);

转换为

  DBMS_OUTPUT.PUT_LINE(D_ROW.DID||' | '||D_ROW.DEPARTMENT_NAME||' | '||
                       D_ROW.MANAGER_ID||' | '||D_ROW.LOCATION_ID);

Demo

答案 1 :(得分:0)

开始于:

select owner, object_type, object_name
from all_objects
where object_name = 'DEPT_ID';

可能是,以前已经声明过这样名称的对象

[dba-oracle] [1] http://dba-oracle.com/t_pls_00302.htm