首页 > 数据库技术 > 详细

Oracle存储过程生成日期维度

时间:2014-03-12 05:42:06      阅读:445      评论:0      收藏:0      [点我收藏+]

在数据仓库的创建过程中,往往需要创建日期维度来为以后的数据分析来服务。

方面从多个日期角度:

如:年-月-日,年-季度-月-日,年-周-日

创建表的脚本如下(存储过程的创建过程中有一步操作是向time_dimension表中插入数据,所以首先需要创建好此表)

bubuko.com,布布扣
bubuko.com,布布扣
create table TIME_DIMENSION
(
  the_date     NUMBER not null,
  date_name    NVARCHAR2(15),
  the_year     NUMBER,
  year_name    NVARCHAR2(10),
  the_quarter  VARCHAR2(10),
  quarter_name NVARCHAR2(10),
  the_month    NUMBER,
  month_name   NVARCHAR2(10),
  the_week     NUMBER,
  week_name    NVARCHAR2(10),
  week_day     NVARCHAR2(10)
)
tablespace TBS_COGNOS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    next 1
    minextents 1
    maxextents unlimited
  );
创建日期维度表脚本
bubuko.com,布布扣

 

存储过程脚本如下

bubuko.com,布布扣
bubuko.com,布布扣
CREATE OR REPLACE PROCEDURE SP_CREATE_TIME_DIMENSION(begin_date in varchar2,
                                                    end_date in varchar2) is

  /*SP_CREATE_TIME_DIMENSION: 生成时间维数据
  begin_date: 起始时间
  end_date:结束时间
  */
dDate date;
v_the_date number;
v_the_year varchar2(4);
v_the_quarter varchar2(2);
v_the_month varchar2(10);
v_the_month2 varchar2(2);
v_the_week varchar2(2);
v_the_day varchar2(10);
v_the_day2 varchar2(2);
v_week_day nvarchar2(10);

adddays int;
BEGIN
adddays := 1 ;
dDate := to_date(begin_date,yyyymmdd);

WHILE (dDate <= to_date(end_date,yyyymmdd))
loop
   v_the_date := to_number(to_char(dDate,yyyymmdd));--key值
   v_the_year:= to_char(dDate, yyyy);--
   v_the_quarter := to_char(dDate, q);--季度
   v_the_month:=to_char(dDate, mm);--月份(字符型)
   v_the_month2:=to_number(to_char(dDate, mm));--月份(数字型)
   v_the_day:=to_char(dDate, dd);--日(字符型)
   v_the_day2:=to_char(dDate, dd);
   v_the_week:= to_char(dDate,fmww);--年的第几周
   v_week_day := to_char(dDate, day); --星期几


  insert into time_dimension(the_date,date_name,the_year,year_name,
                            the_quarter,quarter_name,the_month,
                            month_name,the_week,week_name,week_day)
                      values(v_the_date,v_the_year||||v_the_month2||||v_the_day2||,v_the_year,v_the_year||,
                            v_the_year||Q||v_the_quarter,v_the_year||||v_the_quarter||季度,to_number(v_the_year||v_the_month),
                            v_the_year||||v_the_month2||,v_the_week,||v_the_week||,
                            v_week_day);

   dDate :=  dDate + adddays;
END loop;
end SP_CREATE_TIME_DIMENSION;
创建存储过程脚本
bubuko.com,布布扣

 

 

OK,存储过程创建完毕,下面我们需要传参并且只需存储过程,问题来了,如何通过PLSQL执行存储过程?之前都是在PLSQL中创建 SQL 窗口来执行procedure

结果报错,其实应该创建 命令窗口 来执行procedure

如下图:

bubuko.com,布布扣

命令窗口的执行脚本如下:

bubuko.com,布布扣
SQL> exec SP_CREATE_TIME_DIMENSION(20140101,20140630);
 
PL/SQL procedure successfully completed
 
SQL> commit;
 
Commit complete
 
SQL> select * from time_dimension;
bubuko.com,布布扣

OK,截止目前,日期维度已经生成。

Oracle存储过程生成日期维度,布布扣,bubuko.com

Oracle存储过程生成日期维度

原文:http://www.cnblogs.com/wxjnew/p/3594044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!