【问题标题】:How can I use PL/SQL Cursor for loop for a Trigger如何将 PL/SQL 游标用于触发器的循环
【发布时间】:2013-03-15 05:11:18
【问题描述】:

嗨,我是 plsql 的新手,这是我第一次使用 plsql

我使用 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;
;

【问题讨论】:

  • 变量数据类型的声明应该基于列类型——例如。 “employee_company_code emplyee_master.company_code%type;”。 miclaim_supervisor_count 可以是 pls_integer。
  • miclaim_supervisor_count 永远不会为 null,因为 count(*) 不会返回 null。如果您只对是否存在任何记录感兴趣,请在查询中放置 rownum = 1。

标签: oracle for-loop plsql cursor database-trigger


【解决方案1】:

感谢您的所有回答和时间:)

好吧,感谢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; 

希望这对像我这样的人有帮助:)

【讨论】:

    【解决方案2】:

    根本不需要游标——为什么不直接使用 SQL 语句来执行插入呢?

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多