Sql oracle trigger error - sql

Hi I was wondering if someone can help me adjust my trigger. I have only been working with triggers for only couple of weeks and quite new to it. My trigger is suppose to add $75 per day a boat is return late to the customer_balance account. If the boat is return early then it adds $20 per day the boat was return early to the customer_Balance account. I keep getting errors trying to get this trigger to work right so I was wondering can help or explain to me what I am doing wrong. Thank you.
create or replace trigger trig_penalty
before update of customer_balance
on customer for each row
declare
i number;
s varchar(20);
begin
select customer_balance into s
from customer
where customer_ID =:new.customer_id;
i := :new.charter_returndate - :old.charter_duedate;
if i>=0 then
dbms_output.put_line('Late return fee added');
update customer
set customer_balance=customer_balance+75*i
where customer_id=s;
else
dbms_output.put_line('Early return refund added');
update customer
set customer_balance=customer_balance+20*i
where customer_id=s;
end if;
end;
/

Well you have to understand that a trigger is an artifact that work for (in your case) each row that are being modified. So you are doing unecessary work on it.
Try this:
create or replace trigger trig_penalty
before update of customer_balance
on customer for each row
declare
i number;
s varchar(20);
begin
--You dont need to select the id is it is already on :new.customer_id
-- your trigger is for UPDATE
---select customer_balance into s
--- from customer
--- where customer_ID := :new.customer_id;
i := :new.charter_returndate - :old.charter_duedate;
if i>=0 then
--Do not use dbms_output in an internal object.
--dbms_output.put_line('Late return fee added');
--You don't need to run an update for an update trigger you
-- just need to set the new value on :NEW.FIELD
--update customer
-- set customer_balance=customer_balance+75*i
-- where customer_id=s;
--As you select this value I'm using the OLD which means the value that
--Was already on the table
:NEW.customer_balance := :OLD.customer_balance+75*i;
else
--dbms_output.put_line('Early return refund added');
--update customer
-- set customer_balance=customer_balance+20*i
-- where customer_id=s;
:NEW.customer_balance := :OLD.customer_balance+20*i;
end if;
end;
/
That should do it. Remeber for an BEFORE trigger the value setted on :NEW will end up on the table.

Related

how do i verify a row before and after update

Need to code in a way that if a buyer buys quantity more than inventory console should display a message that you can buy only available quantity as of now, rest of the quantity will be updated soon, also if the inventory is sufficient to buy it should display please go ahead
In short, "function, procedure to verify the quantity on hand before insert a row in table order_line and to update also the quantity on hand of table inventory"
CREATE OR REPLACE PACKAGE order_package IS
global_inv_id NUMBER (6);
global_quantity NUMBER (6);
PROCEDURE create_new_order(current_c_id NUMBER,
current_meth_pmt VARCHAR2, current_os_id NUMBER);
PROCEDURE create_new_order_line(current_o_id NUMBER);
END;
/
CREATE OR REPLACE PACKAGE BODY order_package IS
PROCEDURE create_new_order(current_c_id NUMBER,
current_meth_pmt VARCHAR2, current_os_id NUMBER) AS
current_o_id NUMBER;
BEGIN
SELECT order_seq.NEXTVAL
INTO current_o_id
FROM dual;
INSERT INTO orders
VALUES(current_o_id, sysdate,current_meth_pmt, current_c_id,
current_os_id);
COMMIT;
create_new_order_line(current_o_id);
END create_new_order;
PROCEDURE create_new_order_line(current_o_id NUMBER)AS
BEGIN
INSERT INTO order_line
VALUES(current_o_id,global_inv_id, global_quantity);
COMMIT;
END create_new_order_line;
END;
/
You don't show your inventory table and your procedures don't seem to have a quantity ordered value, so some of this is conjecture. What you might want to do is first update that table and use the RETURNING INTO clause to get the updated inventory.
UPDATE inventory SET global_quantity = global_quantity - order_quantity
WHERE global_inv_id = current_c_id
RETURNING global_quantity INTO l_global_quantity;
IF l_global_quantity < 0 THEN
ROLLBACK;
raise_application_error( -20001, 'You ordered too much!' );
ELSE
[... create order goes here ...]
END IF;
Is current_c_id is the item being ordered? This will raise an exception, which should be caught by whatever is calling your procedure. How you display the error to the user will depend on the application layer being used.

Updating the record of same table when new record is inserted or updated in oracle

