使用oracle

时间:2015-04-22 18:41:20

标签: oracle stored-procedures plsql

我有一个表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以及所有键和其他属性。

喜欢

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

1 个答案:

答案 0 :(得分:2)

好吧,它有很多东西;)但我认为现在应该可以了:

-- 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