Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculate the total value of payments with the procedures and triggers?

925181Mar 21 2012 — edited Mar 27 2012
Hello!

I work for a college project and I have a big problem because professor requires to do it by the examples he gives during the lecture. Here's an example that should be applied to its base, so please help!

I have three table with that should work:

Invoice (#number_of_invoices, date, comm, total_payment, number_of_customer, number_of_dispatch)
where:
number_of_invoices primary key (number),
date (date),
comm (var2),
total_payment is UDT (din - currency in my country) - in this field should be entered value is calculated
number_of_customer and number_of_dispatch (number) are foreign keys and they are not relevant for this example

Invoice_items (#serial_number, #number_of_invoices, quantity, pin)
serial_number primary key (number),
number_of_invoices primary key (number),
quantity (number),
pin foreign keys (number)

Item (#pin, name, price, tax, price_plus_tax)
pin primary key (number),
name (var2),
price, tax, UDT (din) not relevant for this example
price_plus_tax UDT (din)

These are the triggers and procedures with my calculation should be done:

trigger1:
CREATE OR REPLACE TRIGGER  "trg1" 
BEFORE INSERT OR UPDATE OR DELETE ON Invoice_items
FOR EACH ROW
BEGIN   
	IF (INSERTING OR UPDATING) 
	THEN      
		BEGIN Invoice_items.number_of_invoices := :NEW.number_of_invoices; END; 
	ELSE
		BEGIN Invoice_items.number_of_invoices :=: OLD.number_of_invoices; END;   
	END IF;
END;
trigger2:
CREATE OR REPLACE TRIGGER  "trg2"
AFTER INSERT OR UPDATE OR DELETE ON Invoice_items
DECLARE
doc NUMBER := Invoice_items.number_of_invoices;
BEGIN   
	entire_payment (doc);
END;
procedure
CREATE OR REPLACE PROCEDURE  "entire_payment" (code_of_doc IN NUMBER) AS  
entire NUMBER := 0;
BEGIN  
	SELECT SUM (a.price_plus_tax * i.quantity) INTO entire
	FROM Item a join Invoice_items i on (a.pin = i.pin)  
	WHERE number_of_invoices = code_of_doc;
	UPDATE Invoice
	SET total_payment = entire
	WHERE number_of_invoices = code_of_doc;
END;
As you can see the procedure is called from the triggers, I have a problem at the first trigger, and I think it will be even higher in procedure because UDT, field "total_payment".
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 24 2012
Added on Mar 21 2012
15 comments
3,368 views