Oracle 中的 FORALL 语句
2015-09-06 15:40
288 查看
当要在 Oracle 中之心批量 INSERT、UPDATE 和 DELETE 操作时,可以使用 FORALL 语句。
语法:
Sql代码
--语法1:
FORALL 下标变量(只能当作下标被引用) IN 下限..上限
sql 语句; --只允许一条 sql 语句
--语法2:
FORALL 下标变量 IN INDICES OF(跳过没有赋值的元素,例如被 DELETE 的元素,NULL 也算值) 集合
[BETWEEN 下限 AND 上限]
sql 语句;
--语法3:
FORALL 下标变量 IN VALUES OF 集合(把该集合中的值当作下标,且该集合值的类型只能是 PLS_INTEGER BINARY_INTEGER)
sql 语句;
Sql代码
create table tb1(
id number(5),
name varchar2(50)
);
语法1演示:
Sql代码
--批量插入演示
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;
语法2演示:
Sql代码
select * from tb1;
declare
type demo_table_type is table of demo%rowtype
index by binary_integer;
demo_table demo_table_type;
begin
for i in 1..10 loop
demo_table(i).id:=i;
demo_table(i).name:='NAME'||i;
end loop;
demo_table.delete(3);
demo_table.delete(6);
demo_table.delete(9);
forall i in indices of demo_table
insert into demo values demo_table(i);
end;
select * from demo;
1 1 NAME1
2 2 NAME2
3 4 NAME4
4 5 NAME5
5 7 NAME7
6 8 NAME8
7 10 NAME10
语法3演示:
Sql代码
declare
type index_poniter_type is table of pls_integer;
index_poniter index_poniter_type;
type demo_table_type is table of demo%rowtype
index by binary_integer;
demo_table demo_table_type;
begin
index_poniter:=index_poniter_type(1,3,5,7);
for i in 1..10 loop
demo_table(i).id:=i;
demo_table(i).name:='NAME'||i;
end loop;
forall i in values of index_poniter
insert into demo values demo_table(i);
end;
select * from demo;
1 1 NAME1
2 3 NAME3
3 5 NAME5
4 7 NAME7
语法:
Sql代码
--语法1:
FORALL 下标变量(只能当作下标被引用) IN 下限..上限
sql 语句; --只允许一条 sql 语句
--语法2:
FORALL 下标变量 IN INDICES OF(跳过没有赋值的元素,例如被 DELETE 的元素,NULL 也算值) 集合
[BETWEEN 下限 AND 上限]
sql 语句;
--语法3:
FORALL 下标变量 IN VALUES OF 集合(把该集合中的值当作下标,且该集合值的类型只能是 PLS_INTEGER BINARY_INTEGER)
sql 语句;
Sql代码
create table tb1(
id number(5),
name varchar2(50)
);
语法1演示:
Sql代码
--批量插入演示
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;
语法2演示:
Sql代码
select * from tb1;
declare
type demo_table_type is table of demo%rowtype
index by binary_integer;
demo_table demo_table_type;
begin
for i in 1..10 loop
demo_table(i).id:=i;
demo_table(i).name:='NAME'||i;
end loop;
demo_table.delete(3);
demo_table.delete(6);
demo_table.delete(9);
forall i in indices of demo_table
insert into demo values demo_table(i);
end;
select * from demo;
1 1 NAME1
2 2 NAME2
3 4 NAME4
4 5 NAME5
5 7 NAME7
6 8 NAME8
7 10 NAME10
语法3演示:
Sql代码
declare
type index_poniter_type is table of pls_integer;
index_poniter index_poniter_type;
type demo_table_type is table of demo%rowtype
index by binary_integer;
demo_table demo_table_type;
begin
index_poniter:=index_poniter_type(1,3,5,7);
for i in 1..10 loop
demo_table(i).id:=i;
demo_table(i).name:='NAME'||i;
end loop;
forall i in values of index_poniter
insert into demo values demo_table(i);
end;
select * from demo;
1 1 NAME1
2 3 NAME3
3 5 NAME5
4 7 NAME7
相关文章推荐
- oracle impdp的table_exists_action详解
- 14->oracle常用的动态性能表
- 利用GoldenGate实现Oracle实时同步方案
- oracle常见受权与回收权限 grant和revoke
- 13->oracle的数字字典
- Mysql和oracle的分页问题
- oracle 存储过程 技巧
- oracle sql语句优化
- Oracle中HWM与数据库性能的探讨
- 32位win7下安装Oracle10g(10.2.0.1)和补丁Patch(10.2.0.4)笔记
- Oracle 11g oracle客户端(32位)PL/SQL develepment的安装配置
- oracle添加新用户
- plsql连接数据库ora-28547 oracle net 管理错误
- Oracle GoldenGate 简介 数据库同步 异地复制方案
- oracle中的buffer cache
- ORACLE分页查询SQL语法——最高效的分页
- oracle RMAN克隆数据库
- PLSQL怎样导出oracle表结构和数据
- Oracle定时调用存储过程
- VS本地调试oracle报错解决方法