Return the SQL Statement of an Explicit Cursor

这一生的挚爱 提交于 2019-12-06 08:02:18

Yes, you can do that with DBMS_SQL.TO_CURSOR_NUMBER function. You Procedure will look like this:

PROCEDURE run_query(p_cur IN OUT SYS_REFCURSOR) IS
    ...
BEGIN

    c := DBMS_SQL.TO_CURSOR_NUMBER(p_cur);

    -- get a description of the returned columns
    DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
    ...

Then you have to call it like this:

declare

    l_cur SYS_REFCURSOR;

BEGIN

    OPEN l_cur FOR
       select  *
       from    table_a
       where   employee_number     = nvl(p_emp_no, employee_number)
       and     payroll_id          = nvl(p_payroll_id, payroll_id);
       and     business_group_id   = p_bg_id
       ...;

    tabletoexcel.run_query(l_cur);

OPEN FOR Statement allows CLOB as statement, so there is no practical limit in terms of size.

Since you don't know at design time which columns will be selected (at least I assume so) there is no way to get rid of DBMS_SQL.DESCRIBE_COLUMNS and DBMS_SQL.DEFINE_COLUMN. Otherwise you can use FETCH Statement instead of DBMS_SQL.FETCH_ROWS(c)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!