forall and bulk collect

本文介绍了使用PL/SQL进行批量操作的方法,包括删除、更新及收集员工数据等操作,通过FORALL语句一次发送多条SQL指令到SQL引擎,提高执行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DECLARE

TYPE DeptList IS VARRAY(20) OF NUMBER;

depts DeptList := DeptList(10, 30, 70); -- department numbers

BEGIN FORALL i IN depts.FIRST..depts.LAST

DELETE FROM emp WHERE deptno = depts(i);

END;

The above DELETE statement is sent to the SQL engine just once, even though it performs three DELETE operations:

 

 

DECLARE TYPE DeptList

IS VARRAY(10) OF NUMBER;

depts DeptList := DeptList(10,20,30,40,50);

BEGIN

FORALL j IN 3..5 -- bulk-bind only part of varray

UPDATE emp SET sal = sal * 1.10

WHERE deptno = depts(j);

END;

As the following example shows, the bounds of the FORALL loop can apply to part of a collection, not necessarily all the elements:

 

 

DECLARE

TYPE enoTab IS TABLE OF emp.empno%TYPE;

TYPE enameTab IS TABLE OF emp.ename%TYPE;

enum enoTab; -- no need to initialize

Name enameTab;

BEGIN SELECT empno, ename

BULK COLLECT INTO enum, name FROM emp;

END;

 

The target is to reduce the context switch between SQL engine and PL/SQL engine.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值