如果使用了全局临时表和sequence,有时会遇到一些问题。因为全局临时表与会话(或会话中的事务)相关,而sequence与数据库级别相关。
在12C中,可以创建一个sequence,其使用范围只是针对一个给定的session。
--当全局临时表gtt在某个会话中被清空时(如commit),sequence的值不会被重置。下次调用该sequence会使用其nextval值。
SQL> connect scott/tiger
SQL> drop table gtt;
SQL> create global temporary table gtt(id number,seq_number number);
SQL> grant all on gtt to tiger;
SQL> drop sequence seq_session;
SQL> create sequence seq_session start with 1 session;
SQL> grant all on seq_session to tiger;
SQL> insert into gtt values(1,seq_session.nextval);
SQL> insert into gtt values(2,seq_session.nextval);
SQL> select * from scott.gtt;
ID SEQ_NUMBER
1 1
2 2
SQL>
commit;
SQL> select * from scott.gtt;
no rows selected
SQL>
insert into gtt values(1,seq_session.nextval);
insert into gtt values(2,seq_session.nextval);
SQL> select * from scott.gtt;
ID SEQ_NUMBER
1 3
2 4
SQL>
继续测试。
--会话结束,sequence的值会被重置
SQL>conn tiger/tiger
SQL>drop synonym gtt;
SQL>drop synonym seq_session;
SQL>create synonym gtt for scott.gtt;
SQL>create synonym seq_session for scott.seq_session;
SQL> select * from gtt;
no rows selected
SQL>
SQL> insert into gtt values(1,seq_session.nextval);
SQL> insert into gtt values(2,seq_session.nextval);
--在11g中,这里查询结果是5,6
SQL> select * from gtt;
ID SEQ_NUMBER
1 1
2 2
SQL>
SQL> commit;
SQL> select * from gtt;
no rows selected
SQL>
SQL> conn scott/tiger
SQL> insert into gtt values(1,seq_session.nextval);
SQL> insert into gtt values(2,seq_session.nextval);
--在11g中,这里查询结果是7,8
SQL> select * from gtt;
ID SEQ_NUMBER
1 1
2 2
SQL>
转载于:https://my.oschina.net/u/3761438/blog/1835875