简体   繁体   English

在Oracle SQL Developer中使用Ref Cursor

[英]Using Ref Cursor in Oracle SQL Developer

I am using Oracle SQL Developer, but I am having an issue seeing results from a package that returns a ref cursor. 我正在使用Oracle SQL Developer,但是我在查看返回引用游标的包的结果时遇到了问题。 Below is the package definition: 以下是包装定义:

CREATE OR REPLACE package instance.lswkt_chgoff_recov
as
      type rec_type is record
            (
            source_cd                       lswk_tpr.gltrans.tpr_source_cd%TYPE,
            as_of_dt                        lswk_tpr.gltrans.tpr_as_of_dt%TYPE,
            chrg_off_recov                  varchar2(5),
            process_dt                      lswk_tpr.gltrans.dtgltran%TYPE,
            effect_dt                       lswk_tpr.gltrans.dtgltran%TYPE,
            account_nbr                     lswk_tpr.contract.lcontid%TYPE,
            naics_cd                        lswk_tpr.udfdata.sdata%TYPE,
            prod_type                       varchar2(20),
            off_nbr                         lswk_tpr.schedule.sctrcdty%TYPE,
            borrower_nm                     lswk_tpr.customer.scustnm%TYPE,
            tran_type_cd                    lswk_tpr.gltrans.sglcd%TYPE,
            tran_type_desc                  lswk_tpr.gltrans.sglcd%TYPE,
            tran_amt                        lswk_tpr.gltrans.ctranamt%TYPE,
            note_dt                         lswk_tpr.schedule.dtbk%TYPE,
            accru_cd                        number,
            non_accr_cd                     lswk_tpr.schedule.dtlstincsus%TYPE,
            comm_sb_ind                     varchar2(4)
            );

      type cur_type is ref cursor return rec_type;

      procedure sp
            (
            p_as_of_dt              in      date,
            ref_cur                 in out  cur_type
            );
end;
/

I guess the question is this possible and if so, what do I need to do. 我想这个问题是可能的,如果是的话,我需要做什么。 I am using Oracle SQL Developer 1.5.5. 我正在使用Oracle SQL Developer 1.5.5。 Thanks. 谢谢。

Wade

Here is the code I used to call my package (generated by TOAD): 这是我用来调用我的包的代码(由TOAD生成):

DECLARE 
  P_AS_OF_DT DATE;
  REF_CUR instance.LSWKT_CHGOFF_RECOV.CUR_TYPE;
  REF_CUR_row REF_CUR%ROWTYPE;

