Oracle 中的 FORALL 语句
Oracle 中的 FORALL 语句
当要在 Oracle 中之心批量 INSERT、UPDATE 和 DELETE 操作时,可以使用 FORALL 语句。
语法:
- --语法1:
- FORALL 下标变量(只能当作下标被引用) IN 下限..上限
- sql 语句; --只允许一条 sql 语句
- --语法2:
- FORALL 下标变量 IN INDICES OF(跳过没有赋值的元素,例如被 DELETE 的元素,NULL 也算值) 集合
- [BETWEEN 下限 AND 上限]
- sql 语句;
- --语法3:
- FORALL 下标变量 IN VALUES OF 集合(把该集合中的值当作下标变量)
- sql 语句;
- create table tb1(
- id number(5),
- name varchar2(50)
- );
语法1演示:
- --批量插入演示
- declare
- type tb_table_type is table of tb1%rowtype
- index by binary_integer;
- tb_table tb_table_type;
- begin
- for i in 1..10 loop
- tb_table(i).id:=i;
- tb_table(i).name:='NAME'||i;
- end loop;
- forall i in 1..tb_table.count
- insert into tb1 values tb_table(i);
- end;
- --批量修改演示
- declare
- type tb_table_type is table of tb1%rowtype
- index by binary_integer;
- tb_table tb_table_type;
- begin
- for i in 1..10 loop
- tb_table(i).id:=i;
- tb_table(i).name:='NAMES'||i;
- end loop;
- forall i in 1..tb_table.count
- update tb1 t set row = tb_table(i) where t.id = tb_table(i).id;
- end;
- --批量删除演示
- declare
- type tb_table_type is table of tb1%rowtype
- index by binary_integer;
- tb_table tb_table_type;
- begin
- for i in 1..10 loop
- tb_table(i).id:=i;
- tb_table(i).name:='NAMES'||i;
- end loop;
- forall i in 1..tb_table.count
- delete tb1 where id = tb_table(i).id;
- end;
更多Oracle相关信息见Oracle 专题页面 http://www.bkjia.com/topicnews.aspx?tid=12
评论暂时关闭