我从存储的函数中获取了一些变量。我想将它们作为存储过程中的游标返回。 (原因是因为我想在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'
如何在游标中返回函数的结果?
答案 0 :(得分:0)
离开冒号(:),因为@Ramblin'Man评论为我解决了这个问题。
select output1 as myoutput1, output2 as myoutput2, output3 as myoutput3, output4 as myoutput4 from DUAL
SQL中的冒号称为Bind Variable。
正如您在本文中所看到的, PL / SQL本身负责处理绑定变量的大部分问题
同样在PL / SQL示例的末尾:现在您可能认为必须用绑定变量替换p_empno。然而,好消息是对PL / SQL变量的每个引用实际上都是绑定变量。
感谢您的帮助。
答案 1 :(得分:-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
*/