2015-11-08 76 views
0

我對plsql非常陌生,在分配任務時遇到了一些麻煩。我被要求創建一個日期維度表並用各種日期信息填充它。在PL/SQL中的兩個日期範圍之間填充日期維度

我創造了我的表如下

CREATE TABLE date_dimension_table 
( 
v_date DATE NOT NULL, 
full_date VARCHAR2(50) NOT NULL, 
day_of_week NUMBER(1,0) NOT NULL, 
day_in_month NUMBER(2,0) NOT NULL, 
day_in_year NUMBER(3,0) NOT NULL, 
last_day_of_week_flag VARCHAR2(1) NOT NULL, 
last_day_of_month_flag VARCHAR2(1) NOT NULL, 
end_of_week_date DATE NOT NULL, 
month_number NUMBER(2,0) NOT NULL, 
month_name VARCHAR2(32) NOT NULL, 
year_month CHAR(32) NOT NULL, 
quarter_number NUMBER(1,0) NOT NULL, 
year_and_quarter VARCHAR2(32) NOT NULL, 
v_year NUMBER(4,0) NOT NULL, 
CONSTRAINT date_dimension_table_PK PRIMARY KEY (v_date) 
); 

我然後創建以下程序來填充表。

create or replace PROCEDURE sp_populate_dates(
v_first_date_in DATE, 
v_second_date_in DATE) 

AS 
--Declare two variables as DATE datatypes 
v_current_date DATE; 
v_end_date  DATE; 

BEGIN 

--Assign the start date and end date to the variables 
v_current_date := TO_DATE(v_first_date_in, 'DDMMYYYY'); 
v_end_date := TO_DATE(v_second_date_in, 'DDMMYYYY'); 


--Delete whats currently in the table 
DELETE FROM date_dimension_table; 

--condition checks if the first date is before the second date 
WHILE v_current_date <= v_end_date 
LOOP 
--insert into table 
INSERT INTO date_dimension_table 
(
    v_date, 
    full_date, 
    day_of_week, 
    day_in_month, 
    day_in_year, 
    last_day_of_week_flag, 
    last_day_of_month_flag, 
    end_of_week_date, 
    month_number, 
    month_name, 
    year_month, 
    quarter_number, 
    year_and_quarter, 
    v_year  
)  
VALUES 
(
    v_current_date,         --date 
    TO_CHAR(v_current_date, 'Day, Month DD, YYYY'), --full date 
    TO_NUMBER(TO_CHAR(v_current_date, 'D')) -1,  --day in week 
    TO_CHAR(v_current_date,'DD'),     --day in month   
    TO_CHAR(v_current_date,'DDD'),     --day in year 
    CASE           --last day of week flag 
     WHEN TO_CHAR(v_current_date,'FMDay') = 'Sunday' THEN 'Y' 
     ELSE 'N' 
    END, 
    CASE           --last day of month flag 
     WHEN last_day(TO_DATE(v_current_date, 'MM/DD/YYYY')) = TO_DATE(v_current_date, 'MM/DD/YYYY') THEN 'Y' 
     ELSE 'N' 
    END, 
    CASE           --date of next sunday 
     WHEN TO_CHAR(v_current_date,'FMDay') = 'Sunday' THEN v_current_date 
     ELSE NEXT_DAY(v_current_date,'SUNDAY') 
    END, 
    TO_CHAR(v_current_date,'MM'),     --month number 
    TO_CHAR(v_current_date,'MONTH'),    --name of month 
    TO_CHAR(v_current_date,'MONTH YYYY'),   --month and year   
    TO_CHAR(v_current_date,'Q'),     --number of quarter 
    TO_CHAR(v_current_date,'YYYY Q'),    --year and quarter 
    TO_CHAR(v_current_date,'YYYY')     --year  

); 
--Increment and assign the current date value to be re-evaluated 
v_current_date := v_current_date + 1; 

END LOOP; 
END; 

由於程序具有插入來自日期範圍信息,我用兩個日期作爲輸入參數並且它投擲錯誤。

例子:

BEGIN 
sp_populate_dates(01/01/2005, 01/01/2006); 
END; 

我當時收到錯誤提示這是錯誤的類型參數的函數調用。我想知道是否有人可以幫助這個請,並告訴我我要去哪裏錯了。乾杯

回答

1

首先,你必須的過程,得到

v_first_date_in DATE, 
v_second_date_in DATE 

日期,但是你要通過甚至不是一個字符串。嘗試這樣的事情

BEGIN 
sp_populate_dates(TO_DATE('01/01/2005', 'dd/mm/yyyy'), TO_DATE('01/01/2005', 'dd/mm/yyyy')); 
END; 

另一件事是 - 你在程序中處理這些參數,所以你可能想通過VARCHAR2類型的變量。這意味着,

create or replace PROCEDURE sp_populate_dates(
v_first_date_in IN VARCHAR2, 
v_second_date_in IN VARCHAR2) 
... 

,並調用它像

BEGIN 
sp_populate_dates('01/01/2005','01/01/2005'); 
END; 

但是這裏要小心:如果日期這裏的類型是( 'DD/MM/YYYY')的過程中,你的處理日期(」 ddmmyyyy')是不正確的。

希望它會幫助!

0

如果你真的不需要任何的日期格式和分析,始終使用SQL標準DATE literal

BEGIN 
    sp_populate_dates(DATE '2005-01-01', DATE '2006-01-01'); 
END; 
相關問題