Not familiar with writing PL/SQL code and would like to use a loop to eliminate doubling up of an SQL statement in a procedure. The logic is:
Since the sql insert statement is the same in both, only the what is returned by the variables differ, I would like to figure out how to modify the above so that it runs the insert statement twice and with the slightly adjusted variables only on the day when the condition is true. Rather than having to have the insert statement written out twice in the script as is currently the case. One of the reasons would like to have only one insert statement, is that when making changes to the query, only have to do it once rather than twice and risk possibly missing something.
Thanks.
Code:
-- today <= fall census date + 1
if trunc(sysdate) <= trunc(fall_census + 1) then
v_term := substr(current_term,1,4);
v_weekno := v_term || lpad(trunc((trunc(sysdate) - trunc(last_fall_census))/7), 2, 0);
if substr(v_weekno,5) between 0 and 21 then
v_sem := 20; -- want 20, 30, 40
else
v_sem := 30; -- want 30, 40
end if;
insert into histstats
Select a.*
FROM
(....
....
WHERE substr(term,1,4) = v_term
AND substr(term,5,2) >= v_sem
...
...
) a
;
commit;
end if;
-- today >= fall census date + 1
if trunc(sysdate) >= trunc(fall_census + 1) then
v_term := substr(current_term,1,4) + 1;
v_weekno := v_term || lpad(trunc((trunc(sysdate) - trunc(fall_census))/7), 2, 0);
if substr(v_weekno,5) between 0 and 21 then
v_sem := 20; -- want 20, 30, 40
else
v_sem := 30; -- want 30, 40
end if;
insert into histstats
Select a.*
FROM
(....
....
WHERE substr(term,1,4) = v_term
AND substr(term,5,2) >= v_sem
...
...
) a
;
commit;
end if;
Since the sql insert statement is the same in both, only the what is returned by the variables differ, I would like to figure out how to modify the above so that it runs the insert statement twice and with the slightly adjusted variables only on the day when the condition is true. Rather than having to have the insert statement written out twice in the script as is currently the case. One of the reasons would like to have only one insert statement, is that when making changes to the query, only have to do it once rather than twice and risk possibly missing something.
Thanks.