从游标批量获取

我们可以从游标中获取一个或多个集合:


DECLARE
TYPE NameList IS TABLE OF employees.last_name%TYPE;
TYPE SalList IS TABLE OF employees.salary%TYPE;
CURSOR c1 IS SELECT last_name, salary FROM employees WHERE salary > 10000;
names NameList;
sals SalList;
TYPE RecList IS TABLE OF c1%ROWTYPE;
recs RecList;
PROCEDURE print_results IS
BEGIN
dbms_output.put_line('Results:');
IF names IS NULL OR names.COUNT = 0 THEN
RETURN; -- Don't print anything if collections are empty.
END IF;
FOR i IN names.FIRST .. names.LAST
LOOP
dbms_output.put_line(' Employee ' || names(i) || ': $' ||
sals(i));
END LOOP;
END;
BEGIN
dbms_output.put_line('--- Processing all results at once ---');
OPEN c1;
FETCH c1 BULK COLLECT INTO names, sals;
CLOSE c1;
print_results;
dbms_output.put_line('--- Processing 7 rows at a time ---');
OPEN c1;
LOOP
--LIMIT restricts the number of records to fetch.
FETCH c1 BULK COLLECT INTO names, sals LIMIT 7;
EXIT WHEN c1%NOTFOUND;
print_results;
END LOOP;
-- Loop exits when fewer than 7 rows are fetched. Have to
-- process the last few. Need extra checking inside PRINT_RESULTS
-- in case it is called when the collection is empty.
print_results;
CLOSE c1;
dbms_output.put_line('--- Fetching records rather than columns ---');
OPEN c1;
FETCH c1 BULK COLLECT INTO recs;
FOR i IN recs.FIRST .. recs.LAST
LOOP
-- Now all the columns from the result set come from a single record.
dbms_output.put_line(' Employee ' || recs(i).last_name || ': $'
|| recs(i).salary);
END LOOP;
END;

From: https://bytes.com/topic/oracle/insights/754491-bulk-fetching-cursor

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值