pl / sql嵌套表对象上的oracle动态sql

时间:2013-01-22 14:06:36

标签: oracle plsql oracle11g

CREATE TYPE PROD_RPT_DTL  IS object
(
    DIVISION_ID NUMBER(6),
    DIVISION_NM VARCHAR2(100), 
    DEPT_ID NUMBER(10), 
    DEPT_NM VARCHAR2(100),
    WORK_FLOOR_ID NUMBER(10),
    ...........................
)/
CREATE TYPE PROD_RPT_DTL_TABLE is TABLE OF PROD_RPT_DTL

- >过程返回PROD_RPT_DTL_TABLE类型是

PROCEDURE sp_GetAllInQtyDetailForCutting(V_PROD_RPT_DTL out PROD_RPT_DTL_TABLE);

- >现在我的程序正在执行类似下面的代码

DECLARE
    v_PROD_RPT_DTL PROD_RPT_DTL_TABLE:=PROD_RPT_DTL_TABLE();
    vv_PROD_RPT_DTL PROD_RPT_DTL_TABLE:=PROD_RPT_DTL_TABLE();
    WHEREClause varchar2(500);
    QUERY VARCHAR2(2000);
    CUR SYS_REFCURSOR;
BEGIN
    --calling procedure to return valued in v_PROD_RPT_DTL
    sp_GetAllInQtyDetailForCutting(v_PROD_RPT_DTL);
    WHEREClause := ' Where division_id=1'; ; 
    --> now v_PROD_RPT_DTL is a nested table and i have to apply some dynamic filter on it.
    QUERY := 'SELECT * bulk collect INTO vv_PROD_RPT_DTL FROM Table(cast(v_PROD_RPT_DTL AS PROD_RPT_DTL_TABLE))' || WHEREClause ;
    open cur for QUERY;
    .................................................
END ;

当我运行整件事时我会遇到错误

ORA-00904: "V_PROD_RPT_DTL": invalid identifier 
ORA-06512: at "SCHEMA.PKG_PROD_REP_MDL", line 52 
ORA-06512: at line 4  !

第52行是QUERY:= ......

请问我的PL / SQL有什么问题。

1 个答案:

答案 0 :(得分:1)

做这样的事情:

SQL> create procedure sp_getallinqtydetailforcutting(v_prod_rpt_dtl out prod_rpt_dtl_table)
  2  is
  3  begin
  4    V_PROD_RPT_DTL := PROD_RPT_DTL_TABLE(PROD_RPT_DTL(1, 'a', 1, 'a', 1),
  5                                         PROD_RPT_DTL(2, 'a', 1, 'a', 1),
  6                                         PROD_RPT_DTL(1, 'b', 1, 'a', 1));
  7  end;
  8  /

Procedure created.

SQL> declare
  2      v_prod_rpt_dtl    prod_rpt_dtl_table;
  3      whereclause varchar2(500);
  4      CUR SYS_REFCURSOR;
  5  begin
  6      --calling procedure to return valued in v_PROD_RPT_DTL
  7      sp_GetAllInQtyDetailForCutting(v_PROD_RPT_DTL);
  8      WHEREClause := ' Where division_id=1';
  9      open CUR for 'SELECT PROD_RPT_DTL(DIVISION_ID , DIVISION_NM, '
 10                   || 'DEPT_ID, DEPT_NM, WORK_FLOOR_ID) FROM Table(:b1)'
 11                   || WHEREClause using v_PROD_RPT_DTL;
 12      fetch cur bulk collect into v_PROD_RPT_DTL;
 13      close cur;
 14
 15      for idx in 1..v_PROD_RPT_DTL.count
 16      loop
 17        dbms_output.put_line(v_PROD_RPT_DTL(idx).DIVISION_ID
 18                             || ',' || v_PROD_RPT_DTL(idx).DIVISION_NM);
 19      end loop;
 20  end;
 21  /
1,a
1,b

PL/SQL procedure successfully completed.