masterTable, detailTable, historySumTable 세개가 있다고 했을때요....
masterTable : pk1, pk2, historyPk
detailTable : pk1, pk2, pk3, pk4, amtCol
historySumTable : historyPk, line_seq (pk), amtSumCol
하고싶은것은,
detail 테이블의 amtCol 이 바뀔때마다
historySumTable 에 insert 를 두개 하고 싶습니다.
1. historySumTable 원래 있던금액을 line_seq 증가로 amtSumCol * (-1) 처리하여 insert.
2. 변경된 detailTalbe 의 amtCol 합계금액을 line_seq 증가로 amtSumCol 로 insert.
여기에서 1번까지는 되는데 2번 합계금액을 구하는데 있어서 문제가 발생합니다.
에러가 왜 나는지는 알겠는데 이것을 어떻게 구해야 할지 모르겠어요.
(트리거에서 자기 자신을 select 했음 ora-04091)
===========================================================================
CREATE OR REPLACE TRIGGER trauDetailTable
after update
ON detailTable referencing new as new old as old
for each row
declare
wk_hisPk masterTable.historyPk%type;
wk_sum historyTable.amtSumCol%type;
-- historyPK를 구함
begin
select historyPk
into wk_hisPk
from masterTable
where pk1 = :new.pk1
and pk2 = :new.pk2;
exception
when no_data_found then
wk_hisPk := '999';
end;
-- 합계금액을 구함 (여기가 문제점이라는것은 알겠는데....합계를 어케 구해야 할지..ㅠ.ㅠ )
begin
select sum(amtCol)
into wk_sum
from detailTable
where pk1 = :new.pk1
and pk2 = :new.pk2;
exception
when no_data_found then
wk_sum := 0;
end;
-- historySumTable amtSumCol * (-1) 하여 insert
begin
insert into historySumTable
select
historyPk,
(select max(line_seq)+1 from historySumTable where historyPk = wk_hisPk) as line_seq,
amtSumCol * (-1) as amtSumCol
from historyPk = wk_hisPk;
exception
when others then
raise_application_error(-20010, replace(replace(replace(sqlerrm,'"',''), chr(10),''), chr(13),''));
end;
-- historySumTable amtSumCol에 위에서 구한 합계금액으로 하여 insert
begin
insert into historySumTable
select
historyPk,
(select max(line_seq)+1 from historySumTable where historyPk = wk_hisPk) as line_seq,
wk_sum as amtSumCol
from historyPk = wk_hisPk;
exception
when others then
raise_application_error(-20010, replace(replace(replace(sqlerrm,'"',''), chr(10),''), chr(13),''));
end;
exception
when others then
raise_application_error(-20010, sqlerrm);
end trauDetailTable;