BEGIN 
  P_AS_OF_DT := '31-AUG-2009';

  instance.LSWKT_CHGOFF_RECOV.SP ( P_AS_OF_DT, REF_CUR );

  DBMS_OUTPUT.Put_Line('REF_CUR =');
  IF REF_CUR%ISOPEN THEN
  DBMS_OUTPUT.Put_Line('  SOURCE_CD  AS_OF_DT  CHRG_OFF_RECOV  PROCESS_DT  EFFECT_DT  ACCOUNT_NBR  NAICS_CD  PROD_TYPE  OFF_NBR  BORROWER_NM  TRAN_TYPE_CD  TRAN_TYPE_DESC  TRAN_AMT  NOTE_DT  ACCRU_CD  NON_ACCR_CD  COMM_SB_IND');
    LOOP
      FETCH REF_CUR INTO REF_CUR_row;
      EXIT WHEN REF_CUR%NOTFOUND;
      DBMS_OUTPUT.Put_Line(
           '  ' || '[TPR_SOURCE_CD%type]'
        || '  ' || '[TPR_AS_OF_DT%type]'
        || '  ' || '''' || REF_CUR_row.CHRG_OFF_RECOV || ''''
        || '  ' || '[DTGLTRAN%type]'
        || '  ' || '[DTGLTRAN%type]'
        || '  ' || '[LCONTID%type]'
        || '  ' || '[SDATA%type]'
        || '  ' || '''' || REF_CUR_row.PROD_TYPE || ''''
        || '  ' || '[SCTRCDTY%type]'
        || '  ' || '[SCUSTNM%type]'
        || '  ' || '[SGLCD%type]'
        || '  ' || '[SGLCD%type]'
        || '  ' || '[CTRANAMT%type]'
        || '  ' || '[DTBK%type]'
        || '  ' || NVL(TO_CHAR(REF_CUR_row.ACCRU_CD), 'NULL')
        || '  ' || '[DTLSTINCSUS%type]'
        || '  ' || '''' || REF_CUR_row.COMM_SB_IND || '''');
    END LOOP;
  ELSE
    DBMS_OUTPUT.Put_line('  (Ref Cursor is closed)');
  END IF;


  COMMIT; 
END;

I get the error: 我收到错误:

ORA-06502: PL/SQL: numeric or value error ORA-06502:PL / SQL:数值或值错误

Hope this clears it up a bit more. 希望这能把它搞清楚一点。

You can easily print the output results of a ref_cursor in SQL Developer to see the return value.. 您可以在SQL Developer中轻松打印ref_cursor的输出结果以查看返回值。

Here's an example of a Refcursor Function: 这是一个Refcursor函数的例子:

create or replace function get_employees() return sys_refcursor as
  ret_cursor sys_refcursor;
begin
  open ret_cursor for
    select * from employees;
  return ret_cursor; 
end get_employees;

Quick and dirty method: 快速而肮脏的方法:

select get_employees() from dual; 

Neat and tidy method: 干净整洁的方法:

variable v_ref_cursor refcursor;
exec :v_ref_cursor := get_employees(); 
print :v_ref_cursor

If you have a procedure which requires a refcursor in the signature of the proc, you can do this: 如果你有一个程序需要在proc的签名中使用refcursor,你可以这样做:

var rc refcursor;
execute <package>.my_proc(:rc);
print rc;

Highlight and hit F5. 突出显示并点击F5。

The only explicit value that I see in the generated program is 我在生成的程序中看到的唯一显式值是

 P_AS_OF_DT := '31-AUG-2009';

Try a an explicit conversion ( to_date ('31-AUG-2009', 'DD-MON-YYYY') instead, maybe that gets rid of the problem. 尝试显式转换( to_date ('31-AUG-2009', 'DD-MON-YYYY') ,也许可以解决问题。

If that doesn't help, can you see if your error is generated in the sp or in yor code? 如果这没有帮助,你能看到你的错误是在sp或yor代码中生成的吗? If you can't figure this out directly, define an sp from the code you have, set a breakpoint and step through the code to see where the error comes from. 如果您无法直接解决这个问题,请从您拥有的代码中定义一个sp,设置一个断点并逐步执行代码以查看错误的来源。

In your comment you sayHere is the error: 在你的评论中,你说这是错误:

ORA-06502: PL/SQL: numeric or value error ORA-06512: at line 16 ORA-06502:PL / SQL:数字或值错误ORA-06512:第16行

Whatever it may appear like sometimes, PL/SQL errors are not randomly generated. 无论有时看起来如何,PL / SQL错误都不是随机生成的。 This error points to a flawed data type conversion which occurs at line 16 of your procedure. 此错误指向在您的过程的第16行发生的有缺陷的数据类型转换。 Without seeing your whole procedure it is not possible for us to pinpoint line 16. Fortunately the Code Editor in SQL Developer will put line numbers in the gutter; 在没有看到整个过程的情况下,我们无法确定第16行。幸运的是,SQL Developer中的代码编辑器会将行号放在阴沟中; if you are not seeing line numbers you will need to toggle a preference. 如果您没有看到行号,则需要切换首选项。

What you need to look for is a string being cast to a number or date variable, or a number being cast to a date field. 您需要查找的是将字符串强制转换为数字或日期变量,或者将数字强制转换为日期字段。 This may be signalled by an explicit TO_NUMBER() or TO_DATE, in which case you need to check the format mask and/or the data content. 这可以通过显式TO_NUMBER()或TO_DATE来发出信号,在这种情况下,您需要检查格式掩码和/或数据内容。 Alternatively you may have an implicit cast. 或者,您可能有隐式演员表。 In that case you may need to make it explicit, with the appropriate format mask. 在这种情况下,您可能需要使用适当的格式掩码使其显式化。 Of course it could be an accidental and unwanted conversion because the projection of the SELECT statement doesn't match the signature of the REF CURSOR record. 当然,这可能是意外和不需要的转换,因为SELECT语句的投影与REF CURSOR记录的签名不匹配。 That is easy to fix. 这很容易解决。

Just make a loop which iterates through the ref cursor returned. 只需创建一个循环,循环返回引用的ref游标。 You can output to the console using DBMS_OUTPUT.PUT_LINE() and choosing specific fields to show. 您可以使用DBMS_OUTPUT.PUT_LINE()输出到控制台并选择要显示的特定字段。

There's no way to tell without seeing what the cursor's query is. 没有看到光标的查询是什么就没有办法告诉你。 Take a look at the SELECT statement you're running in the procedure SP. 看一下您在SP过程中运行的SELECT语句。 One of the column's you are selecting into a numeric field in rec_type is returning character data, which cannot be converted to a number. 您在rec_type中选择数字字段的列之一是返回字符数据,该字符数据无法转换为数字。

Instead of trying to figure out how to output the cursor, take the SELECT statement from sp and run it standalone. 而不是试图弄清楚如何输出游标,从sp获取SELECT语句并独立运行它。 Look down the results you get. 向下看你得到的结果。 You're going to be looking for some non-digit values coming back in one of the fields where you expect a number. 您将在一个您期望数字的字段中查找一些非数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM