gpt4 book ai didi

sql - BULK COLLECT INTO 多个集合

转载 作者:行者123 更新时间:2023-12-05 00:49:50 26 4
gpt4 key购买 nike

我想知道是否使用 BULK COLLECT INTO多个集合保证行中的相应列在嵌套表中具有相同的索引。

例子:

CREATE TABLE tx AS
SELECT CAST(level AS INT) AS col1, level || 'a' AS col2, level || 'b' as col3
FROM dual
CONNECT BY level <= 100000;

数据:
col1    col2   col3
1 1a 1b
2 2a 2b
3 3a 3b
4 4a 4b
5 5a 5b
... ... ...

和代码:
DECLARE
CURSOR cur IS SELECT /*+parallel(4)*/col1, col2, col3 FROM tx;

TYPE t1 IS TABLE of INT;
TYPE t2 IS TABLE of VARCHAR2(20);

vt1 t1;
vt2 t2;
vt3 t2;
BEGIN
OPEN cur;
FETCH cur BULK COLLECT INTO vt1, vt2, vt3 LIMIT 1000;

LOOP
FOR i IN 1..vt1.COUNT LOOP
DBMS_OUTPUT.put_line('i => '|| i || ' vt1 => ' || vt1(i)
|| ' vt2 => '|| vt2(i) || ' vt3 =>' || vt3(i));
END LOOP;

EXIT WHEN cur%NOTFOUND;
FETCH cur BULK COLLECT INTO vt1, vt2, vt3 LIMIT 1000;
END LOOP;
CLOSE cur;
END;

输出:
i => 1 vt1 => 22418 vt2 => 22418a vt3 =>22418b
i => 2 vt1 => 22419 vt2 => 22419a vt3 =>22419b
i => 3 vt1 => 22420 vt2 => 22420a vt3 =>22420b
i => 4 vt1 => 22421 vt2 => 22421a vt3 =>22421b
i => 5 vt1 => 22422 vt2 => 22422a vt3 =>22422b
i => 6 vt1 => 22423 vt2 => 22423a vt3 =>22423b

所以在每一行中总是有相同的前缀。或者在某些情况下可能会得到类似的东西:
i => 1 vt1=> 100 vt2=>200a vt3=>300b

备注:我知道我可以定义记录类型并只使用一个集合。

寻找来自官方来源的答案。

最佳答案

Oracle 的 PL/SQL 语言引用文档没有明确说明来自同一行的值将具有相同的索引,但确实说明了:

PL/SQL processes the BULK COLLECT clause similar to the way it processes a FETCH statement inside a LOOP statement.



还有关于 BULK COLLECT的章节给出隐含假设来自同一行的值将具有相同索引的示例。例如。这来自版本 12.1 的 Oracle PL/SQL 语言引用中的示例 12-22。

FOR i IN names.FIRST .. names.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(' Employee ' || names(i) || ': $' || sals(i));
END LOOP;


见: https://docs.oracle.com/database/121/LNPLS/tuning.htm#GUID-541A8B35-9B8F-432F-9E30-E73305A6E17C__BABCCJCB

关于sql - BULK COLLECT INTO 多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46693975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com