oracle生成日期维表,oracle建时间维度

用存储过程如何建立时间维度表

create PROCEDURE [dbo].[Create_time_by_day_dimension]

-- Add the parameters for the stored procedure here

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

begin try

drop table [time_by_day_dimension]

end try

begin catch

end catch

CREATE TABLE [dbo].[time_by_day_dimension] (

[time_id] [int] IDENTITY (1, 1) NOT NULL ,

[the_date] [datetime] NULL ,

[the_day] [nvarchar] (15) NULL ,

[the_month] [nvarchar] (15) NULL ,

[the_year] [smallint] NULL ,

[day_of_month] [smallint] NULL ,

[week_of_year] [smallint] NULL ,

[month_of_year] [smallint] NULL ,

[quarter] [nvarchar] (2) NULL ,

[fiscal_period] [nvarchar] (20) NULL

) ON [PRIMARY]

DECLARE @WeekString varchar(12),

@dDate SMALLDATETIME,

@sMonth varchar(20),

@iYear smallint,

@iDayOfMonth smallint,

@iWeekOfYear smallint,

@iMonthOfYear smallint,

@sQuarter varchar(2),

@sSQL varchar(100),

@adddays int

SELECT @adddays = 1 --日期增量(可以自由设定)

SELECT @dDate = '01/01/2006' --开始日期

WHILE @dDate < '12/31/2010' --结束日期

BEGIN

SELECT @WeekString = DATENAME (dw, @dDate)

SELECT @sMonth=DATENAME(mm,@dDate)

SELECT @iYear= DATENAME (yy, @dDate)

SELECT @iDayOfMonth=DATENAME (dd, @dDate)

SELECT @iWeekOfYear= DATENAME (week, @dDate)

SELECT @iMonthOfYear=DATEPART(month, @dDate)

SELECT @sQuarter = 'Q' + CAST(DATENAME (quarter, @dDate)as varchar(1))

INSERT INTO time_by_day_dimension(the_date, the_day, the_month, the_year,

day_of_month,

week_of_year, month_of_year, quarter) VALUES

(@dDate, @WeekString, @sMonth, @iYear, @iDayOfMonth, @iWeekOfYear,

@iMonthOfYear, @sQuarter)

SELECT @dDate = @dDate + @adddays

END

END

这有个例子,但不会用。@是什么意义,调用时还要设置参数么?你参考的例子是MS Sql Server的,@XX是变量的意思。我改写了一下,因为oracle没有identity自增,所以用了sequence。

首先建表:

CREATE TABLE time_by_day_dimension(

time_id int NOT NULL ,

the_date date NULL ,

the_day varchar2(15) NULL ,

the_month varchar2(15) NULL ,

the_year smallint NULL ,

day_of_month smallint NULL ,

week_of_year smallint NULL ,

month_of_year smallint NULL ,

fiscal_period varchar2(20) NULL

);

然后是创建序列:

create sequence seq_time_id start with 1 increment by 1 nocycle nocache ;

最后是过程:

create or replace

PROCEDURE Create_time_by_day_dimension

IS

WeekString varchar(12);

dDate DATE;

sMonth varchar(20);

iYear smallint;

iDayOfMonth smallint;

iWeekOfYear smallint;

iMonthOfYear smallint;

adddays int;

BEGIN

adddays := 1 ;

dDate := to_date('01/01/2006','mm/dd/yyyy');

WHILE (dDate < to_date('12/31/2010','mm/dd/yyyy'))

loop

WeekString := to_char(dDate, 'day'); --星期几

sMonth:=to_char(dDate, 'mm');--月份

iYear:= to_char(dDate, 'yyyy');--年

iDayOfMonth:=to_char(dDate, 'dd');--日(字符型)

iWeekOfYear:= to_char(dDate,'fmww');--年的第几周

iMonthOfYear:=to_number(sMonth);--日(数字型)

INSERT INTO time_by_day_dimension(time_id,the_date, the_day, the_month, the_year, day_of_month, week_of_year, month_of_year)

VALUES (seq_time_id.nextval,dDate, WeekString, sMonth, iYear, iDayOfMonth, iWeekOfYear, iMonthOfYear);

dDate := dDate + adddays;

END loop;

end;

最后少了一个字段:季度。你再学习添加一下吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值