I have created two example tables, test1 and test2.
test1 has three columns: test_id (PK), test_count, test_desc.
test2 has two columns: test_count (PK), test_desc.
From test2 table I want to retrieve the max value of column test_count and I want the value to be inserted into test1 table during each insert into statement execution like below:
insert into test1 (test_id, test_desc) values (1,'blahblahblah');
The value should be inserted automatically by the following trigger execution:
CREATE OR REPLACE TRIGGER maxTestCount
after insert on test1
FOR EACH ROW
DECLARE
varMaxCount number(10);
BEGIN
Select max(test_count) INTO varMaxCount
From test2;
Insert Into test1 (test_count) values (varMaxCount);
END;
It cannot be performed due to the following error:
SQL Error: ORA-04091: table MMDMANAGER.TEST1 is mutating, trigger/function may not see it
ORA-06512: at "MMDMANAGER.MAXTESTCOUNT", line 6
ORA-04088: error during execution of trigger 'MMDMANAGER.MAXTESTCOUNT'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
Would You give me advice how to avoid this error?

max(value)data. - Lalit Kumar B