将值返回到游标plsql

时间:2016-06-10 07:36:47

标签: plsql cursor

嗨我有一个程序,在输出中使用游标返回记录集我正在尝试做的是我使用游标来获取数据,同样必须将它返回到输出光标。我可以这样做

PROCEDURE test(value_one IN someTabel.somecolumn%TYPE,                             
           valu_two   IN someTabel.somecolumn%TYPE,
           Outputcursor OUT SYS_REFCURSOR) IS
mydeclaration goes here 
output_value_one sometable.somecolumn%Type;
---
cursor test_select is 
select statement
begin 
for val in test_select loop 
fetch test_select into output_value_one; -- I want my cursor outputcursor to     be return instead of output_value_one 
end loop;
end;

1 个答案:

答案 0 :(得分:1)

你发送deptno游标返回你可以从前端处理的查询。

 create or replace Procedure Transproc(p_deptno    IN  emp.deptno%TYPE,
                        Outputcursor Out Sys_Refcursor,
                        p_recordset Out Varchar) 
            Is
            Vquery varchar2(200);
            Begin
            Vquery:='select * from emp where deptno='|| p_deptno ||'';

            Open Outputcursor For Vquery;
            OMessage:='Success';
            Exception 
            When others then 
            OMessage:='Fail';
            End;

或根据您的要求尝试低于一个。

CREATE OR REPLACE PROCEDURE get_emp_rs (p_deptno    IN  emp.deptno%TYPE,
                      p_recordset OUT SYS_REFCURSOR) AS 
BEGIN 
  OPEN p_recordset FOR
    SELECT ename,
           empno,
           deptno
    FROM   emp
    WHERE  deptno = p_deptno
    ORDER BY ename;
END GetEmpRS;
/

测试: -

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_cursor  SYS_REFCURSOR;
  l_ename   emp.ename%TYPE;
  l_empno   emp.empno%TYPE;
  l_deptno  emp.deptno%TYPE;
BEGIN
  get_emp_rs (p_deptno    => 30,
              p_recordset => l_cursor);

  LOOP 
    FETCH l_cursor
    INTO  l_ename, l_empno, l_deptno;
    EXIT WHEN l_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(l_ename || ' | ' || l_empno || ' | ' || l_deptno);
  END LOOP;
  CLOSE l_cursor;
End;
/