如何从具有RETURNING子句的动态SQL返回集合

时间:2015-09-09 07:21:47

标签: oracle plsql return-value dynamic-sql bulk

我有一个像这样的PL / SQL块:

CREATE OR REPLACE TYPE NUMBER_TABLE_TYPE AS TABLE OF NUMBER;

DECLARE

    updatedEmp NUMBER_TABLE_TYPE;
    sqlstr VARCHAR2(1000);
    tableName VARCHAR2(30) := 'EMPLOYEE';
    deptId NUMBER := 12;

BEGIN

    sqlstr := 'UPDATE '||tableName||' SET SALARY = SALARY * 2 WHERE DEPT_ID = :deptId '||
              'RETURNING EMP_ID BULK COLLECT INTO :res'; 
    EXECUTE IMMEDIATE sqlstr BULK COLLECT INTO updatedEmp USING deptId;

END;
对于静态DML,

RETURNING BULK COLLECT INTO子句很简单,但在我的应用程序中它是动态的。我尝试了几种方法,但没有解决问题。知道如何实现这个目标吗?

1 个答案:

答案 0 :(得分:3)

CREATE OR REPLACE TYPE NUMBER_TABLE_TYPE AS TABLE OF NUMBER;

DECLARE

    updatedEmp NUMBER_TABLE_TYPE;
    sqlstr VARCHAR2(1000);
    tableName VARCHAR2(30) := 'EMPLOYEE';
    deptId NUMBER := 12;

BEGIN

    sqlstr := 'UPDATE '||tableName||' SET SALARY = SALARY * 2 WHERE DEPT_ID = :deptId '||
              'RETURNING EMP_ID  INTO :res'; 

EXECUTE IMMEDIATE sqlstr 
      USING deptId RETURNING BULK COLLECT INTO updatedEmp ;

END;