2015-04-22 16 views
0

一個存儲過程我有一個表t_time在那裏我有下面的屬性使用的是Oracle

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; 

回答

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

+0

非常感謝... – Spider