Oracle Sql trigger before update of column - sql

I want to create an update trigger for a table called purchases that will update the total cost field and also log a record in the purchases log table to indicate which purchase was updated.
I don't have much experience with creating triggers but so far this is what i've got
CREATE OR REPLACE TRIGGER Update_Purchase_Audit BEFORE
UPDATE ON Purchases
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
WHEN(NEW.p_id>0)
DECLARE total_cost INTEGER;
BEGIN
set total_cost := (:NEW.quantity * :NEW.unitcost);
:NEW.total_cost:=total_cost*0.165;
INSERT INTO Purchase_Log(eventdate, p_id, descrip)
VALUES(SYSDATE, :NEW.p_id, :NEW.product);
END;
/
I get this error when creating the trigger:
trigger created with compilation errors. When I run show errors; I get ORA-00922: missing or invalid option
Any suggestion? Thanks

CREATE OR REPLACE TRIGGER Update_Purchase_Audit
BEFORE UPDATE ON Purchases
FOR EACH ROW
WHEN (NEW.p_id > 0)
DECLARE
total_cost number;
BEGIN
total_cost := :NEW.quantity * :NEW.unitcost;
:NEW.total_cost := total_cost * 0.165; --<< this is ok BEFORE update
-->> Do you have table relations here? THis may need to go AFTER update
-- in separate trigger
INSERT INTO Purchase_Log (eventdate, p_id, descrip)
VALUES(SYSDATE, :NEW.p_id, :NEW.product);
END;
/

Related

Oracle Trigger - Condition before insert

I don't know what my problem when created new trigger.
Is my syntax correct? Thanks! Console Logging pane
p/s: This my console display when I try to insert values
CREATE OR REPLACE TRIGGER EX03_3
BEFORE INSERT ON HR.CHITIETDATHANG
FOR EACH ROW
DECLARE
TONGHANG NUMBER; -- Total Items
HANGHIENCO NUMBER; -- Items present
HANGDABAN NUMBER; -- Items was sales.
BEGIN
-- Get total Items
SELECT SUM(MH.SOLUONG) INTO TONGHANG
FROM HR.MATHANG MH;
-- Get total Items was sales
SELECT SUM(CTDH.SOLUONG) INTO HANGDABAN
FROM HR.CHITIETDATHANG CTDH;
-- Items present
HANGHIENCO := TONGHANG - HANGDABAN;
IF(HANGHIENCO >= HANGDABAN) THEN
HANGHIENCO := HANGHIENCO-1;
INSERT INTO HR.CHITIETDATHANG VALUES(:NEW.SOHOADON,:NEW.MAHANG,
:NEW.GIABAN,:NEW.SOLUONG,:NEW.MUCGIAMGIA);
ROLLBACK;
END IF;
NULL;
END;
Seem two critical mistakes
1) trigger tries to insert into HR.CHITIETDATHANG in the body of insert trigger of HR.CHITIETDATHANG.
2) to use rollback after an insert statement is useless.
Note : I can see nothing relevant to make raise no_data_found in those select statements. EX03_3 and EX04_4 are confused, as Kaushik Nayak says.

ORACLE SQL trigger and SUM()

I have a table COMMANDE and a table REGROUPE. I have that function:
CREATE OR REPLACE PROCEDURE multiplicateur(a NUMBER, taxes NUMBER, c OUT NUMBER)
IS
BEGIN
c := a * taxes ;
END multiplicateur;
/
I'm trying to make a trigger using that founction to update a total price with taxes from a command that can contain more than one item. I tried this but it doesn't want to work:
create or replace TRIGGER MAJ_PRIX_COMMANDE
AFTER INSERT OR UPDATE OR DELETE ON REGROUPE
FOR EACH ROW
DECLARE
resultat NUMBER;
BEGIN
UPDATE COMMANDE
SET COMMANDE.prixTotal = multiplicateur((CAST((SELECT SUM(prixRegroupe)FROM REGROUPE WHERE REGROUPE.numCommande = :NEW.numCommande)AS NUMBER)),1.15,resultat)
WHERE COMMANDE.numCommande = :NEW.numCommande;
END;
Can someone help me?
How about this? I sent this from my iPhone; this code hasn't been tested.
create or replace TRIGGER MAJ_PRIX_COMMANDE
AFTER INSERT OR UPDATE OR DELETE ON REGROUPE
FOR EACH ROW
DECLARE
resultat NUMBER;
l_groupe NUMBER; --will store sum based on numCommande
BEGIN
--retrieve sum of prixRegroupe
SELECT SUM(r.prixRegroupe)
INTO l_groupe
FROM regroupe r
WHERE r.numCommande = :NEW.numCommande;
--call procedure and pass the sum of priRegroupe
multiplicateur(l_groupe, 1.15, resultat);
--use precedures out argument to update commande
UPDATE COMMANDE c
SET c.prixTotal = resultat
WHERE c.numCommande = :NEW.numCommande;
END;
This code won't work. It will result in a mutating trigger error. Within a trigger you cannot query or modify the same table that the trigger is based on until after the trigger has completed execution.
ORA-04091: table name is mutating, trigger/function may not see it
Your problem right now is that you're querying the regroupe table while the trigger is trying to insert/update the same table. This cannot be accomplished.
You'll need to find a way to get the sum of the prixRegroup column some other way. It's late if I think of anything I'll respond tomorrow.

