Can I pass an explicit cursor to a function/procedure for use in FOR loop?

后端 未结 4 1111
时光说笑
时光说笑 2021-01-12 02:59

I have a procedure that performs some calculations on all records returned by a cursor. It looks a bit like this:

PROCEDURE do_calc(id table.id_column%TYPE)
         


        
4条回答
  •  隐瞒了意图╮
    2021-01-12 03:55

    Create a package.

    Declare your cursor as package variable.

    Use %rowtype to set function parameter type.

    create or replace package test is
      cursor c is select 1 as one, 2 as two from dual;
    
      procedure test1;
      function test2(test_record c%ROWTYPE) return number;
    
    end test;
    
    
    create or replace package body test is
      procedure test1 is    
      begin
        for r in c loop      
          dbms_output.put_line(test2(r));
        end loop;
      end;
    
      function test2(test_record c%ROWTYPE) return number is
        l_summ number;
      begin
        l_summ := test_record.one + test_record.two;
        return l_summ;
      end;
    end test;
    

提交回复
热议问题