2017-08-08 86 views
0

不知道我在做什麼錯誤。我嘗試了移動的東西,但沒有運氣。繼續收到以下錯誤。有人,請指教。Oracle中的PL/SQL錯誤

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: 
+0

什麼是你的這個非常明確的錯誤消息,解釋'標識「EMPLOYEE_NAME」必須declared'? –

+0

確認表中的列名並使用IS null而不是= null,因爲null不能與任何值進行比較。 – Shahzaib

回答

0

有你需要同時編碼到understamd一些基本的東西。 "NULL"不能比較任何事情。我們需要使用IS NULL/IS NOT NULL來檢查NULL條件。該代碼似乎確定按基本語法,但需要確認的列名錯誤表明沒有在列不匹配identifier.Also避免使用""RESERVED關鍵字在code.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; 
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)); 
0

錯誤信息給你一個行號,這樣你就知道哪一行引起該問題。第29行是長DBMS_OUTPUT調用,從dbms_output.put_line(rpad(v_rec1.HIRE_DATE, 15)開始。

錯誤消息也給你一個數列。列等同於字符(從所有東西都是固定大小的字體開始)。列204點這個元素:

rpad(Employee_NAME,15) 
    ^
     204 

編譯器的工作原理由左到右,所以你知道它很樂意這麼做,是這段代碼的左邊的一切。代碼的比較顯示「好」標識符全部以光標名稱作爲前綴,例如, rpad(v_rec1.HIRE_DATE, 15)。 PL/SQL需要在正確的名稱空間中標識所有引用(默認爲DECLARE部分)。因此,解決辦法是預先設置Employee_NAME - 和salary - 用光標名稱引用:rpad(v_rec1.Employee_NAME,15)

PL/SQL的錯誤並不總是這樣明確:由缺少逗號或括號可以有線條和/或與實際錯字很長的路要走列數語法錯誤。但它們不只是隨機數字,它們包含診斷我們錯誤的有用線索。它有助於獲得一個體面的程序編輯器,它在源代碼視圖中顯示行號和列號。


順便說一句,這永遠不會是真的:Employee_NAME = null。 NULL是特殊的,爲了測試它,你需要IS運算符:Employee_NAME is null