0

我有一些从存储函数中获取的变量。我想将它们作为存储过程中的游标返回。(原因是因为我想在 Jasper 报告中使用它们,而 Jasper 只允许带有游标输出的存储过程)

我的 PL/SQL 代码是:

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;

我明白了PLS-00049: bad bind variable 'output1'

如何在游标中返回函数的结果?

4

2 回答 2

0

正如@Ramblin' Man 评论的那样,留下冒号(:) 为我解决了这个问题。

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

SQL 中的冒号称为绑定变量

正如您在本文中看到的,PL/SQL 本身负责处理与绑定变量有关的大部分问题

同样在 PL/SQL 示例的末尾: 现在您可能认为您必须将 p_empno 替换为绑定变量。然而,好消息是每个对 PL/SQL 变量的引用实际上都是一个绑定变量

谢谢您的帮助。

于 2013-10-01T09:40:35.917 回答
-1
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
*/
于 2013-09-10T13:13:05.247 回答