How can i fix this Mutating table from a procedure an trigger - oracle

This trigger will pass on inserted values to a procedure which will insert those values in another table. I'm getting a mutating table error. How can i fix this?
CREATE OR REPLACE TRIGGER ADD_INVOICE
BEFORE INSERT OR UPDATE OF APP_NO,C_NO ON APPOINTMENT
FOR EACH ROW
DECLARE
BEGIN
POP_INVOICE(:NEW.APP_NO,:NEW.C_NO,:NEW.APP_DATE);
END;
/
CREATE OR REPLACE PROCEDURE POP_INVOICE(
I_APP_NO IN INVOICE.APP_NO%TYPE,
I_C_NO IN INVOICE.C_NO%TYPE,
I_INV_DATE IN INVOICE.INV_DATE%TYPE)
AS
CURSOR C_POP IS SELECT PRICE FROM TREATMENT T,APPOINTMENT A
WHERE T.TRT_NO=A.TRT_NO
AND A.APP_NO=I_APP_NO;
V_BILL INVOICE.BILL%TYPE;
BEGIN
OPEN C_POP;
FETCH C_POP INTO V_BILL;
UPDATE INVOICE
SET INV_NO=INV_IDSEQ.NEXTVAL,
APP_NO=I_APP_NO,
C_NO=I_C_NO,
BILL=V_BILL,
INV_DATE=I_INV_DATE;
END;
/

The problem is caused by referencing the table with the trigger on it within the trigger itself. Changing the procedure to accept the TRT_NO as a parameter removes the need to include APPOINTMENT in the query, and so will avoid the mutating table exception. Depending on how many records there are for each treatment, you could even incorporate the cursor into your UPDATE statement.
I think this should do it, although I haven't been able to check against a database.
CREATE OR REPLACE TRIGGER ADD_INVOICE
BEFORE INSERT OR UPDATE OF APP_NO,C_NO ON APPOINTMENT
FOR EACH ROW
DECLARE
BEGIN
POP_INVOICE(:NEW.APP_NO,:NEW.C_NO,:NEW.APP_DATE,:NEW.TRT_NO);
END;
/
The revised procedure:
CREATE OR REPLACE PROCEDURE POP_INVOICE(
I_APP_NO IN INVOICE.APP_NO%TYPE,
I_C_NO IN INVOICE.C_NO%TYPE,
I_INV_DATE IN INVOICE.INV_DATE%TYPE,
I_TRT_NO IN APPOINTMENT.TRT_NO%TYPE
)
AS
CURSOR C_POP IS SELECT PRICE
FROM TREATMENT T
WHERE T.TRT_NO = I_TRT_NO;
V_BILL INVOICE.BILL%TYPE;
BEGIN
OPEN C_POP;
FETCH C_POP INTO V_BILL;
CLOSE C_POP;
INSERT INVOICE
(inv_no, app_no, c_no, bill, inv_date)
VALUES
(INV_IDSEQ.NEXTVAL, I_APP_NO, I_C_NO, V_BILL, I_INV_DATE);
END;
/

Related

Oracle update trigger on the same table

I want to update field data_aktualizacji when some row in the same table is updated. I created the following compound trigger.
CREATE OR REPLACE TRIGGER oferta_update_trigger
FOR UPDATE ON oferty
compound TRIGGER
id_oferty number(10);
AFTER EACH ROW IS
BEGIN
id_oferty := :new.idk;
END AFTER EACH ROW;
AFTER STATEMENT IS
BEGIN
UPDATE oferty SET data_aktualizacji = SYSDATE WHERE idk = id_oferty;
END AFTER STATEMENT;
END;
/
When I want to update some record, I get the following error.
SQL Error: ORA-00036: maximum number of recursive SQL levels (50) exceeded.
How to solve this problem? I this that some loop is created, but I don't know, how to workaround this.
Update oracle to alter the column to default to sysdate
Alter table oferty alter column data_aktualizacji set default sysdate
No need for trigger at all
As Ctznkane525 wrote, you definitively should use default-value to perform this action.
If you don't want to use default you can modify new.data_aktualizacji:
CREATE OR REPLACE TRIGGER oferty_update_aktualizacji
BEFORE INSERT OR UPDATE
ON oferty
FOR EACH ROW
DECLARE
BEGIN
:new.data_aktualizacji:= sysdate;
END;

Addition of values in two columns isn't working in PL/SQL ORACLE

So I am trying to add age with price and store the result on another table with the ID of the person who did this. I am able to set the business rule by making the trigger but when I check my second (END) table, there is nothing there.. Here is my code for the trigger:
CREATE OR REPLACE TRIGGER JIM
BEFORE INSERT ON END
FOR EACH ROW ENABLE
DECLARE
V_AGE JIM.AGE%TYPE;
V_PRICE JIM.PRICE%TYPE;
v_prices NUMBER(20);
BEGIN
SELECT AGE,PRICE INTO V_AGE,V_PRICE FROM JIM WHERE ID=:NEW.ID;
v_prices:=V_AGE+V_PRICE;
INSERT INTO END VALUES(:new.ID,v_prices);
END;
However, When I insert values onto the JIM table using the following code:
insert into jim values(4,'Sim',45,100);
nothing actually gets stored on the END table. i am sort of new to triggers and its so confusing. Please let me know what to do. thanls
Don't use a keyword end as a table name, this causes problem during
creation of trigger. I've presumed the table's name as t_end.
I think you are confused on which table to define trigger. It seems
you should define on table jim instead of t_end.
I've presumed you have a sequence named seq_end to populate the id
column of the table t_end
So , your trigger creation statement will be as follows :
create or replace trigger trg_ins_jim before insert on jim for each row
declare
v_id_end t_end.id%type;
v_prices t_end.prices%type;
begin
v_prices := :new.age + :new.price;
v_id_end := seq_end.nextval; insert into t_end values(v_id_end, v_prices);
/* if you have defined sequence for t_end as default value of id column, you may change the upper row as "insert into t_end(prices) values(v_prices);" and there would be no need for "v_id_end" */
end;
and when you issue insert into jim values(4,'Sim',45,100); command, you'll also have values inserted into t_end.
If there is only one table, then the SELECT and the INSERT are redundant.
CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
:NEW.PRICES := :NEW.AGE + :NEW.PRICE;
END;
Possible duplicate

