使用BULK COLLECT向集合分配多个列

red*_*ost 2 oracle plsql

我不确定为什么此代码会出现此错误,请帮助我调试此代码。

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:
Run Code Online (Sandbox Code Playgroud)

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

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;
Run Code Online (Sandbox Code Playgroud)

Syl*_*oux 5

使用其中之一:

  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
Run Code Online (Sandbox Code Playgroud)

要么

  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)
Run Code Online (Sandbox Code Playgroud)

您很有可能正在寻找第二种形式。使用自定义类型RECORD尝试批量收集。请注意记录声明中%TYPE属性的使用。这是每列类型的别名。