本博客是对之前写的博客 Oracle 批量新增更新数据的补充, oracle 的知识真是多, 其实要学精任何一门知识都是要花大量时间的, 正所谓:
学如逆水行舟, 不进则退
先介绍 oracle sequence 的一些知识, sequence 顾名思义, 就是创建一个序列, 这个序列没调一次就会自动加, 利用它这个特性, 我们显然可以用来写一些批量脚本
(1)sequence 用法
oracle sequence 的基本语法, 这里只做简单介绍
创建 sequence
create sequence [序列名]
minvalue [最小值, eg:1]
nomaxvalue | maxvalue [最大值, eg:9999999]
start with [开始值, eg:1]
increment by [每次递增: eg:1]
nocycle // 一直累加, 不循环
no cache |cache [缓存值, eg:10];
更新 sequence
update sequence [序列名]
minvalue [最小值, eg:1]
nomaxvalue | maxvalue [最大值, eg:9999999]
increment by [每次递增: eg:1]
nocycle // 一直累加, 不循环
no cache |cache [缓存值, eg:10];
ps: 注意 start with 不能 update, 只能 drop sequence [序列名], 然后再 create, 序列使用是用序列名. nextval
(2)sequence 用于批量
这里举例介绍 sequence 用于批量更新新增的案例
业务场景: 要按某个数值为起点, 批量新增数据
先场景 sequence:
- create sequence seq_t_handle_info
- minvalue 1
- maxvalue 999999999999999999999999999
- start with 624203
- increment by 1
- cache 20;
- commit;
编写批量脚本:
- insert into t_stuff_dir_related
- (seq, dir_seq, create_date, create_man, stuff_id, t_item)
- select sys_guid(),
- seq_t_handle_info.nextval,
- sysdate,
- 'admin',
- b.rs_id,
- a.t_item
- from t_itm_define a
- where a.is_valid =1
来源: http://www.jianshu.com/p/f9fbd57a0537