PL/SQL Triggers

I am using these tables:
flights (flno, origin, destination, distance, departs, arrives, price)
aircraft (aid, aname, crusingrange)
employees (eid, ename, salary)
certified (eid,aid)
and I need to create a trigger that displays a warning when inserting an employee with "666" anywhere in his/her name.
This is what I came up with so far; I am lost with the rest of it.
set serveroutput on
create or replace trigger emp_warning
before insert
on employees
for each row
declare
v_name;
begin
select e.ename into v_ename
from employees e
A trigger cannot "display a warning"; a trigger can raise an exception.
In the context of the body of a before insert for each row trigger, the value being supplied for the column is available from :NEW.columname
For example:
BEGIN
IF :NEW.ename LIKE '%666%' THEN
RAISE_APPLICATION_ERROR(-20000, 'ename contains ''666''.');
END IF;
END;
It's not mandatory that you use the RAISE_APPLICATION_ERROR. You could emit some line(s) using DBMS_OUTPUT.PUT_LINE... the line could include whatever text you wanted, including the word "warning". But this isn't really a display of a warning.
Use a check constraint instead of a trigger:
alter table empoloyees modify ename check (ename not like '%666%');
I finally figured it out thanks for you help. This my answer to the trigger question.
set serveroutput on
create or replace trigger name_warning
before insert on employees
for each row
begin
if :new.ename like '%666%' then
dbms_output.put_line('Warning employees name contains 666');
end if;
end;
/

Will I miss any changes if I replace an oracle trigger while my application is running?

I'm wondering if I will miss any data if I replace a trigger while my oracle database is in use. I created a toy example and it seems like I won't, but one of my coworkers claims otherwise.
create table test_trigger (id number);
create table test_trigger_h (id number);
create sequence test_trigger_seq;
--/
create or replace trigger test_trigger_t after insert on test_trigger for each row
begin
insert into test_trigger_h (id) values (:new.id);
end;
/
--/
begin
for i in 1..100000 loop
insert into test_trigger (id) values (test_trigger_seq.nextval);
end loop;
end;
/
--/
begin
for i in 1..10000 loop
execute immediate 'create or replace trigger test_trigger_t after insert on test_trigger for each row begin insert into test_trigger_h (id) values (:new.id); end;';
end loop;
end;
/
ran the two loops at the same time
select count(1) from test_trigger;
COUNT(1)
100000
select count(1) from test_trigger_h;
COUNT(1)
100000
create or replace is locking the table. So all the inserts will wait until it completes. Don't worry about missed inserts.
I think you might be going about testing this in the wrong way. Your insert statements won't take any time at all and so the replacement of the trigger can fit in through the gaps between inserts. As least this is what I infer due to the below.
If you change your test to ensure you have a long running SQL statement, e.g.
create table test_trigger (id number);
create table test_trigger_h (id number);
create sequence test_trigger_seq;
create or replace trigger test_trigger_t
after insert on test_trigger for each row
begin
insert into test_trigger_h (id) values (:new.id);
end;
/
insert into test_trigger
select level
from dual
connect by level <= 1000000;
If you then try to replace the trigger in a separate session it will not occur until after the insert has completed.
Unfortunately, I can't find anything in the documentation to back me up; this is just behavior that I'm aware of.
Following URL answers that trigger can be modified while application is running. its will a "library cache" lock and NOT a "data" lock. Oracle handles it internally without you worrying abt it.
Check out question raised by Ben- Can a trigger be locked; how would one determine that it is?
-- Run this from session 2:
select * from v$access where object = upper('test_trigger_t');

PL/SQL calling a function inside a trigger

I am trying to create a function using Oracle PL/SQL that stores the current date into a variable. This function will be called inside a trigger. The trigger (among other things) will insert this variable into a new table that has already been created. My code complies and runs without errors, but it doesn't work. Nothing happens. The trigger is an ON DELETE trigger, so when I delete one row from the original table, it just stays. Any clues what I am missing? Thank you.
Function:
CREATE OR REPLACE FUNCTION get_date (i_stdid archive_student.stdid%TYPE)
RETURN date
AS
v_date DATE;
BEGIN
SELECT CURRENT_DATE INTO v_date FROM DUAL;
RETURN v_date;
END;
Function call inside trigger:
CREATE OR REPLACE TRIGGER ARCHIVE_DELETED_STUDENT
AFTER DELETE ON STUDENT
FOR EACH ROW
DECLARE
v_date archive_student.change_date%TYPE;
BEGIN
-- other if statements here that are working properly
v_date := get_date(:old.stdid);
INSERT INTO archive_student (change_date) VALUES (v_date);
END;
You doing well, check this SQLFiddle.
Only one thing - you missed stdid while inserting into archive_student so it is null after insert.

Resources