rbarbalho
rbarbalho

Reputation: 41

Dynamic parameters in USING clause PLSQL

I have a question, I'm building a PLSQL code to execute any procedure or function the dynamic way. I'm using a metadata table to know which element sql I'll execute this way.

Process some codes to recover informations to build a similar block:

V_COMMAND := 'BEGIN PROC_EX(:1,:2,:3,:4); END;';

But the problem is in apply the command execute immediate because I need bind variable in clause USING the dynamic way. I tried to create a String variable that storage all parameters but when I executed I received a ora error ORA-01008.

I created a temporary var to storage params:

V_PARAMS := 'IN P_TABLE, IN P_WHERE, OUT O_MESSAGE, OUT O_CODE';

where: P_TABLE, P_WHERE, O_MESSAGE and O_CODE are variables declared at begin code.

And I executed the command this way:

EXECUTE IMMEDIATE V_COMMAND USING V_PARAMS;

Is it possible mapping variable the dynamic way in using clause?

Upvotes: 0

Views: 1530

Answers (1)

Olafur Tryggvason
Olafur Tryggvason

Reputation: 4874

You are looking for DBMS_SQL

Example of usage that fits your needs:

CREATE OR REPLACE PROCEDURE foo (n NUMBER, square OUT NUMBER) IS
      BEGIN square := n * n; END;/

      CREATE OR REPLACE PROCEDURE bulk_plsql 
         (n DBMS_SQL.NUMBER_TABLE, square OUT DBMS_SQL.NUMBER_TABLE) IS
      c NUMBER;
      r NUMBER;
      BEGIN
        c := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE(c, 'BEGIN foo(:bnd1, :bnd2); END;', DBMS_SQL.NATIVE);
        DBMS_SQL.BIND_ARRAY(c, 'bnd1', n);
        DBMS_SQL.BIND_ARRAY(c, 'bnd2', square);
        r := DBMS_SQL.EXECUTE(c);
        DBMS_SQL.VARIABLE_VALUE(c, 'bnd2', square);
     END;
     /

Upvotes: 1

Related Questions