2014-09-28 29 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

相關問題