Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

Review My 'Get_Total' Function - PL/SQL

This code will be applied to a hotel database. II got a short pl/sql code that calculates the total price a customer has to pay.

Total price is calculated based on how many nights the client has booked the room for.

v_nights represents number of nights the client's has booked.

Don't worry about exception, i'm more concerned about the body.

Regards,
F-J-K
CREATE OR REPLACE FUNCTION get_total(customer_num IN customer.CustomerID%TYPE)
RETURN NUMBER IS
	   v_price	   MotelRoom.PRICE%TYPE;
	   v_nights	   INTEGER;
	   total_price NUMBER;
BEGIN
	   SELECT mr.price, TO_DATE(checkout, 'dd') - TO_DATE(checkin, 'dd') INTO v_price, v_nights
	   FROM MotelRoom mr JOIN BookedRoom b
	   ON	mr.roomID = b.roomID
	   WHERE b.customerID = customer_num;

	   total_price := v_price * v_nights;
	   
	   RETURN total_price;
	   
EXCEPTION
	   WHEN OTHERS THEN
	   		RAISE_APPLICATION_ERROR(-20001, 'There was an error when get_total function was run. ' || SQLCODE || ', ' || SUBSTR(SQLERRM, 1, 200)); 	  
END;
/

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what is the problem you run into?
Avatar of F-J-K

ASKER

I just would like you to confirm whether my PL/SQL is written correctly or not. Unfortunately, i do not have the access to test the code currently...
TO_DATE(checkout, 'dd') - TO_DATE(checkin, 'dd')

seems incorrect ...
http://www.techonthenet.com/oracle/functions/to_date.php

day of month substracted will give you bad results when checkin is 30th and checkout on 4th... you will own some money to the customer.

instead, presuming that both fields are datetime, simply:
( TRUNC(checkout) - TRUNC(checkin))
Avatar of F-J-K

ASKER

Hm, ok. I will check it out when i get access. By the way, checkin and checkout most probably is of type
TIMESTAMP
ASKER CERTIFIED SOLUTION
Avatar of shru_0409
shru_0409
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

Thanks