Oracle SQL Creating Trigger to Increment a Sequence Number For Each Row

I'm trying to create a trigger but I have learned I can not design it as in my first attempt, which I'm showing below. This will cause a 'mutating table' error due to selecting from the table as it is being modified. It actually didn't cause this error when inserting only one record at a time, but when I insert multiple records at once it does.
The purpose of the trigger is to count the number of records in the table where the customer is equal to the customer about to be inserted, and to set the new order_num value as count+1. I also have a public key value set by the trigger which draws from a sequence. This part works ok once I remove the order_num part of the trigger and the associated SELECT. How can I achieve what I am trying to do here? Thanks in advance.
CREATE OR REPLACE TRIGGER t_trg
BEFORE INSERT ON t
FOR EACH ROW
DECLARE
rec_count NUMBER(2,0);
BEGIN
SELECT COUNT(*) INTO rec_count
FROM t
WHERE customer_id = :NEW.customer_id;
:NEW.order_num:= rec_count+1;
:NEW.order_pk_id:= table_seq.NEXTVAL;
END;
Two triggers and temp table approach can provide solution to you seek, preventing mutating table error. However performance will most likely suffer.
create global temporary table cust_temp(customer_id number, cust_cnt number);
create or replace trigger t_trig1
before insert on t
declare
begin
insert into cust_temp select customer_id, count(*) from t group by customer_id;
end;
/
CREATE OR REPLACE TRIGGER t_trg2
BEFORE INSERT ON t
FOR EACH ROW
DECLARE
rec_count number;
BEGIN
BEGIN
SELECT cust_cnt INTO rec_count
FROM cust_temp
WHERE customer_id = :NEW.customer_id;
EXCEPTION when no_data_found then rec_count := 0;
END;
:NEW.order_num:= rec_count+1;
:NEW.order_pk_id:= table_seq.NEXTVAL;
update cust_temp set cust_cnt = rec_count + 1
where customer_id = :NEW.customer_id;
END;
/

Oracle invalid trigger ORA-04098

I cannot figure out where is the problem. here is my trigger
my produkt table has attributes: produktid, name, manufacturing_price, sale_price
i am trying to make sale price 1.1 value of manu price
CREATE OR REPLACE TRIGGER "predajna_cena"
AFTER update of "MANUFACTURING_PRICE" on "PRODUKT"
begin
UPDATE PRODUKT SET SALE_PRICE = MANUFACTURING_PRICE*1.1;
end;
/
ALTER TRIGGER "predajna_cena" ENABLE;
apex is showing Object Status INVALID
and i cannot change manufacturing price
error ORA-04098: trigger ' ' is invalid and failed re-validation
thanks
CREATE OR REPLACE TRIGGER "predajna_cena"
AFTER update of "MANUFACTURING_PRICE" on "PRODUKT"
begin
UPDATE PRODUKT SET SALE_PRICE = MANUFACTURING_PRICE*1.1;
end;
/
If you were able to make this trigger to work, it will update all prices after each and every update of the table "PRODUKT". Probably not what you want.
For such case, you should use a BEFORE UPDATE ... FOR EACH ROW trigger, changing the product price before inserting it :NEW."SALE_PRICE" = :NEW."MANUFACTURING_PRICE"*1.1.
Something like that maybe:
CREATE OR REPLACE TRIGGER "predajna_cena"
BEFORE UPDATE OF "MANUFACTURING_PRICE" ON "PRODUKT"
FOR EACH ROW
BEGIN
:NEW.SET SALE_PRICE := :NEW.MANUFACTURING_PRICE*1.1;
END;
Untested. Beware of typos!
Depending your needs an other option would use a virtual column instead of a trigger to achieve the same result.
DROP TRIGGER "predajna_cena";
ALTER TABLE "PRODUKT" DROP COLUMN "MANUFACTURING_PRICE";
ALTER TABLE "PRODUKT" ADD "MANUFACTURING_PRICE"
GENERATED ALWAYS AS ("SALE_PRICE" * 1.1) VIRTUAL;
Untested. Beware of typos!

Oracle SQL Trigger mutating when implemeneted

Having trouble with this trigger when it runs suring a insert or update operation. The trigger gets created without errors though. The objective is to check if the invoice_total is bigger than the total of payment_total + credit_total. Any help would be much appreciated:
Create or Replace Trigger invoices_before_update_payment
Before Insert or Update On invoices
For Each Row
Declare
InvTotal Number;
t_payment Number;
t_credit Number;
Begin
select invoice_total, payment_total, credit_total
Into
InvTotal, t_payment, t_credit
From invoices
Where invoice_id = :New.invoice_id;
DBMS_OUTPUT.PUT_LINE(InvTotal);
If (InvTotal) < (t_payment + t_credit)
Then
Raise_application_error(-20005, 'Invoice total is larger than the credit total and payment total');
End if;
End;
/
You can't select from the table that a trigger is firing for, else you get this error.
Why not simply use the :new values in your trigger?
BEGIN
IF :new.invoice_total > :new.payment_total + :new.credit_total THEN
I reversed the relational operator based on the error message semantics.