N
N
Nikolay Baranenko2017-09-14 11:45:18
Oracle
Nikolay Baranenko, 2017-09-14 11:45:18

What is the correct way to solve ORA-38104 issue with trigger AFTER INSERT or UPDATE with merge?

Hello.
to solve the problem, it was necessary to create a trigger that would monitor the AFTER INSERT or UPDATE operations.

/*таблица за которой будет следить триггер
*/
create table ALL_EVENTS
(
ID varchar2(255),
LAST_USER varchar2(100),
VALUE number,
TIME_RECIEVED timestamp
)
;

/*таблица лог слежения*/
-- Create table
create table ALL_EVENTS_LOG
(
  id            VARCHAR2(255),
/*  type          VARCHAR2(50),*/
  time_received TIMESTAMP(6)
)
;

/*триггер, c merge не генерит дубль если в логе уже есть информация об ID */

CREATE OR REPLACE TRIGGER ALL_EVENTS_LOG
  AFTER INSERT or UPDATE ON ALL_EVENTS
FOR EACH ROW
declare
  -- local variables here
begin
merge into ALL_EVENTS_LOG log
using DUAL on (log.id=:OLD.id)
WHEN MATCHED THEN UPDATE SET log.id=:OLD.id
  WHEN NOT MATCHED THEN INSERT (ID, TIME_RECEIVED) VALUES (:NEW.id, sysdate);
end ALL_EVENTS_LOG;

when trying to execute insert
insert into ALL_EVENTS values (to_char(abs(dbms_random.random)), 'user1',abs(dbms_random.random),sysdate);

return ORA-38104 exception
ORA-38104: Columns referenced in the ON Clause cannot be updated: "LOG"."ID"
ORA-06512: at "ALL_EVENTS_LOG", line 4
ORA-04088: error during execution of trigger 'ALL_EVENTS_LOG'

I understand that I'm making some simple mistake somewhere ...
How to solve this problem correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Holub, 2017-09-14
@drno-reg

Solutions from another resource:
1
2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question