Hi Everyone.
Let me start off by saying that this is my first attempt at dynamic sql. I've been programming with PL/SQL for about a year and a half, but this is the first d-sql I've done.
At issue is this:
I need to populate a column based on what month it is with information that is calculated based on the current date. At a very high level, this is it:
update table set month_column =
((((month1 + month2 + month3)/3) * other_data)* other data)
My code is below, and reflects my most recent attempt at success. Any suggestions would be very much appreciated.
Regards,
Tim
Let me start off by saying that this is my first attempt at dynamic sql. I've been programming with PL/SQL for about a year and a half, but this is the first d-sql I've done.
At issue is this:
I need to populate a column based on what month it is with information that is calculated based on the current date. At a very high level, this is it:
update table set month_column =
((((month1 + month2 + month3)/3) * other_data)* other data)
My code is below, and reflects my most recent attempt at success. Any suggestions would be very much appreciated.
Regards,
Tim
Code:
...
CURSOR three_cost IS
SELECT r.leaf_node, r.node_05, s.p_id, s.driver_id, s.quarter, s.year
FROM rqm437t.cost_idt_rollup r, vt_org_prod_vol_stage s
WHERE r.node_05 = s.secondary_node
AND s.quarter = 0
AND (s.secondary_node, s.p_id, s.driver_id) NOT IN (
SELECT secondary_node, p_id, driver_id
FROM rqm437t.vtl_driver_duration)
AND (s.secondary_node, s.p_id, s.driver_id, r.leaf_node) NOT IN (
SELECT secondary_node, p_id, driver_id, leaf_node
FROM rqm437t.vtl_driver_allocation)
AND (r.leaf_node, s.p_id, s.driver_id) IN (
SELECT org_unit_id, p_id, driver_id
FROM vt_org_prod_vol_forecast);
...
(variable definitions)
...
BEGIN
SELECT 'MONTH_'||(to_char(sysdate, 'MM')) INTO cmon FROM dual;
SELECT TO_CHAR(sysdate, 'YYYY') INTO cyr FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -1), 'MM')) INTO mon1 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -1), 'YYYY') INTO yr1 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -2), 'MM')) INTO mos2 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -2), 'YYYY') INTO yr2 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -3), 'MM')) INTO mos3 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -3), 'YYYY') INTO yr3 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -4), 'MM')) INTO mos4 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -4), 'YYYY') INTO yr4 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -5), 'MM')) INTO mos5 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -5), 'YYYY') INTO yr5 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -6), 'MM')) INTO mos6 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -6), 'YYYY') INTO yr6 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -7), 'MM')) INTO mos7 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -7), 'YYYY') INTO yr7 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -8), 'MM')) INTO mos8 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -8), 'YYYY') INTO yr8 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -9), 'MM')) INTO mos9 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -9), 'YYYY') INTO yr9 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -10), 'MM')) INTO mos10 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -10), 'YYYY') INTO yr10 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -11), 'MM')) INTO mos11 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -11), 'YYYY') INTO yr11 FROM dual;
SELECT 'MONTH_'||(to_char(add_months(sysdate, -12), 'MM')) INTO mos12 FROM dual;
SELECT TO_CHAR(ADD_MONTHS(sysdate, -12), 'YYYY') INTO yr12 FROM dual;
IF cmon in ('MONTH_01')
THEN dsql_cost_str3 := 'UPDATE vt_org_prod_vol_forecast -'
||' SET MONTH_01 = ) -'
||' (((((SELECT :mos3 FROM caa.cost_org_prod_vols_hist WHERE year = :yr3) -'
||' + (SELECT :mos2 FROM caa.cost_org_prod_vols_hist WHERE year = :yr2) -'
||' + (SELECT :mon1 FROM caa.cost_org_prod_vols_hist WHERE year = :yr1)) / 3) -'
||' / (SELECT MONTH_01 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' * (SELECT MONTH_01 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' WHERE org_unit_id = '||oui3s||' -'
||' AND p_id = '||pid3s||' -'
||' AND driver_id = '||did3s||')';
ELSIF cmon in ('MONTH_02')
THEN dsql_cost_str3 := 'UPDATE vt_org_prod_vol_forecast -'
||' SET MONTH_02 = ) -'
||' (((((SELECT :mos3 FROM caa.cost_org_prod_vols_hist WHERE year = :yr3) -'
||' + (SELECT :mos2 FROM caa.cost_org_prod_vols_hist WHERE year = :yr2) -'
||' + (SELECT :mon1 FROM caa.cost_org_prod_vols_hist WHERE year = :yr1)) / 3) -'
||' / (SELECT MONTH_02 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' * (SELECT MONTH_02 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' WHERE org_unit_id = '||oui3s||' -'
||' AND p_id = '||pid3s||' -'
||' AND driver_id = '||did3s||')';
ELSIF cmon in ('MONTH_03')
THEN dsql_cost_str3 := 'UPDATE vt_org_prod_vol_forecast -'
||' SET MONTH_03 = ) -'
||' (((((SELECT :mos3 FROM caa.cost_org_prod_vols_hist WHERE year = :yr3) -'
||' + (SELECT :mos2 FROM caa.cost_org_prod_vols_hist WHERE year = :yr2) -'
||' + (SELECT :mon1 FROM caa.cost_org_prod_vols_hist WHERE year = :yr1)) / 3) -'
||' / (SELECT MONTH_03 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' * (SELECT MONTH_03 FROM vt_org_prod_vol_stage -'
||' WHERE secondary_node, p_id, driver_id IN ('||snd3s||', '||pid3s||', '||did3s||')) -'
||' WHERE org_unit_id = '||oui3s||' -'
||' AND p_id = '||pid3s||' -'
||' AND driver_id = '||did3s||')';
... same pattern up to MONTH_12.
OPEN three_cost;
LOOP
FETCH three_cost INTO oui3s, snd3s, pid3s, did3s, qtr3s, yr3s;
EXIT WHEN three_cost%NOTFOUND;
EXECUTE IMMEDIATE dsql_cost_str3
USING oui3s, snd3s, pid3s, did3s, qtr3s, yr3s, mos3, mos2, mon1, cmon, yr3, yr2, yr1;
...
vt_fcb_status := 1;
END LOOP;
CLOSE three_cost;
COMMIT;
EXCEPTION
WHEN others THEN DBMS_OUTPUT.PUT_LINE('Problems: '||sqlerrm);
ROLLBACK;
vt_fcb_status := 2;
END;
/
SHOW ERRORS;