如何将查询参数传递给程序?

时间:2014-12-05 10:59:12

标签: oracle plsql oracle-apex

PROCEDURE drawComponent (title IN VARCHAR2) IS
BEGIN
    htp.p('<tr>');
    htp.p('<td class="row-hdr"><strong>' || title || '</strong></td>');
    for rec in (SELECT CELLS.ID as CELLID, CELLS.NUM as CELLNUM, CELLS.A_ID as ID, TBL.REMOVED as REMOVED FROM CELLS LEFT OUTER JOIN TBL ON CELLS.A_ID = TBL.ID ORDER BY CELLS.NUM)  
...

我需要在上述程序中添加CELLSA_IDTBL作为参数。我不确定类型和语法。

澄清:

对于不同的表,不同的列,将多次调用过程。身体是一样的,只有我提到的那些参数不同。

1 个答案:

答案 0 :(得分:1)

您可以(ab)使用DYNAMIC SQL将表格parameter传递给procedure

SQL> CREATE OR REPLACE
  2  PROCEDURE p(
  3      table_name IN VARCHAR2)
  4  AS
  5  TYPE ref_typ
  6  IS
  7    REF
  8    CURSOR;
  9      var_ref ref_typ;
 10      emp_no NUMBER ;
 11    BEGIN
 12      OPEN var_ref FOR 'SELECT EMPNO FROM '||table_name;
 13      LOOP
 14        FETCH var_ref INTO emp_no ;
 15      EXIT
 16    WHEN var_ref%NOTFOUND;
 17      DBMS_OUTPUT.PUT_LINE('empno --> '||emp_no);
 18    END LOOP;
 19    CLOSE var_ref;
 20  END;
 21  /

Procedure created.

让我们执行该程序并查看。

SQL> set serveroutput on
SQL> BEGIN
  2     p('emp');
  3  END;
  4  /
empno --> 7369
empno --> 7499
empno --> 7521
empno --> 7566
empno --> 7654
empno --> 7698
empno --> 7782
empno --> 7788
empno --> 7839
empno --> 7844
empno --> 7876
empno --> 7900
empno --> 7902
empno --> 7934

PL/SQL procedure successfully completed.

SQL>