My stored procedure has this structure:
if Count 1>0 then
count 2 with (condition 1,)
if Count 2>0 then
count 3 with (condition 1,2)
if Count 3>0 then
count 4 with(condition 1,2,3,)
if Count 4>0 then
count 5 with (condition 1,2,3,4,)
if Count 5>0 then
count 6 with (condition 1,2,3,4,5)
if count 6>0 then
count 7 where (condition 1,2 := +,3,4,5)
…
like wise 60 filters are there.
This filter in where clause goes on until it satisfies count = 1 where clause filters are like:
cnt1 checking filter only on DD_EQP_SIZE if cnt is more than 1 then goes adding one more filter DD_EQP_TYPE,
if again cnt is more than 1 then adds DD_DEL_COUNTRY etc.
and at end checking againg only filter DD_EQP_TYPE.
like wise many more filter are there.
In the end we want tarif ID based on the eqpt type and epqt size ,deliver country and rec country tarif will be calculated and sometimes we end up no ID after appling all filters . so after confirming CNT3=0 then adding and removing one by one filters.
For example, if there is no country-specific tarif for this equipment, we'll default to the international tarif, and so on for each of the optional filters that help getting a "better" tarif but, if not found, can be omitted.
i want to reduce multiple line of code and if conditions without effecting performance.
Here is the start of the function (I tried to reduce as much as I can; see it complete and working in a fiddle):
Create OR REPLACE function Fun_Dem_Ref (P_PARTNER_CODE in varchar2 , P_del_country in varchar2,P_rec_country in varchar2,
P_cnSize in varchar2 , P_cnType in varchar2)
RETURN VARCHAR2 IS
result VARCHAR2(50);
CNTn NUMBER; CNT3 NUMBER;CNT4 NUMBER;CNT5 NUMBER;CNT6 NUMBER;CNT7 NUMBER;CNT8 NUMBER;
CNT9 NUMBER; CNT10 NUMBER; CNT11 NUMBER; CNT12 NUMBER;CNT13 NUMBER; CNT14 NUMBER; CNT15 NUMBER;
CNT16 NUMBER; CNT17 NUMBER; CNT18 NUMBER; CNT19 NUMBER;N_DD_REF NUMBER;VAR_DND_TYPE VARCHAR2(2);
VAR_DND_SIZE VARCHAR2(2);
VAR_DND_DEL_COUNTRY VARCHAR2(2);
CNTR1 NUMBER;
CNTR2 NUMBER;
endDate DATE := SYSDATE;
begin
--IF CNT2 > 0 THEN
SELECT COUNT(*) INTO CNT3 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = P_cnType AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
IF CNT3 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = P_cnType AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
END IF;
IF CNT3=0 THEN
SELECT COUNT(*) INTO CNT4 FROM TEMP_DEMDET_TARIF ;
IF CNT4 >0 THEN SELECT COUNT(*) INTO CNT5 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE;
IF CNT5 >0 THEN SELECT COUNT(*) INTO CNT6 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize;
IF CNT6 >0 THEN SELECT COUNT(*) INTO CNT6 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = P_cnType;
IF CNT7>0 THEN
VAR_DND_TYPE := P_cnType;
SELECT COUNT(*) INTO CNT8 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country;
IF CNT8 >0 THEN VAR_DND_DEL_COUNTRY := P_del_country;
ELSE
VAR_DND_DEL_COUNTRY := '+';
SELECT COUNT(*) INTO CNT9 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
IF CNT9 = 1 THEN SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
result:= N_DD_REF;
else
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+'
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_TYPE := '+';
SELECT COUNT(*) INTO CNT10 FROM TEMP_DEMDET_TARIF
WHERE DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
IF CNT10>0 THEN
VAR_DND_DEL_COUNTRY := P_del_country;
SELECT COUNT(*) INTO CNT11 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
IF CNT11 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_DEL_COUNTRY := '+';
SELECT COUNT(*) INTO CNT12 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY ;
IF CNT12 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = P_cnSize
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_SIZE := '+';
SELECT COUNT(*) INTO CNT13 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType;
IF CNT13>=1 THEN
SELECT COUNT(*) INTO CNT14 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country;
IF CNT14 >=1 THEN
SELECT COUNT(*) INTO CNT15 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country;
IF CNT15 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_DEL_COUNTRY := '+';
SELECT COUNT(*) INTO CNT16 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY ;
IF CNT16 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = P_cnType
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_TYPE := '+';
SELECT COUNT(*) INTO CNT17 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country;
IF CNT17 >=1 THEN
SELECT COUNT(*) INTO CNT18 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country;
IF CNT18 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE =P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = P_del_country
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
ELSE
VAR_DND_DEL_COUNTRY := '+';
SELECT COUNT(*) INTO CNT19 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY ;
IF CNT19 = 1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY;
result:= N_DD_REF;
ELSE
SELECT COUNT(*)INTO CNTR1 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
IF CNTR1=1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = P_rec_country;
result:= N_DD_REF;
ELSE
SELECT COUNT(*) INTO CNTR2 FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
IF CNTR2 =1 THEN
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = P_PARTNER_CODE
AND DD_EQP_SIZE = VAR_DND_SIZE
AND DD_EQP_TYPE = VAR_DND_TYPE
AND DD_DEL_COUNTRY = VAR_DND_DEL_COUNTRY
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
ELSE
SELECT count(*) into CNTn FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = '+'
AND DD_EQP_SIZE = '+'
AND DD_EQP_TYPE = '+'
AND DD_DEL_COUNTRY = '+'
AND DD_REC_COUNTRY = '+';
END IF;
if CNTn = 1 then
SELECT DD_REF INTO N_DD_REF FROM TEMP_DEMDET_TARIF
WHERE DD_PARTNER_CODE = '+'
AND DD_EQP_SIZE = '+'
AND DD_EQP_TYPE = '+'
AND DD_DEL_COUNTRY = '+'
AND DD_REC_COUNTRY = '+';
result:= N_DD_REF;
END IF;
END IF;
END IF;
END IF;
RETURN result;
END Fun_Dem_Ref;
IFs to a dozen) an end to the function: what should it return in the end? Add two small call examples, with one row of data per table and two calls to the function: one that reaches that nested return, one that stops in the middle (let's say on the 5thIF): what do they return? Create a fiddle (db<>fiddle, SQL Fiddle, …) where you ensure your queries work (for example you'll add a name to the function…), which will get everybody a starting point to understand and solve the problemWHERE pool_code = pool_code). Why are you fine with an arbitrary POOL_DND_PARAM for the POOL_CODE? I would rather expect you loop through its different POOL_DND_PARAMs. Or are you dealing with a flawed (i.e. non-normalized) database model here where you expect to find the same POOL_DND_PARAM in all rows for the POOL_CODE? Or is POOL_CODE unique in TAB_MHD_POOL andROWNUM = 1completely superfluous? And why do you select into VAR_DND_PARAM at all? You are not using it.