I am new to learning Oracle. I have a task in which I need to update value of any previous record if new record contains its reference.
Table structure is as below :
Review_Table
(review_id number pk,
review_name varchar2,
previous_review number null,
followup_review number null
)
Here previous_review and followup_review columns are objects of same table i.e Review_table.
Now consider we have two records in Review_table A and B, A does not have any previous or followup review. When user creates/updates the record B and he selects record A as previous record, then we want to automatically update (via trigger) the value of A record's followup review with B's Review ID.
I have tried writing following trigger
create or replace trigger "REVIEW_T1"
AFTER insert or update on "REVIEW_TABLE"
for each row
begin
update REVIEW_TABLE
set review_follow_up_review = :new.REVIEW_ID
where REVIEW_ID = :new.REVIEW_PREVIOUS_REVIEW;
end;
But I am getting error as : REVIEW_TABLE is mutating, trigger/function may not see it ORA-06512
I have tried searching everything but was unable to find any solution for it
TL;DR: No trigger, no mutating. Do not use trigger to change another row in the same table.
I absolutely agree with #StevenFeuerstein's comment:
I also suggest not using a trigger at all. Instead, create a package that contains two procedures, one to insert into table, one to update. And within these procedures, implement the above logic. Then make sure that the only way developers and apps can modify the table is through this package (don't grant privs on the table, only execute on the package).
Take a look at the following example.
Prepare the schema:
create table reviews (
id number primary key,
name varchar2 (32),
previous number,
followup number
);
create or replace procedure createNextReview (name varchar2, lastId number := null) is
lastReview reviews%rowtype;
nextReview reviews%rowtype;
function getLastReview (lastId number) return reviews%rowtype is
begin
for ret in (
select * from reviews where id = lastId
for update
) loop return ret; end loop;
raise_application_error (-20000, 'last review does not exist');
end;
procedure insertReview (nextReview reviews%rowtype) is
begin
insert into reviews values nextReview;
exception when others then
raise_application_error (-20000, 'cannot insert next review');
end;
procedure setFollowUp (nextId number, lastId number) is
begin
update reviews set
followup = nextId
where id = lastId
;
exception when others then
raise_application_error (-20000, 'cannot update last review');
end;
begin
if lastId is not null then
lastReview := getLastReview (lastId);
end if;
nextReview.id := coalesce (lastReview.id, 0)+1;
nextReview.name := name;
nextReview.previous := lastId;
insertReview (nextReview);
if lastReview.Id is not null then
setFollowUp (nextReview.id, lastReview.Id);
end if;
exception when others then
dbms_output.put_line (
'createNextReview: '||sqlerrm||chr(10)||dbms_utility.format_error_backtrace ()
);
end;
/
Execute:
exec createNextReview ('first review')
exec createNextReview ('next review', 1)
See the outcome of work done:
select * from reviews;
ID NAME PREVIOUS FOLLOWUP
---------- ---------------- ---------- ----------
1 first review 2
2 next review 1
First you need to read about triggers, mutating table error and compound triggers: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS2005
Your trigger is AFTER UPDATE OR INSERT. Means if you run UPDATE OR INSERT statements on this table, the trigger will fire. But you are trying to update the same table again inside your trigger, which is compl. wrong.
I think you can fix this by rewriting this as a before trigger, rather than an after trigger.

PL/SQL ORACLE: Trigger updates on delete

I am trying to make a trigger that increases the booked value and decreases the available value whenever new record is inserted inside the table ticket_price. If a record is deleted, I want it to decrease the booked value and increase the available value.
Although I am able to successfully make the trigger work for INSERT, I am unable to do the same for updating the values on deletion of a record.T his is the error I get whenever I try to delete a record
ORA-01403: no data found
ORA-06512: at "K.CAL", line 6
ORA-04088: error during execution of trigger 'K.CAL'
Just to clarify, I am updating values in another table, not the same table I am deleting!
Here is my code for the trigger:
CREATE OR REPLACE TRIGGER cal
BEFORE INSERT OR DELETE ON TICKET_PRICE FOR EACH ROW
DECLARE
V_TICKET TICKET_PRICE.TICKETPRICE%TYPE;
V_BOOKED FLIGHTSEAT.BOOKED_SEATS%TYPE;
V_AVAILABLE FLIGHTSEAT.AVAILABLE_SEATS%TYPE;
BEGIN
SELECT BOOKED_SEATS,AVAILABLE_SEATS
INTO V_BOOKED,V_AVAILABLE
FROM FLIGHTSEAT
WHERE SEAT_ID=:NEW.SEAT_ID;
IF INSERTING THEN
V_BOOKED:=V_BOOKED+1;
V_AVAILABLE:=V_AVAILABLE-1;
UPDATE FLIGHTSEAT
SET BOOKED_SEATS=V_BOOKED, AVAILABLE_SEATS=V_AVAILABLE
WHERE SEAT_ID=:NEW.SEAT_ID;
ELSIF DELETING THEN
V_BOOKED:=V_BOOKED-1;
V_AVAILABLE:=V_AVAILABLE+1;
UPDATE FLIGHTSEAT
SET BOOKED_SEATS=V_BOOKED, AVAILABLE_SEATS=V_AVAILABLE
WHERE SEAT_ID=1;
END IF;
END;
You have correctly surmised that :new.seat is not available on the update for a delete. But neither is it available for the select and ow could you know sea_id=1 was need to be updated? For reference to Deleted row data use :Old.column name; is this case use :old_seat_id for both select and update.
But you don't need the select at all. Note: Further you have an implied assumption that seat_id is unique. I'll accept that below.
create or replace trigger cal
before insert or delete on ticket_price
for each row
declare
v_seat_id flightseat.seat_id%type;
v_booked flightseat.booked_seats%type;
begin
if INSERTING then
v_booked := 1;
v_seat_id := :new.seat_id;
else
v_booked := -1;
v_seat_id := :old.seat_id;
end if;
update flightseat
set booked_seats=booked_seats+v_booked
, available_seats=available_seats-v_booked
where seat_id = v_seat_id;
end;

Oracle Sql trigger before update of column

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;
/

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.