我开始在Oracle中创建一个序列。在浏览oracle文档时,我得到了这个原型
Create Sequence SeqTest5
Start With 0
Increment by 1
Min value 0
Maxvalue 8
NoCycle --I got to know we can give this as 'Cycle' which will again
-- Repeat the loop. But my doubt is why cannot we specify number of
-- Loops. Such as Cycle 20
NoCache --I got to know we can give certain buffer lenght via cache
你可以解释一下我为什么不能宣布它,因为我已经尝试过并得到了这个错误
1 create sequence seqtest4
2 cache 30,
3* cycle 20,
SQL> /
cache 30,
*
ERROR at line 2:
ORA-00933: SQL command not properly ended
例如: -
TNUM
0
1
4
2
3
5
6
7
8
这个0-8应写10次并停止。
答案 0 :(得分:1)
您无法指定周期数;只是你想要循环。 CREATE SEQUENCE
语法为here。
上面的CREATE SEQUENCE
存在一些问题:
CYCLE 20
- 您只能指定CYCLE
或NOCYCLE
。默认值为NOCYCLE
。CYCLE
,则还必须指定MAXVALUE
。附录:根据实际要求更新问题,即计数1-8十次。这是没有序列的方法;它基于一个常用的Oracle技巧来生成数字序列:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 8
上述声明将按顺序输出数字1到8。要重复十次,你需要做另一个“1到10”计数器,然后将它交叉连接到“1到8”,然后确保它正确排序。这有点复杂,可以在最终答案中看到:
SELECT SeqCounter FROM (
SELECT SeqCounter, CycleCounter FROM (
SELECT LEVEL AS SeqCounter FROM DUAL CONNECT BY LEVEL <= 8)
CROSS JOIN (
SELECT LEVEL AS CycleCounter FROM DUAL CONNECT BY LEVEL <= 10)
) ORDER BY CycleCounter, SeqCounter
上述声明将提供问题中要求的输出。
答案 1 :(得分:0)
用模数函数最好处理这个吗?
CREATE SEQUENCE seqtest5 START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 80 NOCYCLE;
SELECT mod(seqtest5.nextval, 9) from dual;
0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, ...
ORA-08004: sequence SEQTEST5.NEXTVAL exceeds MAXVALUE and cannot be instantiated