2013-03-15 27 views
0

嗨,我是一個新手,PLSQL這是我第一次使用PLSQL我如何使用PL/SQL遊標for循環的觸發

我用PLSQL創建觸發器。這是我用來創建觸發器的語法。但它給出了一個錯誤,因爲「[Err] ORA-24344: success with compilation error」我不知道我出錯的地方。在這個觸發器中,我使用了一個帶有遊標的for循環。 我覺得那個遊標有什麼問題任何人都可以幫助我弄清楚我錯了什麼地方。我使用Navicat來做到這一點。我這個苦苦掙扎了近5天:(在此先感謝

CREATE OR REPLACE TRIGGER "c" AFTER INSERT ON "EMP_REPORT_TO" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW 
DECLARE 

miclaim_supervisor_count number; 
employee_company_code number; 
employee_businessunit number; 

cursor projMgrsCursor is select b.BU_MEMBER_ID 
from BU_MEMBER b, EMP_SUB_DIV s 
where s.EMP_NO = :NEW.EMP_NO 
and s.SUB_DIVISION_CODE = '02' and s.DIV_CODE = '041' 
and b.BU_ID IN (select BU_ID from BU_MEMBER where BU_MEMBER_ID = :NEW.EMP_NO); 


BEGIN 
     delete from MICL_SUPERVISORS where EMP_NO = :NEW.EMP_NO and IS_OVVERRIDDEN = 0; 
     select count(*) into miclaim_supervisor_count from MICL_SUPERVISORS where EMP_NO = :NEW.EMP_NO and IS_OVVERRIDDEN = 1; 
     select COMPANY_CODE into employee_company_code from EMPLOYEE_MASTER where EMP_NO = :NEW.EMP_NO; 


if (employee_company_code = 'SOFT')then 

      OPEN projMgrsCursor; 

      FOR projMgrsCursor IN projMgrs 
      LOOP     
      insert into MICL_SUPERVISORS VALUES ((:NEW.ID), (SELECT SYSDATE FROM DUAL), :NEW.ENTRYADDEDBY_EMP_NO, 3000, 0, projMgrEmpNo, NULL,:NEW.EMP_NO); 
      END LOOP; 
     close projMgrsCursor; 

else 
      if(miclaim_supervisor_count IS NULL or miclaim_supervisor_count<1) then 
       insert into MICL_SUPERVISORS VALUES ((:NEW.ID), (SELECT SYSDATE `enter code here`FROM DUAL), :NEW.ENTRYADDEDBY_EMP_NO, 3000, 0, :NEW.SUP_EMP_NO, NULL,:NEW.EMP_NO); 
      end if; 
end if; 


END; 
; 
+0

變量數據類型的聲明應該是基於列類型 - 例如。 「employee_company_code emplyee_master.company_code%type;」。 miclaim_supervisor_count可以是pls_integer。 – 2013-03-15 09:23:20

+0

miclaim_supervisor_count永遠不會爲空,因爲count(*)不會返回null。如果您只關心是否存在任何記錄,請在查詢中輸入rownum = 1。 – 2013-03-15 09:24:33

回答

0

感謝所有?烏爾答案和時間:)

好,我想通了,我哪裏出了錯感謝的Oracle SQL Developer實際上有毛病for循環

這裏是代碼(用於環路糾正碼)

OPEN projMgrsCursor; 

    LOOP 
    FETCH projMgrsCursor INTO projMgrs; 
    EXIT WHEN projMgrsCursor%NOTFOUND; 
    insert into 
MICL_SUPERVISORS VALUES ((:NEW.ID), (SELECT SYSDATE FROM DUAL), :NEW.ENTRYADDEDBY_EMP_NO, 3000, 0,projMgrs, NULL,:NEW.EMP_NO); 
    END LOOP; 

    CLOSE projMgrsCursor; 

希望這將有助於任何人都像我一樣:)

0

無需光標在所有 - 爲什麼不直接使用一個SQL語句來執行插入