使用 oracle 中的存储过程更新 table

update a table using a stored procedure in oracle

我有一个 table t_time,其中我有以下属性

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 * from t_time where time_key = (select max(time_key) from t_time);

74937   31-12-2015  2015    4   12  5   5   27-12-2015  02-01-2016  17-07-2009  201512

我想编写一个存储过程,这样当我指定年份时,t_time 应该与所有键和其他属性一起插入..

喜欢

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 是那个星期的星期六 cal_st_dt_of_wk 是那个星期的星期日

谁能给我一个开始的想法..

time_key -  time_key + 1
calendar_dt - select sysdate from dual;
cal_year    - select extract(year from sysdate) from dual;
cal_quarter - select case when extract(month from sysdate) in (1,2,3) then 1
                     case when extract(month from sysdate) in (4,5,6) then 2
                     case when extract(month from sysdate) in (7,8,9) then 3
                     case when extract(month from sysdate) in (10,11,12) then 4 else 0 end as cal_quarter from dual;
cal_month   - select extract(month from sysdate) from dual; 
cal_week    - select to_char(to_date(sysdate),'ww') from dual;  
week_in_month - select to_char(to_date(sysdate),'w') from dual;
cal_st_dt_of_wk -  select trunc(sysdate,'iw')-1 from dual;
cal_end_dt_of_wk - select trunc(sysdate,'iw')+5 from dual;
rfrsh_dt - select sysdate from dual;
cal_yyyymm - select to_char(sysdate,'yyyymm') from dual;

好的,内容很多 ;) 但我认为这现在应该可以了:

-- provide year as YYYY
CREATE OR REPLACE PROCEDURE fill_table (year IN VARCHAR2) IS
  date_holder DATE := TO_DATE ('01.01.' || year,'DD.MM.YYYY');
BEGIN
  WHILE (TO_CHAR (date_holder,'YYYY') = year) LOOP
  INSERT INTO t_time 
     VALUES (1, 
             date_holder,
             extract(year from date_holder), 
             case when extract(month from date_holder) in (1,2,3) then 1
                  when extract(month from date_holder) in (4,5,6) then 2
                  when extract(month from date_holder) in (7,8,9) then 3
                  when extract(month from date_holder) in (10,11,12) then 4 else 0 END, 
             extract(month from 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;
/

所以基本思路是:

  • 1.1.<YEAR> 日期开始
  • 一天一天相加并按照您的描述插入值

日历周、月中周、周的开始和结束似乎有问题......无论如何 - 这种方法应该没问题。我也省略了正确的密钥计算 - 最好的选择是 SEQUENCE.

p.s.: 检查this demo.