Oracle 使用大容量收集为集合分配多个列

Oracle 使用大容量收集为集合分配多个列,oracle,plsql,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.las

我不知道为什么我会在这段代码中出现这个错误,请帮助我调试这段代码,谢谢

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;
emp_t声明为ROWTYPE,其中包括所有列, 但在检索记录时,您只检索员工id、工资、经理id、, 这可能是你犯错误的原因

试试这个:

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;

第二个问题似乎还有其他问题,如类型差异VAR和REC

使用一个或另一个:

  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


很可能你正在寻找第二种形式。使用您正在尝试批量收集的。请注意在记录声明中使用的。这是每列类型的别名。

您能解释一下为什么在将代码更改为第二种形式后仍会出错。@Asfakul我没有尝试过,但乍一看这是同一个问题:您选择了列的子集,但rec仍然使用%ROWTYPE声明为整行。简单地说:如果您使用%ROWTYPE,您应该选择*。否则,您需要一个自定义记录类型。但对于游标,我可以声明一个rowtype变量,然后将列的子集提取到该变量中。我想这是不允许的collection@Asfakul对不起,我之前的评论有点过于简单了。表%ROWTYPE是对应于完整表行的记录。因此,请求选择*。但是游标%ROWTYPE是与SELECT x、y、z、WHERTHE语句生成的虚拟表相对应的行。但也许这需要另一个问题?我知道这是一个附带问题,但如何在DELETE子句中使用这种技术呢?甚至可以使用类似的技术从删除的行中检索多个列吗?
  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)