PL/SQL - ¿Cómo devolver variables como cursor?

I have some variables acquired from a stored function. I want to return them as a cursor in a stored procedure. (The reason is because I want to use them in a Jasper report and Jasper only allows stored procedures with cursor output)

My PL/SQL code is:

procedure myprocedure (my_cursor OUT sys_refcursor) is
    output1 TABLE1.FIELD1%TYPE;
    output2 TABLE1.FIELD2%TYPE;
    output3 TABLE2.FIELD1%TYPE;
    output4 TABLE1.FIELD3%TYPE;
    begin
        IF NOT(package1.function1 (output1, output2, output3, output3))
        THEN                
            output1 := NULL;
            output2 := NULL;
            output3 := NULL;
            output4 := NULL;
        END IF;
    open my_cursor for
    select :output1 as myoutput1, :output2 as myoutput2, :output3 as myoutput3, :output4 as myoutput4
    from DUAL
end myprocedure;

yo obtengo PLS-00049: bad bind variable 'output1'

How can I return the results of the function in a cursor?

preguntado el 09 de septiembre de 13 a las 10:09

There is no need to use colon(:) for the vriables. select output1 as myoutput1, output2 ... -

2 Respuestas

Leaving the colon(:) out as @Ramblin' Man commented solved the problem for me.

select output1 as myoutput1, output2 as myoutput2, output3 as myoutput3, output4 as myoutput4 from DUAL

The colon in SQL is called Bind Variable.

As you can see in this article, PL/SQL itself takes care of most of the issues to do with bind variables

Also at the end of the PL/SQL example: Now you might be thinking that you've got to replace the p_empno with a bind variable. However, the good news is that every reference to a PL/SQL variable is in fact a bind variable.

Gracias por tu ayuda.

Respondido 01 Oct 13, 10:10

CREATE TABLE test_tbl
(
    col1 VARCHAR(5)
,   col2 VARCHAR(5)
,   col3 VARCHAR(5)
);


DECLARE
    l_output1 test_tbl.col1%TYPE;
    l_output2 test_tbl.col2%TYPE;
    l_output3 test_tbl.col3%TYPE;

    l_select VARCHAR2(32767);
    l_cur    SYS_REFCURSOR;
BEGIN
    l_output1 := 'col1';
    l_output2 := 'col2';
  --l_output3

    l_select := 'SELECT '
                    || CASE WHEN l_output1 IS NOT NULL THEN '''' || l_output1 || ''' AS my_' || l_output1 ELSE 'NULL' END || CASE WHEN l_output2 IS NOT NULL THEN  ', ' ELSE NULL END
                    || CASE WHEN l_output2 IS NOT NULL THEN '''' || l_output2 || ''' AS my_' || l_output2 ELSE NULL   END || CASE WHEN l_output3 IS NOT NULL THEN  ', ' ELSE NULL END
                    || CASE WHEN l_output3 IS NOT NULL THEN '''' || l_output3 || ''' AS my_' || l_output3 ELSE NULL   END ||
                ' FROM DUAL';

    DBMS_OUTPUT.PUT_LINE(l_select);

    OPEN l_cur FOR l_select;

    IF l_cur%ISOPEN THEN
        DBMS_OUTPUT.PUT_LINE('l_cur IS OPEN');

        CLOSE l_cur;
    END IF;
END;
/*
Result:

SELECT 'col1' AS my_col1, 'col2' AS my_col2 FROM DUAL
l_cur IS OPEN
*/

Respondido el 10 de Septiembre de 13 a las 17:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.