0% found this document useful (0 votes)
35 views

CH Function

The function average_product_price calculates and returns the average price of products with a given product type ID. It uses a SELECT statement to retrieve the average price from the products table where the product type ID matches the parameter, stores it in a variable, and returns the variable. The function Bofn_Mcib_Ld_Datedefault returns the date a loan account should be defaulted, if applicable. It loops through loan due dates and outstanding amounts, finds the first date where outstanding is negative, and returns this date or a date 31 days later, unless there is no default date, in which case it returns null. The function show_description returns the description of a course from the course

Uploaded by

O
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

CH Function

The function average_product_price calculates and returns the average price of products with a given product type ID. It uses a SELECT statement to retrieve the average price from the products table where the product type ID matches the parameter, stores it in a variable, and returns the variable. The function Bofn_Mcib_Ld_Datedefault returns the date a loan account should be defaulted, if applicable. It loops through loan due dates and outstanding amounts, finds the first date where outstanding is negative, and returns this date or a date 31 days later, unless there is no default date, in which case it returns null. The function show_description returns the description of a course from the course

Uploaded by

O
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FUNCTION WITH NO CURSOR

CREATE OR REPLACE FUNCTION average_product_price (


p_product_type_id IN INTEGER
) RETURN NUMBER AS
v_average_product_price NUMBER;
BEGIN
SELECT AVG(price)
INTO v_average_product_price
FROM products
WHERE product_type_id = p_product_type_id;
RETURN v_average_product_price;
END average_product_price;

LD DATE DEFAULT:

CREATE OR REPLACE FUNCTION Bofn_Mcib_Ld_Datedefault(REF_NO IN


VARCHAR2)

RETURN DATE IS

PLUGGED_TODAY DATE;
AMT_ARREARS NUMBER;
DATE_DUE DATE;
AMT_OUTSTANDING NUMBER;
DATE_DEFAULT DATE;
CURRENT_DUE_DATE DATE;
TODAYS_DUE_AMT NUMBER;
PLUGGED_TMP_DATE DATE; -- Temporary variable

-- Fetch all date and CUMULATIVE OUSTANDING BAL in reverse order


of date

CURSOR C1 IS

SELECT due_date DUE_DATE,


SUM(amt_settled-amt_due) over (PARTITION BY
contract_ref_no ORDER BY due_date) cum_outstanding
FROM(SELECT w.contract_ref_no, SUM(w.amount_due) amt_due,
SUM(w.amount_settled) amt_settled, w.due_date
FROM ldtbs_amount_due w WHERE w.CONTRACT_REF_NO = REF_N AND
W.DUE_DATE<(SELECT TODAY FROM STTM_DATES F WHERE F.BRANCH_CODE='001')
GROUP BY w.contract_ref_no,w.due_date)
ORDER BY DUE_DATE ASC;
BEGIN

--GETS TODAY'S DATE

SELECT S.TODAY INTO PLUGGED_TODAY FROM STTM_DATES S WHERE


S.BRANCH_CODE = '001';

-- TO CHECK IF CONTRACT HAVE AMOUNT ARREARS

SELECT SUM(B.amount_due)-SUM(B.AMOUNT_SETTLED) INTO AMT_ARREARS FROM


CSTB_AMOUNT_DUE B WHERE b.due_date < (SELECT TODAY FROM STTM_DATES C
WHERE C.BRANCH_CODE='001') AND b.CONTRACT_REF_NO=REF_NO GROUP BY
B.CONTRACT_REF_NO;

--CHECK IS LOAN HAVE AND ARREARS, RETURNS NULL AND EXIT FUNCTION IN
CASE IT DOES NOT HAVE ARREARS--

IF AMT_ARREARS<=0 THEN

RETURN NULL;
END IF;
--From this point client ni mtundu...

PLUGGED_TMP_DATE := PLUGGED_TODAY;
DATE_DEFAULT := NULL;
FOR LN_REC IN C1
LOOP

CURRENT_DUE_DATE := LN_REC.DUE_DATE;
TODAYS_DUE_AMT := LN_REC.CUM_OUTSTANDING;

IF TODAYS_DUE_AMT <0 THEN

DATE_DEFAULT:= CURRENT_DUE_DATE;

EXIT;
END IF;
PLUGGED_TMP_DATE :=CURRENT_DUE_DATE;
END LOOP;

IF (PLUGGED_TODAY-DATE_DEFAULT)>30 THEN

RETURN (DATE_DEFAULT+31);

ELSE
RETURN NULL;
END IF;

END;

/
Funtion 3

CREATE OR REPLACE FUNCTION show_description


(i_course_no course.course_no%TYPE)
RETURN varchar2
AS
v_description varchar2(50);
BEGIN
SELECT description
INTO v_description
FROM course
WHERE course_no = i_course_no;
RETURN v_description;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN('The Course is not in the database');
WHEN OTHERS
THEN
RETURN('Error in running show_description');
END;

You might also like