Need to create a trigger that updates without adding same number - oracle

I need to create a trigger that activates after i make an update in Table A, registering in an audit log the number that i updated in Table A, but if the number has already been added (example the trigger tries to add 1 when there is a 1 already)it must ignore it and only let the first one.
Example:
Table A updates with 5,5,6,8,4,4
Then the audit log must save 5,6,8,4
The trigger i have already:
CREATE OR REPLACE TRIGGER registro_aeropuerto
AFTER UPDATE ON AEROPUERTO
FOR EACH ROW
DECLARE
A INT;
B INT;
BEGIN
A := table_A_updated_column_value;
SELECT CASE
WHEN EXISTS(SELECT * FROM Audit_log WHERE A = Coordinator)
THEN 1
ELSE 0
END INTO B FROM DUAL;
IF B = 0
THEN
INSERT INTO Audit_log(Coodinator, Date) VALUES (A, trunc(sysdate));
END;
Whenever i try to execute the trigger it gives me the next error:
The symbol ";" has been found when the it was expected:

Sample tables (upd_col_value is column you're updating; you named it "table_A_updated_column_value")
SQL> create table aeropuerto (upd_col_value number);
Table created.
SQL> create table audit_log (coordinator number, datum date);
Table created.
Trigger can be simplified; no need to declare any additional variables nor to check first and insert next; do it in the same select statement:
SQL> create or replace trigger registro_aeropuerto
2 after update on aeropuerto
3 for each row
4 begin
5 insert into audit_log (coordinator, datum)
6 select :new.upd_col_value, sysdate
7 from dual
8 where not exists (select null
9 from audit_log a
10 where a.coordinator = :new.upd_col_value
11 );
12 end;
13 /
Trigger created.
Testing:
SQL> insert into aeropuerto (upd_col_value) values (1);
1 row created.
SQL> select * from audit_log;
no rows selected
There's nothing in the log because nothing was updated. So, let's update it:
SQL> update aeropuerto set upd_col_value = 5;
1 row updated.
SQL> select * from audit_log;
COORDINATOR DATUM
----------- -------------------
5 15.09.2021 07:14:46
SQL>
OK; log now contains a row. Another update:
SQL> update aeropuerto set upd_col_value = 6;
1 row updated.
SQL> select * from audit_log;
COORDINATOR DATUM
----------- -------------------
5 15.09.2021 07:14:46
6 15.09.2021 07:15:37
SQL>
Right; two rows, as 5 was updated to 6. What happens if we update 6 back to 5?
SQL> update aeropuerto set upd_col_value = 5;
1 row updated.
SQL> select * from audit_log;
COORDINATOR DATUM
----------- -------------------
5 15.09.2021 07:14:46
6 15.09.2021 07:15:37
SQL>
Nothing happened; row with coordinator = 5 was in the table already so new row wasn't added.

Related

limit the amount of trigger updates for a specific row in oracle

I want to audit update changes in a specific table, and for that I've created a trigger that tracks every time a row is updated, it then write the updated changes into a new historical table:
create table test (id number generated always as identity,name varchar2(10) default null, school varchar2(10) null);
insert into test (name,school) values ('John','MIT');
insert into test (name,school) values ('Max','Oxford');
create table test_history (id int,name varchar2(10), school varchar2(10));
create or replace trigger test_trigger
after update
of name,school
on test
for each row
begin
insert into test_history
values
(
:old.id,
:new.name,
:new.school
);
end;
/
What I would like to do is to limit the amount a specific row is updated to a certain value. For example, the following update statement can only be executed 10 times:
update test
set
name = 'Jason'
where id = 1;
In this way if I execute the above statement 10 times it should work, but if the execution happens the 11th time it should fail. So the maximum amount of rows of a specific unique id is 10.
Count number of rows in the history table and raise an error if it exceeds value you find appropriate.
SQL> create or replace trigger test_trigger
2 after update
3 of name,school
4 on test
5 for each row
6 declare
7 l_cnt number;
8 begin
9 select count(*) into l_cnt
10 from test_history
11 where id = :new.id;
12
13 if l_cnt <= 10 then
14 insert into test_history
15 values
16 (
17 :old.id,
18 :new.name,
19 :new.school
20 );
21 else
22 raise_application_error(-20000, 'Too many updates');
23 end if;
24 end;
25 /
Trigger created.
Update:
SQL> update test set name = 'Jason' where id = 1;
1 row updated.
<snip>
SQL> update test set name = 'Jason' where id = 1;
1 row updated.
SQL> update test set name = 'Jason' where id = 1;
update test set name = 'Jason' where id = 1
*
ERROR at line 1:
ORA-20000: Too many updates
ORA-06512: at "SCOTT.TEST_TRIGGER", line 17
ORA-04088: error during execution of trigger 'SCOTT.TEST_TRIGGER'
SQL>

Get the last updated row in an Oracle table

How can I get the last updated row in an Oracle table? I have a table named Employees and I will perform some updates on it. Then I want to find out which row was updated last (i.e. which employee was updated last).
The easiest way
A table with a field name updated type date
A trigger to update the column whenever any of the other columns are updated.
Then
SQL> create table test.t1_trigger ( c1 number, c2 number, updated date );
Table created.
SQL> create or replace trigger test.t1_trg_update
before update of c1,c2 on test.t1_trigger
referencing new as new old as old
for each row
begin
:new.updated := sysdate;
end;
/
Trigger created.
SQL> insert into test.t1_trigger values ( 1 , 1 , null );
1 row created.
SQL> commit ;
Commit complete.
SQL> update test.t1_trigger set c1=2 , c2=2 ;
SQL> commit ;
Commit complete.
SQL> select * from test.t1_trigger ;
C1 C2 UPDATED
---------- ---------- -------------------
2 2 05.11.2021 12:18:30

How do i Solve Oracle Mutation Error from TRIGGER

I Have Two Tables; TBL_EMPDETAILS (empdetails_id, EMP_SALARY) and TBL_SERVICE (empdetails_id, Salary, Date_Appointed). The idea is that when i update the tbl_service (which basically is salary history) it should update TBL_EMPDETAILS to the most recent Salary.
I've created a TRIGGER But i keep getting MUTATION ERROR. From my research i have seen recommended compound triggers but i am unsure. I also tried pragma autonomous_transaction; befor the bgin statement but encountered "DEADLOCK ERROR"
create or replace trigger Update_Salary
before insert or update on "TBL_SERVICE"
for each row
declare
x number ;
y number ;
z date ;
m date;
begin
x := :NEW."SALARY";
y := :NEW."EMPDETAILS_ID";
z := :NEW."DATE_APPOINTED";
Select max(DATE_APPOINTED)
into m From TBL_SERVICE Where Empdetails_id = y ;
IF z >= m
THEN
update tbl_empdetails Set EMP_SALARY = x Where Empdetails_id = y ;
End If;
commit;
end;
I Expect that when i add a row to the TBL_SERVICE for eg. (empdetails_id, Salary, Date_Appointed) = (100, $500 , 20-Jul-2019) it should update the TBL_EMPDETAILS (empdetails_id, EMP_SALARY) to (100, $500)
Mutation Error -ORA-04091
Deadlock Error -ORA-00060
So i Think the COMPOUND TRIGGER LOOKS LIKE THE ROUTE TO GO... I TRIED CODE BELOW BUT IM STILL MISSING SOMETHING :(
create or replace TRIGGER "RDC_HR".Update_Salary
FOR UPDATE OR INSERT ON "RDC_HR"."TBL_SERVICE"
COMPOUND TRIGGER
m date ;
AFTER EACH ROW IS
begin
Select max(DATE_APPOINTED) into m From TBL_SERVICE
Where Empdetails_id = :NEW."EMPDETAILS_ID" ;
END AFTER EACH ROW;
AFTER STATEMENT IS
BEGIN
IF (:NEW."DATE_APPOINTED") >= m THEN
update tbl_empdetails Set EMP_SALARY = :NEW."SALARY"
Where Empdetails_id = :NEW."EMPDETAILS_ID" ;
End If;
END AFTER STATEMENT;
end Update_Salary;
How about merge?
SQL> create table tbl_empdetails (empdetails_id number, emp_salary number);
Table created.
SQL>
SQL> create table tbl_service (empdetails_id number, salary number, date_appointed date);
Table created.
SQL>
SQL> create or replace trigger trg_biu_ser
2 before insert or update on tbl_service
3 for each row
4 begin
5 merge into tbl_empdetails e
6 using (select :new.empdetails_id empdetails_id,
7 :new.salary salary,
8 :new.date_appointed date_appointed,
9 (select max(s1.date_appointed)
10 from tbl_service s1
11 where s1.empdetails_id = :new.empdetails_id
12 ) da
13 from dual
14 ) x
15 on (x.empdetails_id = e.empdetails_id)
16 when matched then update set e.emp_salary = :new.salary
17 where :new.date_appointed > x.da
18 when not matched then insert (empdetails_id , emp_salary)
19 values (:new.empdetails_id, :new.salary);
20 end;
21 /
Trigger created.
SQL>
Testing:
SQL> -- initial value
SQL> insert into tbl_service values (1, 100, sysdate);
1 row created.
SQL> -- this is now the highest salary
SQL> insert into tbl_service values (1, 200, sysdate);
1 row created.
SQL> -- this won't be used because date is "yesterday", it isn't the most recent
SQL> insert into tbl_service values (1, 700, sysdate - 1);
1 row created.
SQL> -- this will be used ("tomorrow")
SQL> insert into tbl_service values (1, 10, sysdate + 1);
1 row created.
SQL> -- a new employee
SQL> insert into tbl_service values (2, 2000, sysdate);
1 row created.
SQL>
The final result:
SQL> select * From tbL_service order by empdetails_id, date_appointed;
EMPDETAILS_ID SALARY DATE_APPOINTED
------------- ---------- -------------------
1 700 24.07.2019 15:00:21
1 100 25.07.2019 15:00:08
1 200 25.07.2019 15:00:15
1 10 26.07.2019 15:00:27
2 2000 25.07.2019 15:00:33
SQL> select * from tbl_empdetails order by empdetails_id;
EMPDETAILS_ID EMP_SALARY
------------- ----------
1 10
2 2000
SQL>
There are a few basic issues with the trigger as shown.
First, it contains a COMMIT. There shouldn't be a COMMIT in a trigger, since the transaction is still in flight.
The larger problem is that you are accessing the table on which the trigger was created within the trigger:
Select max(DATE_APPOINTED)
into m From TBL_SERVICE Where Empdetails_id = y ;
A row-level trigger cannot query or modify the base table. This is what is causing the mutating table error.
There are a few approaches to handle this.
If you want to use a trigger, you will need to defer the part that queries the base table to a time after the row-level trigger is complete.
This is done using a statement-level trigger or a compound trigger.
A row-level trigger can communicate "work to do" by storing state in a variable in a package, a following statement-level trigger can then inspect the package variables and do work based on the content.
The compound trigger mechanism is a way of putting the row and statement triggers in one code unit, along with the package bits. It is a way of writing the whole thing with one chunk of code (compound trigger) rather than three (row trigger, package, statement trigger).
Here is a detailed writeup of using Compound Triggers: Get rid of mutating table trigger errors with the compound trigger
As mentioned, moving the code out of triggers and into a stored procedure is certainly an option.

Prevent parallel exection of procedure

I have a table trigger, which calls a procedure when the status change from 2 to 3. The procedure check if the whole group of data(group_id) is in status 3 and then perform some actions.
But now I'm facing the problem that when I set the whole group of data in status 3 at the same time, the procedure get called multiple times and perform this actions multiple times. How can I prevent his? For example with locks
Here is my procedure query:
SELECT COUNT(*)
INTO nResult
FROM ticket
WHERE group_id = nGroupId
AND statusid BETWEEN 0 AND 2;
/* If not all tickets of group in status 3, no action required */
IF nResult != 0 THEN
RETURN;
END IF;
And this is my trigger:
IF (:NEW.STATUSID = 3 AND :OLD.STATUSID = 2) THEN
myprocedure(:NEW.group_id);
END IF;
You probably have a row level trigger, that is fired every time a row is updated; for example:
SQL> create table trigger_table(status number);
Table created.
SQL> insert into trigger_table values (1);
1 row created.
SQL> insert into trigger_table values (2);
1 row created.
SQL> insert into trigger_table values (3);
1 row created.
SQL> create trigger update_trigger
2 after update on trigger_table
3 for each row /* ROW LEVEL */
4 begin
5 dbms_output.put_line('change');
6 end;
7 /
Trigger created.
SQL> set serveroutput on
SQL> update trigger_table set status = 1;
change
change
change
3 rows updated.
You need a table level trigger, fired after every update statement:
SQL> create or replace trigger update_trigger
2 after update on trigger_table
3 begin
4 dbms_output.put_line('change');
5 end;
6 /
Trigger created.
SQL> update trigger_table set status = 1;
change
3 rows updated.
Here you find something more.
As rightly observed by Nicholas Krasnov, in this kind of trigger, considering a set of rows and not a single one, you have not the :new or :old values.
A way to get your needs could be the following, but it's a tricky solution and I'd check it carefully before using in a production environment.
You could create a semaphore table to know if you have to fire the trigger or not, then use two triggers, one at row level, BEFORE update, and one at table level, AFTER update; the row level one checks the values and updates the semaphore table while the table level one, fired after the update, reads the semaphore, calls your procedure, if necessary, then resets the semaphore.
For example:
SQL> create table trigger_table(status number);
Table created.
SQL> insert into trigger_table values (1);
1 row created.
SQL> insert into trigger_table values (2);
1 row created.
SQL> insert into trigger_table values (3);
1 row created.
SQL> create table checkChange (fire varchar2(3));
Table created.
SQL> insert into checkChange values ('NO');
1 row created.
SQL> create or replace trigger before_update_trigger
2 before update on trigger_table
3 for each row /* ROW LEVEL */
4 begin
5 if :new.status = 3 and :old.status = 2 then
6 update checkChange set fire = 'YES';
7 end if;
8 end;
9 /
Trigger created.
SQL> create or replace trigger after_update_trigger
2 after update on trigger_table
3 declare
4 vFire varchar2(3);
5 begin
6 select fire
7 into vFire
8 from checkChange;
9 if vFire = 'YES' then
10 dbms_output.put_line('change');
11 update checkChange set fire = 'NO';
12 end if;
13 end;
14 /
Trigger created.
SQL> update trigger_table set status = 2;
3 rows updated.
SQL> update trigger_table set status = 3;
change
3 rows updated.
SQL>

Insert all records bar those in a banned list

I have before insert trigger on table1. If some data (ID) is not allowed an application error is raised.
But, when I use, for example, insert into table1 select id from table2 where id in (1,2,3) And if only ID '3' is not allowed, the others ID's (1 and 2) are not inserted as well.
How can I overcome this? The trigger code is similar to:
CREATE OR REPLACE TRIGGER t1_before_insert BEFORE INSERT
ON table1
FOR EACH ROW
DECLARE
xx number(20);
BEGIN
select id into xx from blocked_id where id=:new.id;
if :new.id=xx then raise_application_error(-20001, '--');
end if;
END;
Okay, two points. Firstly, you're risking a NO_DATA_FOUND exception with your SELECT INTO ..., raising this exception will kill your entire insert. Secondly you're raising an exception, which will stop your entire insert.
You need to ignore those IDs that are in your blocked table rather than raise an exception. To follow your original idea one method would be to utilise the NO_DATA_FOUND exception to only insert if nothing is found. I create a view on your table and define an INSTEAD OF trigger on this.
I would not use this method though (see below)
If we set-up a test-environment:
SQL> create table tmp_test ( id number );
Table created.
SQL> create table tmp_blocked ( id number );
Table created.
SQL> insert into tmp_blocked values (3);
1 row created.
Then you can use the following:
SQL> create or replace view v_tmp_test as select * from tmp_test;
View created.
SQL> create or replace trigger tr_test
2 instead of insert on v_tmp_test
3 for each row
4
5 declare
6
7 l_id tmp_test.id%type;
8
9 begin
10
11 select id into l_id
12 from tmp_blocked
13 where id = :new.id;
14
15 exception when no_data_found then
16 insert into tmp_test values (:new.id);
17 end;
18 /
Trigger created.
SQL> show error
No errors.
SQL> insert into v_tmp_test
2 select level
3 from dual
4 connect by level <= 3;
3 rows created.
SQL> select * from tmp_test;
ID
----------
1
2
As I said, I would not use triggers; a more efficient way of doing it would be to use MERGE. Using the same set-up as above.
SQL> merge into tmp_test o
2 using ( select a.id
3 from ( select level as id
4 from dual
5 connect by level <= 3 ) a
6 left outer join tmp_blocked b
7 on a.id = b.id
8 where b.id is null
9 ) n
10 on ( o.id = n.id )
11 when not matched then
12 insert values (n.id);
2 rows merged.
SQL>
SQL> select * from tmp_test;
ID
----------
1
2
An even easier alternative would be to just use a MINUS
insert into tmp_test
select level
from dual
connect by level <= 3
minus
select id
from tmp_banned
I really don't like the use of a trigger and an error in this way -- data integrity is not really what triggers are for. This seems to me to be a part of the application that should be included in the application code, perhaps in a procedure that acts as an API for inserts into the table.

Resources