In my procedure I have used IF ELSIF often . The part of the procedure is as attached .
is there any other way to replace the IF ELSIF in above case and improve performance or may be remove the cursor loop completely ?
Thanks
create or replace
FUNCTION fn_tenor_bucket
(
pi_template_desc VARCHAR2,
pi_as_of_date DATE,
pi_cashflow_date DATE)
RETURN DATE
IS
PRAGMA AUTONOMOUS_TRANSACTION;
v_cashflow_date DATE;
v_start_dt DATE;
v_end_dt DATE;
v_start_dt1 DATE;
v_bucket glrs_agg.TBL_AGG_GT_TMP_TENOR_BUCKETS%ROWTYPE;
v_bucket_type VARCHAR2(20);
v_counter NUMBER :=0 ;
v_tenor_bucket VARCHAR2(10);
isBusinessDay VARCHAR2(20);
v_count NUMBER :=0;
v_inc_date DATE;
CURSOR c_get_tenor_buckets
IS
SELECT b.tenor_bucket_name ,
c.code ,
b.tenor_bucket_value ,
b.user_tenor_buckets_id
FROM glrs.tbl_glrs_user_tenor_template a ,
glrs.tbl_glrs_user_tenor_buckets b ,
glrs.tbl_glrs_tenor_bucket_type c
WHERE a.user_tenor_template_id = b.user_tenor_template_id
AND a.tenor_template_name = pi_template_desc
AND b.tenor_bucket_type_id = c.tenor_bucket_type_id
ORDER BY b.user_tenor_buckets_id;
BEGIN
DELETE FROM tbl_agg_gt_tmp_tenor_buckets;
commit;
v_start_dt:= pi_as_of_date + 1;
FOR i IN c_get_tenor_buckets
LOOP
IF i.code = 'D' THEN
v_inc_date:= v_start_dt;
WHILE (GLRS.fnc_is_business_date ('US', to_char(v_inc_date,'yyyy-mm-dd'))) = 'N' LOOP
v_count:=v_count+1;
v_inc_date:=v_inc_date+1;
END LOOP;
v_end_dt:= v_start_dt + v_count;
v_count:=0;
ELSIF i.code = 'EOW' THEN
v_end_dt := (pi_as_of_date) + (7 * i.tenor_bucket_value);
ELSIF i.code = 'EOM' THEN
v_end_dt := v_start_dt+(15*i.tenor_bucket_value);
ELSIF i.code = 'M' and i.tenor_bucket_name in ('31 - 60 Days','61 - 90 Days') THEN
v_end_dt := (v_start_dt-1)+(30*i.tenor_bucket_value);
ELSIF i.code = 'M' and i.tenor_bucket_name NOT IN ('31 - 60 Days','61 - 90 Days') THEN
--v_start_dt:=add_months(pi_as_of_date,(3+v_count))+1;
v_start_dt1:=add_months(pi_as_of_date,(3+v_count))+1;
v_end_dt := Add_Months(v_start_dt1 - 1,i.tenor_bucket_value);
v_count:=v_count+i.tenor_bucket_value;
ELSIF i.code = 'MY' THEN
v_end_dt := add_months(pi_as_of_date,12);
ELSIF i.code = 'Y' THEN
v_end_dt := Add_Months(v_start_dt - 1,i.tenor_bucket_value * 12);
ELSIF i.code = 'ALR' THEN
v_end_dt := Add_Months(v_start_dt,100 * 12);
END IF;
INSERT
INTO tbl_agg_gt_tmp_tenor_buckets
(
st_dt ,
end_dt ,
tenor_bucket ,
bucket_type ,
bucket_order ,
bucket_value
)
VALUES
(
v_start_dt ,
v_end_dt ,
i.tenor_bucket_name ,
i.code ,
i.user_tenor_buckets_id ,
i.tenor_bucket_value
);
COMMIT;
v_start_dt := v_end_dt + 1;
END LOOP;
select end_dt into v_cashflow_date from tbl_agg_gt_tmp_tenor_buckets where st_dt <=pi_cashflow_date and end_dt >=pi_cashflow_date;
RETURN v_cashflow_date;
END fn_tenor_bucket;
is there any other way to replace the IF ELSIF in above case and improve performance or may be remove the cursor loop completely ?
Thanks
create or replace
FUNCTION fn_tenor_bucket
(
pi_template_desc VARCHAR2,
pi_as_of_date DATE,
pi_cashflow_date DATE)
RETURN DATE
IS
PRAGMA AUTONOMOUS_TRANSACTION;
v_cashflow_date DATE;
v_start_dt DATE;
v_end_dt DATE;
v_start_dt1 DATE;
v_bucket glrs_agg.TBL_AGG_GT_TMP_TENOR_BUCKETS%ROWTYPE;
v_bucket_type VARCHAR2(20);
v_counter NUMBER :=0 ;
v_tenor_bucket VARCHAR2(10);
isBusinessDay VARCHAR2(20);
v_count NUMBER :=0;
v_inc_date DATE;
CURSOR c_get_tenor_buckets
IS
SELECT b.tenor_bucket_name ,
c.code ,
b.tenor_bucket_value ,
b.user_tenor_buckets_id
FROM glrs.tbl_glrs_user_tenor_template a ,
glrs.tbl_glrs_user_tenor_buckets b ,
glrs.tbl_glrs_tenor_bucket_type c
WHERE a.user_tenor_template_id = b.user_tenor_template_id
AND a.tenor_template_name = pi_template_desc
AND b.tenor_bucket_type_id = c.tenor_bucket_type_id
ORDER BY b.user_tenor_buckets_id;
BEGIN
DELETE FROM tbl_agg_gt_tmp_tenor_buckets;
commit;
v_start_dt:= pi_as_of_date + 1;
FOR i IN c_get_tenor_buckets
LOOP
IF i.code = 'D' THEN
v_inc_date:= v_start_dt;
WHILE (GLRS.fnc_is_business_date ('US', to_char(v_inc_date,'yyyy-mm-dd'))) = 'N' LOOP
v_count:=v_count+1;
v_inc_date:=v_inc_date+1;
END LOOP;
v_end_dt:= v_start_dt + v_count;
v_count:=0;
ELSIF i.code = 'EOW' THEN
v_end_dt := (pi_as_of_date) + (7 * i.tenor_bucket_value);
ELSIF i.code = 'EOM' THEN
v_end_dt := v_start_dt+(15*i.tenor_bucket_value);
ELSIF i.code = 'M' and i.tenor_bucket_name in ('31 - 60 Days','61 - 90 Days') THEN
v_end_dt := (v_start_dt-1)+(30*i.tenor_bucket_value);
ELSIF i.code = 'M' and i.tenor_bucket_name NOT IN ('31 - 60 Days','61 - 90 Days') THEN
--v_start_dt:=add_months(pi_as_of_date,(3+v_count))+1;
v_start_dt1:=add_months(pi_as_of_date,(3+v_count))+1;
v_end_dt := Add_Months(v_start_dt1 - 1,i.tenor_bucket_value);
v_count:=v_count+i.tenor_bucket_value;
ELSIF i.code = 'MY' THEN
v_end_dt := add_months(pi_as_of_date,12);
ELSIF i.code = 'Y' THEN
v_end_dt := Add_Months(v_start_dt - 1,i.tenor_bucket_value * 12);
ELSIF i.code = 'ALR' THEN
v_end_dt := Add_Months(v_start_dt,100 * 12);
END IF;
INSERT
INTO tbl_agg_gt_tmp_tenor_buckets
(
st_dt ,
end_dt ,
tenor_bucket ,
bucket_type ,
bucket_order ,
bucket_value
)
VALUES
(
v_start_dt ,
v_end_dt ,
i.tenor_bucket_name ,
i.code ,
i.user_tenor_buckets_id ,
i.tenor_bucket_value
);
COMMIT;
v_start_dt := v_end_dt + 1;
END LOOP;
select end_dt into v_cashflow_date from tbl_agg_gt_tmp_tenor_buckets where st_dt <=pi_cashflow_date and end_dt >=pi_cashflow_date;
RETURN v_cashflow_date;
END fn_tenor_bucket;