gpt4 book ai didi

oracle - 使用 BULK COLLECT 将多个列分配给集合

转载 作者:行者123 更新时间:2023-12-04 00:08:19 25 4
gpt4 key购买 nike

我不确定为什么这段代码会出现此错误,请帮助我调试此代码提前致谢。

declare
type emp_t is table of employees%rowtype
index by pls_integer;
rec emp_t;
begin
select employee_id,salary,manager_id bulk collect into rec
from employees where rownum <100;

forall i in 1..rec.last
update employees
set salary=salary+10
where employee_id=rec(i).employee_id;

end;

ORA-06550: line 7, column 3:
PL/SQL: ORA-00913: too many values
ORA-06550: line 6, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

我已将代码更改为以下格式,但它仍然给我“表达式类型错误”

declare 
type emp_t is table of employees%rowtype
index by pls_integer;
rec emp_t;

begin
for val in (
select employee_id,salary,manager_id
from employees where rownum <100)

loop
rec:=val;

end loop;

forall i in 1..rec.last
update employees
set salary=salary+10
where employee_id=rec(i).employee_id;

end;

最佳答案

使用一个或另一个:

  TYPE emp_t IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
-- ^^^^^^^^^^^^^^^^^
-- each record is "one row" of table `employees`

...

SELECT * BULK COLLECT INTO rec FROM employees WHERE ROWNUM < 100;
-- ^
-- get all columns for each row

或者

  TYPE emp_rec IS RECORD (
employee_id employees.employee_id%TYPE,
salary employees.salary%TYPE,
manager_id employees.manager_id%TYPE
);
TYPE emp_t IS TABLE OF emp_rec
-- ^^^^^^^
-- each record only contains the necessary fields

...

SELECT employee_id,salary,manager_id BULK COLLECT INTO rec FROM employees WHERE ROWNUM < 100;
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- get only what we need (and in that particular order)

很可能您正在寻找第二种形式。使用 custom type for the RECORD您正在尝试批量收集。请注意使用 %TYPE attribute在备案声明中。这是每列类型的别名。

关于oracle - 使用 BULK COLLECT 将多个列分配给集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087449/

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