2014-09-28 87 views
1

我不确定为什么我得到此代码的错误,请帮助我在调试此代码时提前致谢。使用BULK COLLECT将多个列分配给一个集合

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; 

回答

4

使用一个或另一个:

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。这是每列的类型的别名。

+0

即使在将代码更改为第二种形式之后,您能否解释它为什么会出现错误? – redsoxlost 2014-09-28 18:28:30

+0

@Asfakul我没有尝试,但乍一看,这是同样的问题:你选择列的_subset_,但是'rec'仍然被声明为“全行”(使用%ROWTYPE)。简单地说:如果你使用'%ROWTYPE',你应该'SELECT *'。否则,您需要一个自定义记录类型。 – 2014-09-28 18:32:28

+0

但是在游标的情况下,我可以声明一个rowtype变量,然后将列的子集提取到该变量中。我猜这是不允许在收集 – redsoxlost 2014-09-28 18:48:42

0

emp_t被声明为ROWTYPE,包括所有列,
但在检索记录时,您只能检索到employee_id,salary,manager_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; 

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

相关问题