建立 Sequence 語法
create sequence <schema>.<table_name>_s
increment by 1
start with 1
maxvalue 99999999999999999999
minvalue 1
nocycle;
其中 :
incerment: 每次增加的數值
start : Sequence 開始數值
maxvalue : Sequence 最大值
nocycle : 當 Sequence 達最大值時,不重頭開始
取得下一筆 Sequence 值
select <Sequence_name>.nextval[@db_link]
from dual;
查詢目前 Sequence 值
語法 1:
select <Sequence_name>.currval[@db_link]
from dual;
Mark 提供: 若 "目前所在的 DB Session" 尚未執行 nextval, 就先執行 currval, 則會得到
ORA-08002: sequence DB_SESSION_ID.CURRVAL is not yet defined in this session 錯誤.
語法 2:
select last_number - 1
from all_sequences
where sequence_name = '<Sequence_name>';
修改 Sequence
如: Alter Sequence <Sequence_name> MaxValue 999999999999999;
Alter Sequence 其他線上參考: http://download.oracle.com/docs/cd/B13789_01/server.101/b10759/statements_2011.htm
注意事項
1) Sequence 不能直接用在 "等式 :=" 之後.
如: vID := tomSeq.nextval;
2) Sequence 可以直接用在 "SQL" 之中.
如: insert into tomTable values( tomSeq.nextval, ... );