CREATE TABLE Bank
(
Bank_Id VARCHAR2(30)
, Description VARCHAR2(30)
);
INSERT INTO Bank VALUES('001', 'FNB');
INSERT INTO Bank VALUES('002', 'ABSA');
CREATE TABLE Config
(
Config NUMBER
, ConfigName VARCHAR2(30)
);
INSERT INTO Config VALUES(1, 'SUTS allocated');
INSERT INTO Config VALUES(2, 'SUTS unallocated');
INSERT INTO Config VALUES(3, 'GOV allocated');
INSERT INTO Config VALUES(4, 'GOV unallocated');
CREATE TABLE Config_Value
(
Config_Value_Id NUMBER
, Bank_Id VARCHAR2(30)
, Config_Item_Id NUMBER
, ConfigValue VARCHAR2(30)
);
INSERT INTO Config_Value VALUES(1, '001', 1, 'FNBSAD');
INSERT INTO Config_Value VALUES(2, '001', 2, 'FNBSUAD');
INSERT INTO Config_Value VALUES(3, '001', 3, 'FNBGAD');
INSERT INTO Config_Value VALUES(4, '001', 4, 'FNBGUAD');
SELECT * FROM Bank;
1 FNB
2 ABSA
SELECT * FROM Config;
1 SUTS allocated
2 SUTS unallocated
3 GOV allocated
4 GOV unallocated
SELECT * FROM Config_Value;
1 001 1 FNBSAD
2 001 2 FNBSUAD
3 001 3 FNBGAD
4 001 4 FNBGUAD
SELECT *
FROM Bank b
JOIN Config_Value cv ON b.Bank_Id = cv.Bank_Id
JOIN Config c ON cv.Config_Item_Id = c.Config
;
SELECT cv.ConfigValue
FROM Bank b
JOIN Config_Value cv ON b.Bank_Id = cv.Bank_Id
JOIN Config c ON cv.Config_Item_Id = c.Config
WHERE ConfigName IN ('SUTS allocated', 'SUTS unallocated')
;
SELECT cv.ConfigValue
FROM Bank b
JOIN Config_Value cv ON b.Bank_Id = cv.Bank_Id
JOIN Config c ON cv.Config_Item_Id = c.Config
WHERE ConfigName IN ('GOV allocated', 'GOV unallocated')
;
CREATE OR REPLACE FUNCTION c_sharp_function(bank_in IN VARCHAR2, paymentType_in
VARCHAR2)
RETURN VARCHAR2
AS
TYPE ConfigValue_ntt IS TABLE OF Config_Value.ConfigValue%TYPE;
l_ConfigValue ConfigValue_ntt;
l_csv VARCHAR(32767) := '';
BEGIN
IF UPPER(paymentType_in) LIKE 'SUTS%' THEN
SELECT cv.ConfigValue
BULK COLLECT INTO l_ConfigValue
FROM Bank b
JOIN Config_Value cv ON b.Bank_Id = cv.Bank_Id
JOIN Config c ON cv.Config_Item_Id = c.Config
WHERE ConfigName IN ('SUTS allocated', 'SUTS unallocated')
AND Description = bank_in;
ELSIF UPPER(paymentType_in) LIKE 'GOV%' THEN
SELECT cv.ConfigValue
BULK COLLECT INTO l_ConfigValue
FROM Bank b
JOIN Config_Value cv ON b.Bank_Id = cv.Bank_Id
JOIN Config c ON cv.Config_Item_Id = c.Config
WHERE ConfigName IN ('GOV allocated', 'GOV unallocated')
AND Description = bank_in;
END IF;
FOR indx IN 1..l_ConfigValue.COUNT LOOP
l_csv := l_csv || l_ConfigValue(indx) || ',';
END LOOP;
l_csv := SUBSTR(l_csv, 1, LENGTH(l_csv) - 1);
RETURN l_csv;
END;
DECLARE
l_get_csv VARCHAR2(32767);
BEGIN
l_get_csv := c_sharp_function('FNB', 'SUTS');
DBMS_OUTPUT.PUT_LINE(l_get_csv);
l_get_csv := c_sharp_function('ABSA', 'GOV');
DBMS_OUTPUT.PUT_LINE(l_get_csv);
END;
/*
FNBSAD,FNBSUAD
FNBSAD,FNBSUAD
*/