this procedure code is to apply a discount to the new insert. The problem I’m having is too many records are being fetched. How would i fix this? do i use a trigger or a function?
Ok i have created a trigger but am i getting a PLS-00103: Encountered the symbol “=” when expecting one of the following: := . ( @ % ;
CREATE OR REPLACE PROCEDURE CHECK_DISCOUNT
AS
V_COUNT NUMBER;
V_C_NO APPOINTMENT.C_NO%TYPE;
V_BILL APPOINTMENT.BILL%TYPE;
BEGIN
SELECT C_NO,COUNT(C_NO)
INTO V_C_NO,V_COUNT
FROM APPOINTMENT
GROUP BY C_NO;
SELECT BILL
INTO V_BILL
FROM APPOINTMENT;
IF V_COUNT=3 THEN
V_BILL:=V_BILL * 0.9;
END IF;
UPDATE APPOINTMENT
SET BILL = V_BILL
WHERE C_NO=:new.C_NO;
COMMIT;
END;
/
Create or replace TRIGGER CHECK_DISCOUNT
BEFORE INSERT OR UPDATE OF C_NO ON APPOINT
FOR EACH ROW
DECLARE CURSOR C_APPTMENT IS
SELECT C_NO,COUNT(C_NO)
FROM APPOINTMENT GROUP BY C_NO;
VISIT NUMBER; V_C_NO APPOINTMENT.C_NO%TYPE;
V_BILL APPOINTMENT.BILL%TYPE;
V_TEN NUMBER(3):=0.9;
BEGIN
LOOP
OPEN C_APPTMENT; FETCH C_APPTMENT INTO V_C_NO, VISIT;
EXIT WHEN C_APPTMENT%NOTFOUND;
SELECT BILL INTO V_BILL FROM APPOINTMENT;
IF VISITS =3 THEN V_BILL=V_BILL * V_TEN
WHERE :NEW.C_NO=V_C_NO;
UPDATE APPOINTMENT SET BILL:= V_BILL
WHERE:NEW.C_NO=V_C_NO;
END LOOP;
CLOSE C_APPTMENT;
END;
When using select … into you must assure you only select one row.
Usually you will add a where clause with primary- or unique key columns. That way you are sure to select only one row. You will also have to provide for instances where no rows are selected. Add a exception handler to trap no_data_found exceptions