动态参数不是sql类型的Oracle

时间:2015-02-25 07:29:16

标签: oracle plsql record dynamic-programming rowtype

我试图做这样的事情:

create function getData(r1 IN TABLE1%ROWTYPE,col1 string, valor OUT string)RETURN string AS

  instruccion VARCHAR2(500);
  valor VARCHAR(200);
BEGIN

     valor := r1.col1;

  return valor;

END getData;

一个过程,它将记录和列的名称作为参数,并返回值和:

CREATE procedure p1 as

   vData VARCHAR2(80);
   v1 VARCHAR2(80);
   vValue VARCHAR2(80);

   inst VARCHAR2(500);

  CURSOR vTable2 IS SELECT * FROM TABLE2;

  CURSOR vTable1 IS SELECT * FROM TABLE1;

BEGIN

  --A cursor for the table with the data
  FOR d1 IN vTable1
  LOOP 
          --A cursor for the table that store the name of the columns of the    table 1 
          FOR v1 IN vTable2
          LOOP
            --get the table1 column name
            vData :=  v1.table2Col1;
            --calls the procedure that gives back the value of that record on the column name is stored in VData
            inst := 'begin getData(:d1, :vData, :vValue ); end;';


            EXECUTE IMMEDIATE inst USING in d1 , vData, OUT vValue;

            DBMS_OUTPUT.PUT_LINE(vValue);

          END LOOP;

  END LOOP;

END p1;

总而言之,我只想拥有一个在table2中存储table1名称的表,因为我希望该过程与名为table1的任何表一起使用,这段代码给出了错误"表达式是SQL类型"对于动态块的参数,我理解它,但可以找到一种新的方法来做到这一点。我尝试过很多东西,但我在甲骨文中有点新意,我真的很感激你的帮助。

1 个答案:

答案 0 :(得分:0)

  

inst:='begin getData(:d1,:vData,:vValue);结束;';

     

EXECUTE IMMEDIATE在d1,vData,OUT vValue中使用;

我认为您不需要在此处使用动态SQL 。你想要的只是传递%ROWTYPE 参数并进行处理。 %ROWTYPE被视为 RECORD ,您可以在PL / SQL程序中使用它。

例如,

SQL> CREATE OR REPLACE
  2    FUNCTION func(
  3        i_dept IN NUMBER,
  4        i_emp_rec  IN emp%rowtype )
  5      RETURN VARCHAR2
  6    IS
  7    BEGIN
  8      RETURN 'DEPTNO = ' || i_dept || ' ENAME = ' || i_emp_rec.ename;
  9    END;
 10  /

Function created.

SQL>
SQL> SET serveroutput ON
SQL> DECLARE
  2    v_deptno dept.deptno%type;
  3    cur sys_refcursor;
  4    v_ecur sys_refcursor;
  5    v_emp emp%rowtype;
  6  BEGIN
  7    OPEN cur FOR SELECT d.deptno,
  8    CURSOR
  9      ( SELECT e.* FROM emp e WHERE e.deptno = d.deptno
 10      ) e FROM dept d;
 11    LOOP
 12      FETCH cur INTO v_deptno, v_ecur;
 13      EXIT
 14    WHEN cur%notfound;
 15      LOOP
 16        FETCH v_ecur INTO v_emp;
 17        EXIT
 18      WHEN v_ecur%notfound;
 19        dbms_output.put_line(func(v_deptno,v_emp));
 20      END LOOP;
 21      CLOSE v_ecur;
 22    END LOOP;
 23    CLOSE cur;
 24  END;
 25  /
DEPTNO = 10 ENAME = CLARK
DEPTNO = 10 ENAME = KING
DEPTNO = 10 ENAME = MILLER
DEPTNO = 20 ENAME = SMITH
DEPTNO = 20 ENAME = JONES
DEPTNO = 20 ENAME = SCOTT
DEPTNO = 20 ENAME = ADAMS
DEPTNO = 20 ENAME = FORD
DEPTNO = 30 ENAME = ALLEN
DEPTNO = 30 ENAME = WARD
DEPTNO = 30 ENAME = MARTIN
DEPTNO = 30 ENAME = BLAKE
DEPTNO = 30 ENAME = TURNER
DEPTNO = 30 ENAME = JAMES

PL/SQL procedure successfully completed.

SQL>