update a table using a stored procedure in oracle -
i have table t_time have below attributes
time_key, calendar_dt, cal_year, cal_quarter, cal_month, cal_week, week_in_month, cal_st_dt_of_wk, cal_end_dt_of_wk, rfrsh_dt, cal_yyyymm select * t_time time_key = (select max(time_key) t_time); 74937 31-12-2015 2015 4 12 5 5 27-12-2015 02-01-2016 17-07-2009 201512
i want write stored proc such when specify year,t_time should inserted keys , other attributes..
like
for 2016
time_key calendar_dt cal_year cal_quarter cal_month cal_week week_in_month cal_st_dt_of_wk cal_end_dt_of_wk rfrsh_dt cal_yyyymm 74938 01-01-2016 2016 1 1 1 1 01-01-2016 02-01-2016 22-04-2015 201601 74939 02-01-2016 2016 1 1 1 1 01-01-2016 02-01-2016 22-04-2015 201601 74940 03-01-2016 2016 1 1 2 2 03-01-2016 09-01-2016 22-04-2015 201601 74941 04-01-2016 2016 1 1 2 2 03-01-2016 09-01-2016 22-04-2015 201601
cal_end_dt_of_wk saturday of week cal_st_dt_of_wk sunday of week
can give me idea start with..
time_key - time_key + 1 calendar_dt - select sysdate dual; cal_year - select extract(year sysdate) dual; cal_quarter - select case when extract(month sysdate) in (1,2,3) 1 case when extract(month sysdate) in (4,5,6) 2 case when extract(month sysdate) in (7,8,9) 3 case when extract(month sysdate) in (10,11,12) 4 else 0 end cal_quarter dual; cal_month - select extract(month sysdate) dual; cal_week - select to_char(to_date(sysdate),'ww') dual; week_in_month - select to_char(to_date(sysdate),'w') dual; cal_st_dt_of_wk - select trunc(sysdate,'iw')-1 dual; cal_end_dt_of_wk - select trunc(sysdate,'iw')+5 dual; rfrsh_dt - select sysdate dual; cal_yyyymm - select to_char(sysdate,'yyyymm') dual;
ok, it's lot of stuff ;) think should work now:
-- provide year yyyy create or replace procedure fill_table (year in varchar2) date_holder date := to_date ('01.01.' || year,'dd.mm.yyyy'); begin while (to_char (date_holder,'yyyy') = year) loop insert t_time values (1, date_holder, extract(year date_holder), case when extract(month date_holder) in (1,2,3) 1 when extract(month date_holder) in (4,5,6) 2 when extract(month date_holder) in (7,8,9) 3 when extract(month date_holder) in (10,11,12) 4 else 0 end, extract(month date_holder), to_char(to_date(date_holder),'ww'), to_char(to_date(date_holder),'w'), trunc(date_holder,'iw')-1, trunc(date_holder,'iw')+5, sysdate , to_char(date_holder,'yyyymm')); date_holder := date_holder +1; end loop; end; /
so basic idea is:
- start
1.1.<year>
date - add 1 day other , insert values described
there seems issue on calendar week, week in month, start , end of week though .... anyway - approach should fine. omitted correct key calculation - best option sequence
.
p.s.: check this demo.
Comments
Post a Comment