Oracle错误中的PL / SQL

时间:2017-08-08 02:36:50

标签: sql oracle plsql

不确定我做错了什么。我试过移动的东西,但没有运气。继续收到以下错误。有人,请指教。

Declare 

 Begin
FOR v_rec IN (
select max(TotalEmployee) as TotalEmployee1 , Week_Day 
    from (select count(*) as TotalEmployee,  TO_CHAR(hire_date, 'Day') as Week_Day  
           from Employee  
           group by TO_CHAR(hire_date, 'Day') 
    Order by TotalEmployee desc) Table1
    group by Week_Day
                    )LOOP   
    dbms_output.put_line('*********************************************************************************************************************************************************************************************************');      
    dbms_output.put_line(v_rec.Week_Day || (v_rec.TotalEmployee1)||' employee');
    dbms_output.put_line(rpad('Hire_Date', 15) ||  rpad('Name', 15) || rpad('JOB TITLE', 45) || rpad('SALARY', 20) || rpad( '# of EMP',15 ) || rpad('DEPARTMENT NAME',20) ||rpad( 'MANAGER NAME',15 ) || rpad( 'MANAGER SALARY',15 ));                             
    FOR v_rec1 IN (select HIRE_DATE , EMPLOYEE_NAME as Name , JOB_TITLE , 
                Case when SALARY = null then '-------' 
                     ELSE to_char( SALARY, '$9,999.99' ) 
                END   as "SALARY" ,
                Case when DEPARTMENT_NAME = null then '-------' 
                     ELSE DEPARTMENT_NAME 
                END as DEPARTMENT_NAME,
                Case when Employee_NAME = null then '-------'
                            ELSE Employee_NAME 
                        END as MANAGERNAME,
                Case when SALARY = null then '-------' 
                             ELSE to_char( SALARY, '$9,999.99' ) 
                        END   as MANAGERSALARY 
                from Employee, department
           where TO_CHAR(hire_date, 'Day') in (v_rec.Week_Day) and employee.DEPARTMENT_ID = department.DEPARTMENT_ID  ) LOOP

            dbms_output.put_line(rpad(v_rec1.HIRE_DATE, 15) ||  rpad(v_rec1.Name, 15) || rpad(v_rec1.JOB_TITLE, 45) || rpad(v_rec1.SALARY, 20) || rpad( '# of EMP',15 ) || rpad(v_rec1.DEPARTMENT_NAME,20) ||rpad( Employee_NAME,15 ) || rpad( SALARY,15 ));                             

    END LOOP;
END LOOP;
END;

继续收到以下错误消息。有人请指教。

错误报告 -

ORA-06550: line 29, column 204:
PLS-00201: identifier 'EMPLOYEE_NAME' must be declared
ORA-06550: line 29, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

3 个答案:

答案 0 :(得分:1)

错误在以下行中,除了employee_name和salary之外的所有内容,您都正确指定了v_rec1。对于这两个你没有,假设你没有在别处声明这些变量,那么这将给你指定的错误。

dbms_output.put_line(rpad(v_rec1.HIRE_DATE, 15) ||  rpad(v_rec1.Name, 15) || rpad(v_rec1.JOB_TITLE, 45) || rpad(v_rec1.SALARY, 20) || rpad( '# of EMP',15 ) || rpad(v_rec1.DEPARTMENT_NAME,20) ||rpad( Employee_NAME,15 ) || rpad( SALARY,15 )); 

答案 1 :(得分:0)

编码时需要了解一些基本的东西。 "NULL"无法与任何事物进行比较。我们需要使用IS NULL/IS NOT NULL来检查NULL条件。根据基本语法,代码似乎没问题,但是需要确认列名称,因为错误表明列标识符不匹配。还要避免在代码中使用""RESERVED关键字.Hope下面的片段有帮助。

