如何在触发器内部调用函数

时间:2017-06-24 05:46:41

标签: oracle function plsql

我正在尝试使用Oracle PL / SQL创建一个函数。该函数将在触发器内调用。当我编译触发器时没有发现错误,但是当表krs显示

ORA-04091: table TIKET_BUS.KRS is mutating, trigger/function may not see it
ORA-06512: at "TIKET_BUS.GET_BIAYA_SEMESTER", line 4
ORA-06512: at "TIKET_BUS.GET_BIAYA_SEMESTER", line 17
ORA-06512: at "TIKET_BUS.TAGIH", line 4
ORA-04088: error during execution of trigger 'TIKET_BUS.TAGIH'

我错过了什么线索?

功能:

create or replace FUNCTION GET_BIAYA_SEMESTER
(NO_INDUK CHAR) RETURN NUMBER IS
CURSOR C_IPK IS
SELECT
k.SKS, n.kd_semester
FROM
MATA_KULIAH k, krs n
WHERE 
k.KD_MK = n.kd_mk and n.nim = NO_INDUK;
BARIS C_IPK%ROWTYPE;
S MAHASISWA.NAMA%TYPE;
TOTAL_BIAYA NUMBER := 0;
X NUMBER;
SKS NUMBER;
BIAYA NUMBER;
BEGIN
OPEN C_IPK;
LOOP
FETCH C_IPK INTO BARIS;
EXIT WHEN C_IPK%NOTFOUND;
X := BIAYA_MAKUL(BARIS.SKS);
TOTAL_BIAYA  := total_BIAYA + X;
END LOOP;
BIAYA := TOTAL_BIAYA;
CLOSE C_IPK;
RETURN BIAYA;
END;

触发:

create or replace trigger tagih
after insert on krs
for each row
declare
x number;
begin
x := get_biaya_semester(:new.nim);
update tagihan set bayar = (x) where nim = :new.nim;
end;

1 个答案:

答案 0 :(得分:0)

从行触发器调用的代码无法访问定义触发器的表。这里触发器在表KRS上定义,因此触发器或触发器调用的任何代码都不能访问表KRS。在这种情况下,您的函数GET_BIAYA_SEMESTER访问表KRS

我可以做的最简单的事情就是在GET_BIAYA_SEMESTERINSERT语句中加入对UPDATE的调用,例如

UPDATE KRS
  SET SOME_FIELD = 123,
      SOME_OTHER_FIELD = 456,
      NIM = NIM_VALUE,
      BAYAR = GET_BIAYA_SEMESTER(NIM_VALUE);

这消除了从触发器调用函数的需要。

祝你好运。