0

カーソルを返す以下の関数があります。ただし、1行だけ返す必要があります。データを として返すことについてグーグルで読んだことがあります%rowtypeが、それを機能させることができないようです。

カーソルを返したくない理由は、関数を選択する(cursor)と、4 列のデータを含む 1 つの行を表示する代わりに、1 つのセルが返されるためです。

create or replace function udf
(
    i_ptf_code in varchar2
    ,i_begin_date in date
    ,i_end_date in date
)

    return sys_refcursor

is
    portf_code varchar2(50);
    end_date date;
    chain_linked_ptf_net float;
    chain_linked_ptf_gross float;
    chain_linked_bmk float;
    end_mv float;
    v_cursor sys_refcursor;

    cursor c_return_series is
        select      p.portf_code
                    ,gpr.end_date
                    ,gpr.end_market_value
                    ,gpr.portf_perf_gross as gross_ret
                    ,gpr.portf_perf_net as net_ret
                    ,(exp(sum(ln(1 + gpr.portf_perf_gross)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_ptf_gross
                    ,(exp(sum(ln(1 + gpr.portf_perf_net)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_ptf_net
                    ,(exp(sum(ln(1 + gpr.bmk_perf)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_bmk
        from        portfolio_returns gpr
        inner join  portfolio p on p.portf_id = gpr.portf_id and p.is_composite != 2  --2 means composite
        where       p.portf_code = i_ptf_code
        and         gpr.end_date between i_begin_date and i_end_date;

begin

    select i_ptf_code, i_end_date into portf_code, end_date from dual; 

    for c1line in c_return_series loop

        if c1line.end_date = i_end_date then
            select  c1line.chain_linked_ptf_gross
                    ,c1line.chain_linked_ptf_net
                    ,c1line.chain_linked_bmk
                    ,c1line.end_market_value
            into    chain_linked_ptf_gross
                    ,chain_linked_ptf_net
                    ,chain_linked_bmk
                    ,end_mv
            from    dual;
        end if;

    end loop;

    open v_cursor for

        select      portf_code as portf_code
                    ,end_date as end_date
                    ,chain_linked_ptf_gross * 100 as period_ptf_gross
                    ,chain_linked_ptf_net * 100 as period_ptf_net
                    ,chain_linked_bmk * 100 as period_bmk
                    ,end_mv * 100 as period_mv
        from        dual;

    return v_cursor;
    close v_cursor;

end;
/
4

1 に答える 1