使用集合更新

时间:2014-04-06 16:17:56

标签: oracle oracle11g

我正在测试一个代码,其中基本上我想使用存储在表类型变量中的值从表中删除。这是一个ID列表。到目前为止,我已经尝试了以下..

DECLARE 
 TYPE REC_TYPE IS TABLE OF DEPT.DEPARTMENT_ID%TYPE;
 T_TYPE REC_TYPE;

BEGIN

    SELECT DEPARTMENT_ID BULK COLLECT INTO T_TYPE
    FROM
    (
        SELECT DEPARTMENT_ID FROM DEPT
        INTERSECT
        SELECT DEPARTMENT_ID FROM EMP
     );

     FORALL C IN T_TYPE.FIRST..T_TYPE.LAST
       DELETE FROM DEPT 
        WHERE DEPARTMENT_ID=T_TYPE(C).DEPARTMENT_ID;

      DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
    END;



CREATE TABLE DEPT 
AS
SELECT * FROM DEPARTMENTS;

CREATE TABLE EMP 
AS
SELECT * FROM EMPLOYEES;

但我收到了错误

PLS-00487: Invalid reference to variable 'DEPT.DEPARTMENT_ID%TYPE'
ORA-06550: line 17, column 29:
PL/SQL: ORA-22806: not an object or REF
ORA-06550: line 16, column 8:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.

不确定在哪里纠正。非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

这里的问题是

TYPE REC_TYPE IS TABLE OF DEPT.DEPARTMENT_ID%TYPE;

是Department_id的数据类型表。

所以它不是一行,因此调用

T_TYPE(C).DEPARTMENT_ID;

无效,因为t_type(c)中没有department_id 参考应该是

T_TYPE(C)

代替。

虽然陈旧,但这很有趣:http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/05_colls.htm