我正在编写一段代码,使用SQL Developer在Oracle 11gR2中填充时间维度
表格结构
KEY Number(8,0)
HRS Varchar2
MINS Varchar2
SECS Varchar2
我是PL / SQL的新手 - 我通常认为在伪代码中做的方式是,但PL / SQL中的for循环似乎不会这样:
key = 1
for (hrs=0; hr<=23; hr++)
{
for (min=0; min<=59; min++)
{
for (sec=0; sec<=59; sec++)
{
insert (KEY, tochar(hrs:mins:secs), tochar(hr), tochar(min), tochar(sec))
SK=+1
}
}
}
我从我找到的答案中尝试过这个:
insert into Dim_TIME
(Key,
TIME_IN24_NAME,
TIME_HOUR24,
TIME_MINUTE,
TIME_SECOND)
select
to_char(t, 'HH24:MI:SS'),
to_number(to_char(t, 'HH24'), '00'),
to_number(to_char(t, 'MI'), '00'),
to_number(to_char(t, 'SS'), '00')
from (
select trunc(sysdate) + (level - 1)/86400 as t
from dual
connect by level <= 86400
);
但不确定我如何递增键(列字段不会自动递增)。
我是如何实现这一目标的?
谢谢
答案 0 :(得分:2)
您可以创建SEQUENCE
来增加字段KEY
。
CREATE SEQUENCE dim_time_seq;
并在你的代码中使用它,
INSERT INTO dim_time
(KEY,
time_in24_name,
time_hour24,
time_minute,
time_second)
SELECT
dim_time_seq.nextval,
to_char(t, 'HH24:MI:SS'),
to_number(to_char(t, 'HH24'), '00'),
to_number(to_char(t, 'MI'), '00'),
to_number(to_char(t, 'SS'), '00')
FROM (
SELECT trunc(SYSDATE) + (LEVEL - 1)/86400 AS t
FROM dual
CONNECT BY LEVEL <= 86400
);