DECLARE
BEGIN
  FOR v_rec IN
  (SELECT MAX(TotalEmployee) AS TotalEmployee1 ,
    Week_Day
  FROM
    (SELECT COUNT(*)            AS TotalEmployee,
      TO_CHAR(hire_date, 'Day') AS Week_Day
    FROM Employee
    GROUP BY TO_CHAR(hire_date, 'Day')
    ORDER BY TotalEmployee DESC
    ) Table1
  GROUP BY Week_Day
  )
  LOOP
    dbms_output.put_line('*********************************************************************************************************************************************************************************************************');
    dbms_output.put_line(v_rec.Week_Day || (v_rec.TotalEmployee1)||' employee');
    dbms_output.put_line(rpad('Hire_Date', 15) || rpad('Name', 15) || rpad('JOB TITLE', 45) || rpad('SALARY', 20) || rpad( '# of EMP',15 ) || rpad('DEPARTMENT NAME',20) ||rpad( 'MANAGER NAME',15 ) || rpad( 'MANAGER SALARY',15 ));
    FOR v_rec1 IN
    (SELECT HIRE_DATE ,
      EMPLOYEE_NAME AS EMP_NAME ,
      JOB_TITLE ,
      CASE
        WHEN SALARY IS NULL
        THEN '-------'
        ELSE TO_CHAR( SALARY, '$9,999.99' )
      END AS "SALARY" ,
      CASE
        WHEN DEPARTMENT_NAME IS NULL
        THEN '-------'
        ELSE DEPARTMENT_NAME
      END AS DEPARTMENT_NAME,
      CASE
        WHEN Employee_NAME IS NULL
        THEN '-------'
        ELSE Employee_NAME
      END AS MANAGERNAME,
      CASE
        WHEN SALARY IS NULL
        THEN '-------'
        ELSE TO_CHAR( SALARY, '$9,999.99' )
      END AS MANAGERSALARY
    FROM Employee,
      department
    WHERE TO_CHAR(hire_date, 'Day') IN (v_rec.Week_Day)
    AND employee.DEPARTMENT_ID       = department.DEPARTMENT_ID
    )
    LOOP
      dbms_output.put_line(rpad(v_rec1.HIRE_DATE, 15) || rpad(v_rec1.EMP_NAME, 15) || rpad(v_rec1.JOB_TITLE, 45) || rpad(v_rec1.SALARY, 20) || rpad( '# of EMP',15 ) || rpad(v_rec1.DEPARTMENT_NAME,20) ||rpad( Employee_NAME,15 ) || rpad( SALARY,15 ));
    END LOOP;
  END LOOP;
END;

答案 2 :(得分:0)

错误消息为您提供了行号,因此您知道哪一行会导致问题。第29行是长DBMS_OUTPUT调用,从rpad( Employee_NAME,15 ) ^ 204 开始。

错误消息还会为您提供列号。列等于字符(从一切都是固定大小字体的日子开始)。第204列指向此元素:

rpad(v_rec1.HIRE_DATE, 15)

编译器从左到右工作,因此您知道它对此代码左侧的所有内容感到满意。代码的比较表明" good"标识符都以光标名称为前缀,例如Employee_NAME。 PL / SQL需要在正确的命名空间中标识所有引用(默认为DECLARE部分)。所以解决方案是在salary - 和rpad( v_rec1.Employee_NAME,15 ) - 引用前添加游标名称:Employee_NAME = null

PL / SQL错误并不总是很清楚:由于缺少逗号或括号而导致的语法错误可能包含与实际拼写错误相差很远的行号和/或列号。但它们不仅仅是随机数,它们包含诊断错误的有用线索。它有一个合适的程序编辑器,可以在源代码视图中显示行号和列号。

顺便说一下,这永远不会成真:Employee_NAME is null。 NULL是特殊的,为了测试它,你需要IS运算符: fnOpenFile(){ this.fileChooser.open() .then(uri =>{ console.log(uri); } ) .catch(e =>{ console.log(e); } ); }