Oracle PL / SQL参考光标功能缺少字符

时间:2016-11-19 17:57:59

标签: oracle plsql ref-cursor

当我运行此代码时,结果应为636790,但仅返回63679。在这一个击败大脑!!!为什么缺少数字?源表和列包含正确的数字636790。

create or replace package jt_types 
  as 
    type ttest is ref cursor;
end; 

CREATE OR REPLACE FUNCTION jt_test
      RETURN jt_types.ttest
   IS
       o_return  jt_types.ttest;   
   BEGIN
           OPEN o_return FOR
               SELECT offn_id
               FROM jt_offn_4
              where offn_ID in (636790)
            ORDER BY offn_id;
     RETURN o_return;
  END jt_test;  

DECLARE
     l_ids                  jt_types.ttest;
     l_id                   NUMBER;   
BEGIN
    l_ids := jt_test;   
  LOOP
          FETCH l_ids INTO l_id;
          EXIT WHEN l_ids%NOTFOUND;
          dbms_output.put_line('Rec: ' || l_id);
  END LOOP;
  CLOSE l_ids;
 END;

1 个答案:

答案 0 :(得分:0)

最简单的方法是返回sys_refcursor,这里是代码:

create or replace 
function jt_test
      return sys_refcursor
   is
       o_return  sys_refcursor;   
   begin
           open o_return for
               select offn_id
               from jt_offn_4
              where offn_ID = 636790;
     return o_return;
  end jt_test;

declare
     l_ids                  sys_refcursor;
     l_id                   number;   
begin
  l_ids := jt_test;   
  loop
    fetch l_ids into l_id;
    dbms_output.put_line('Rec: ' || l_id);
    exit when l_ids%NOTFOUND;
  end loop;

  close l_ids;

 end;