REF CURSOR as a return of a function in SQL developer

Hi team,

I have a function which returns a Ref from a function slider while getting an entry number. But once I have create an implementation of single test on it with a valid entry that he throws error like below and have failed. My SQL developer version is 4.0.2.15.21 and it turns on a Linux OS (GNU/Linux 3.10.0 - 123.6.3.el7.x86_64). Here is the error I get.

Cannot be converted to NUMBER <oracle.jdbc.driver.OracleResultSetImpl@5925237>.

oracle.dbtools.raptor.datatypes.oracle.sql.NUMBER.customUnscaledInternalValue(NUMBER.java:93)

oracle.dbtools.raptor.datatypes.oracle.sql.NumericDatum.customInternalValue(NumericDatum.java:37)

oracle.dbtools.raptor.datatypes.impl.DataTypeImpl.customInternalValueFilter(DataTypeImpl.java:411)

oracle.dbtools.raptor.datatypes.impl.DataTypeImpl.getInternalValue(DataTypeImpl.java:399)

oracle.dbtools.raptor.datatypes.impl.DataValueImpl. < init > (DataValueImpl.java:55)

oracle.dbtools.raptor.datatypes.impl.DataTypeImpl.customDataValue(DataTypeImpl.java:196)

oracle.dbtools.raptor.datatypes.impl.DataTypeImpl.getDataValue(DataTypeImpl.java:178)

Oracle.DBTools.unit_test. Runner.UtRunnerImplIterator$ UtRunnerImplObject.getDynamicValueByName (UtRunnerImplIterator.Java:324)

Oracle.DBTools.unit_test. Runner.UtRunnerImplIterator$ UtRunnerImplObject.mapDynamicValueByName (UtRunnerImplIterator.Java:301)

Oracle.DBTools.unit_test. Runner.UtRunnerImplIterator$ UT...

Please help me out here.

Please do not post double fillet.

Mark this thread ANSWER and continue to use your other thread.

Function taking as input ref cursor but throwing error exit

Tags: Database

Similar Questions

  • Export function in SQL Developer

    Hi guys,.

    I wonder what SQL Developer Library now uses for the export to excel function. What's the jxl.jar, please?

    Thank you.

    I'm sure that Apache POI is used - check sqldeveloper/lib in your installation folder, there are three libraries named poi-* .jar.

  • Cursor of return of (nested) function - PLS-00201: identifier of the CURSOR must be declared

    Hello

    I have a function in which I use nested functions (because I don't want to create objects explicit / external, it is a requirement that I don't have any effect on / I don't want to change).

    In my external function, I use cursor by declaring them in IS-section and then open using the loop for instruction.

    In my inner function / nested, I also want to use a cursor.

    There I also said one like I did in an external function within the section IS of the nested function.

    SQL Developer complains about syntax error (';: wait ").

    When you try to compile the whole process I get the following error:

    "Error (97,14): PLS-00201: identifier 'CURSOR' must be declared '.

    It's my external function starts as:

    CREATE OR REPLACE FUNCTION FN_GENERATE_QUERY_PMT (instance_id_in IN NUMBER, language_in IN VARCHAR2)
    RETURN CLOB
    IS
         CURSOR c_outer IS
          SELECT * FROM BLA;
    

    When it comes to my internal function with the section IS of the external function and stands (inner functions should be at the end of the Section IS to stand behind the statements of variable and cursor, otherwise there will be an error):

    FUNCTION nfn_get_value (num_in IN NUMBER)
          RETURN SYS_REFCURSOR
        IS
          SYS_REFCURSOR c_inner IS
          SELECT *
          FROM BLUB;
        BEGIN
          RETURN c_where_in;
        END;
    

    After that there will be another nested function, then the BEGIN section will appear.

    When looking for 'function returns cursor' I can only find solutions where the cursor is declared in section IS (sometimes its an AS-section o_O * worried *) but wihtoug select, just declare type.

    The cursor focus is added in the Begin block to the way open for instruction. But I don't want to open it yet. I want to open the cursor using the loop statement. I am able to reopen? used can it be two sliders while one will not be closed properly?

    How to fix to return a cursor that I can use in my external function.

    Advice would be appreciated.

    Thanks in advance.

    Maybe I got the soultion
    First of all, I found this thread:

    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:5241391331097

    said the following:

    2) a ref cursor is just a cursor.  a ref cursor is just a cursor.  a ref cursor is just a cursor.... (keep saying it over and over
    

    But fortuneately I also found this post:
    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:14188501024541

    where thankfully someone shared her knowledge with others:

    In view of this block of code - you may see more "prominent" unlike - any

    How many times you run this block - cursor C will always be select * twice.  The ref

    cursor is not guaranteed.

    Another difference is that a ref cursor can be returned to a client.  a plsql 'cursor cursor.

    cannot be returned to a client.

    Another difference is a cursor can be global - a ref cursor cannot (you cannot set

    them outside a procedure / function)

    Another difference is a ref cursor can be passed to the subroutine - a subroutine

    cursor cannot be.

    This explains why the samples seem always fair with open (Ref) cursor within the section to START.

    I expeceted this (as I wrote in the first post) and ask if the course of opening will be sent back twice or just redirect. I suppose it it reassigns who were just a little in performance.

    But I can't yet find this good explanation of this type anywhere within the documentation and of course I didn't read it all. Shame on me, but I'm always engaged by internet download.

    I would be grateful if someone could say my last statement and leave a comment on double-distribution of the refcursors has reopened.

    Thanks in advance.

  • How to pass a REF CURSOR from a function

    I tried to compile the following function:

    CREATE or REPLACE FUNCTION example
    RETURN REFCURSOR
    IS
    heart REFCURSOR;
    BEGIN
    OPEN FOR heart
    "SELECT x FROM table";
    RETURN cur;.
    END;

    but I get:

    PLS-00201: identifier 'REFCURSOR' must be declared.

    Can you help me in the right syntax? I never return a REF CURSOR to a function.
    Is my Version of Oracle 8.1.7

    Thank you!

    You cannot use SYS_REFCURSOR in Oracle 8i.
    The work is approximately as follows

    create or replace package my_pk as
    type my_cur is REF CURSOR;
    end my_pk;
    
    -- Now write your function
    create or replace function my_func return my_pk.my_cur
    as
    l_cur my_pk.my_cur;
    begin
    open l_cur for select * from table_name;
    return l_cur;
    end my_func;
    

    Thank you
    Andy

  • Return of the ref cursor of procedure where the cursor is defined in the specification

    Hello

    Here's pseudo-code

    create or replace package test
    as
    cursor c_emp (number cv_emp_id)
    is
    Select emp_name emp where emp_id = cv_emp_id;

    type ref_c is ref cursor;

    function get_emp_name (number p_emp_id) return ref_c;
    end test;
    /


    create or replace the test physics package
    as
    ref_c function get_emp_name (number p_emp_id)
    is
    ret_val ref_c;
    Start
    Open c_emp (p_emp_id);
    ret_val: = c_emp;
    return ret_val;

    end get_emp_name;
    end test;
    /

    Returns me "PLS-00382: expression is of the wrong type. I tried to use a strongly typed Ref cursor based on the cursor, but nothing helped.

    Clearly, I understand something... and if I can't assign as ' ret_val: = c_emp "and if there is another key way it so please let me know.

    Thank you

    Ralph

    Published by: user13024707 on May 5, 2010 04:43

    user13024707 wrote:
    Clearly the point of my question is being missed.

    Or maybe the question is not been clearly asked. ;)

    I want to set the cursor in the PACKAGE SPECIFICATION, and not in the package body. The issue of the bind variable and the string is neither here nor there because I pass parameters to the cursor.

    What you ask is to use a PL/SQL cursor (defined in the specifications of the package or elsewhere) and that convert a Ref Cursor. N ° cannot be done.
    PL/SQL cursors should be used in the PL/SQL code. REF CURSOR is primary used to pass back a reference to a cursor (and all queries are cursors) to a layer of application outside of PL/SQL for example .net, java, etc.. Once you start using the ref Cursor only really, you have the choice of opening it based on a (dynamic) or a fixed query string as in the examples given. You cannot open a cursor of a PL/SQL cursor definition Ref.

    The difference is that when you do:

      CURSOR cur_mycursor IS
        SELECT ...
    

    you declare a cursor in pl/sql definition, not actually a slider itself. The cursor itself is created when you issue an OPENING or a statement FOR etc. So a Ref Cursor (reference to a slider) cannot refer to the definition of the cursor because it is only a definition, not a cursor (i.e. it has not been sent engine sql and instantiated at that time here). Instead the Ref Cursor must reference a cursor (query), itself, and that may be presented as a string or as the query itself that is issued against the sql engine and then the ref cursor points

    Thus, slider ref and PL/SQL cursors are different concepts, both designed to manage cursors in a slightly different way of programming. You cannot mix them.

  • Is it possible to combine dynamic &amp; static queries in the ref cursor?

    Hi all

    I was wondering if it is possible to combine dynamic & static queries in the ref-cursor?
    CREATE OR REPLACE FUNCTION dynamic_static_kk
       RETURN sys_refcursor
    AS
       o_cursor   sys_refcursor;
    BEGIN
      open 'select 1 fom dual'
           union
           select 2 from dual;
    
       RETURN o_cursor;
    END;
    In the existing code, the two parts of the query are static, but I need to improve & replacement of part would do the trick.

    So, I was wondering if it is possible to keep the static part 2. Of course, do both dynamic parts seems the only possible treatment for me.

    So, I was wondering if it is possible to keep the static part 2.

    No, you can't.

    Of course, do both dynamic parts seems the only possible treatment for me.

    Yes, it's the only way.

  • Dynamic SQL with a Ref Cursor

    Hello

    I have a package that returns a Ref Cursor, in this procedure, I have a dynamic sql code that is built according to certain values, and the query is a select query, is it possible that I can put that dynamic sql in the ref cursor and return of the procedure.

    Or y at - it no alternative better workaround.

    Thanks in advance.

    Naveen

    Yes you can.

    Try this...

    create or replace package test_pack is
    type ref_cur is ref cursor;
    procedure just_print(ref_var ref_cur);
    end;
    /
    
    create or replace package body test_pack is
    procedure just_print(ref_var ref_cur) is
    l_var emp%rowtype;
    begin
    loop
    fetch ref_var into l_var;
    exit when ref_var%notfound;
    dbms_output.put_line(l_var.ename);
    end loop;
    end;
    end;
    /
    
    declare
    cur_var test_pack.ref_cur;
    dsql varchar2(100);
    begin
    dsql := 'select * from emp where deptno=10';
    open cur_var for dsql;
    test_pack.just_print(cur_var);
    end;
    /
    CLARK
    KING
    MILLER
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:00.00
    

    Kind regards
    Prazy

  • sys_refcursor and Ref Cursor

    Hello

    I found this diffrence between ref and sys_refcursor slider.

    If you specify return_type, then the variables of type and slider REF CURSOR of this type are strong; If not,
    they are weak.
    The variables SYS_REFCURSOR and slider of this type are weak.

    my situation is that I must write a procedure stored so many reports entery pages. little enter pages use typed data groups and can't.

    so please tel me what is the best in terms of performance and maintainability above three conditions?

    Yours sincerely

    944768 wrote:

    my situation is that I must write a procedure stored so many reports entery pages. little enter pages use typed data groups and can't.

    Called stored procedures what and where?

    If, with a layer of app such as Java or .net, then they do not distinguish between weak and strong ref Cursor - because it is a concept of PL/SQL.

    And by using a ref cursor type in PL/SQL is intended to provide a mechanism to pass the handle of real cursor SQL cursor created in this session, the client (for example Java or .net). Nothing more. So, why even care that PL/SQL? a weak or strong Ref to the application layer slider

    so please tel me what is the best in terms of performance and maintainability above three conditions?

    I find always frustrating that there is this concept that some methods to create cursors, results in faster and better than other methods, the sliders. It is a concept of bs. All SQLs are analyzed as a SQL cursors. Each. Unique. One. By the SAME CBO.

    If the client code (Java, PL/SQL or other) you write for this SQL cursor DOES NOT change the source SQL.

    This therefore does not cause the CBO now compile a different SQL execution plan for her. The CBO does not care if your code uses a strong Ref Cursor or a weak Ref Cursor, or a slider DBMS_SQL, an explicit cursor, or whatever. Those are concepts of customer. And do not change the execution plan of cursor SQL on the server. Or its planned performance/cost.

    Yes, your client code (Java, PL/SQL, etc.) can use the SQL cursor on the server-side evil - and this can affect performance.

    But waiting for a weak Ref cursor will somehow cause a faster cursor or slower for the same SQL statement, that a strong Ref cursor is not understanding what SQL cursor is, and how the customer interacts with its data types, the classes of customers and its customer interfaces with this SQL cursor.

  • REP-0737: must be a function of return type 'ref cursor.

    Hi all

    I have create a ref cursor query in reports 10 g. But it is giving error REP-0737: must be a function of return type 'ref cursor.

    Here is my code

    function QR_1RefCurDS return sys_refcursor is
    
     My_Cur Sys_Refcursor;
    begin
      Open My_Cur for select * from scott.emp order by deptno;
      return My_Cur
    end;
    

    fate of the screen.

    Ref_Cursor_in_reports10g.jpg

    Oracle Forms/Reports has a complete PL/SQL engine and (only) the SQL parser.

    However, the engine of forms/States PL / SQL and SQL Analyzer are at a level that was in the Oracle 8.0 database.

    So, in the forms/States functions/procedures and forms/States triggers, you can not use SQL commands that did not exist in the 8.0 database.

    The predefined SYS_REFCURSOR type is introduced in Oracle 9i.

    Use this:

    PACKAGE test_rc IS

    TYPE of rc_type IS REF CURSOR RETURN emp % ROWTYPE;

    END;

    FUNCTION RETURN QR_1RefCurDS Test_rc.rc_type IS

    test_rc.rc_type RC;

    BEGIN

    OPEN the RC to SELECT * FROM emp;

    RETURN rc;

    END;

    Kind regards

    Zlatko

  • Made Oracle 6i LOV with ref cursor returned by a function of database

    Hi all

    I want to dynamically create a LOV in oracle forms 6i using the value of ref cursor returned by the function of database.

    is this possible?

    Using loop, I could able to display the values returned by the ref cursor, but how can I assign these values in the LOV?

    You will need to loop through your REF Cursor and assign each value to a group of registration of forms and then assign the Group Record to your LOV.  Take a look at the built-ins CREATE_GROUP, ADD_GROUP_COLUMN and ADD_GROUP_ROW in the help system of forms for more information about how to use these built-ins and examples of how to use them.

    Craig...

  • Irregular data loss - function from PL/SQL returning data using Ref Cursor

    Database Version: 10.2.0.4.0 (node 2 CARS)

    The high-level process flow is as below:
    (1) insert records in a few tables & commit the same
    (2) call the pl/sql function to extract files (on certain conditions with joins with other tables) of the tables which are filled in step 1.
    -> It uses the ORDER BY clause to queries inline & line number 5000 records return for each call.
    Sense - if inline query is supposed to return 1,00,000 records then 20 calls to the same function. This, because the application cannot contain records beyond number.
    (3) the data returned by the ref cursor is then processed by application (Tibco BW) to generate the flat file.

    We are facing the problem of data loss in the file and there is no fixed model. It happens once between 200-300 calls process.
    Resolution: When the problem occurs, triggering the process and in almost every time re-outbreak of the process provides required data.

    Guidance on what could be the reason?

    * Examples of Code for the function:
    CREATE OR REPLACE FUNCTION FUNC_GET_HRCH_TOTAL_DATA)
    outinstrid in NUMBERS
    outinstrkey in NUMBERS
    rownumberstart in NUMBERS
    rownumbereend in NUMBERS
    err_code OUT VARCHAR2,
    err_msg OUT VARCHAR2)
    RETURN PACK_TYPES. HRCH_TOTAL_CURSOR
    IS
    REF_HRCH_TOTAL_CURSOR PACK_TYPES. HRCH_TOTAL_CURSOR;
    BEGIN

    OPEN FOR REF_HRCH_TOTAL_CURSOR
    SELECT *.
    FROM (SELECT A.HIERARCHY_KEY, B.KEY, B.VAL_KEY, A.KEY_NEW, C.ITEMID, B.VAL_TAG, B.sort_order, ROWNUM ROWNUMBER
    OF AOD_HRCH_ITEM A, AOD_HRCH_ATTR B, AOD_HRCH_ITEMS C
    WHERE A.outputid = B.outputid
    AND A.outputid = C.outputid AND A.outputkey = B.outputkey
    AND A.outputkey = C.outputkey AND A.outputid = outinstrid
    AND A.outputkey = outinstrkey AND A.ITEM_SEQ = B.ITEM_SEQ
    AND A.ITEM_SEQ = C.ITEM_SEQ AND A.HIERARCHY_LEVEL_ORDER = B.SORT_ORDER
    ORDER BY A.HIERARCHY_LEVEL_ORDER DESC)
    WHERE ROWNUMBER < rownumbereend
    AND ROWNUMBER > = rownumberstart;


    RETURN REF_HRCH_TOTAL_CURSOR;
    EXCEPTION
    WHILE OTHERS
    THEN
    err_code: = x_progress | ' - ' || SQLCODE;
    err_msg: = SUBSTR (SQLERRM, 1, 500);

    END FUNC_GET_HRCH_TOTAL_DATA;
    /

    Published by: meet_sanc on February 16, 2013 10:42

    Your SELECT statement is almost certainly incorrect

    SELECT *
      FROM ( SELECT A.HIERARCHY_KEY, B.KEY, B.VAL_KEY, A.KEY_NEW, C.ITEMID, B.VAL_TAG, B.sort_order,ROWNUM ROWNUMBER
               FROM AOD_HRCH_ITEM A, AOD_HRCH_ATTR B, AOD_HRCH_ITEMS C
              WHERE A.outputid = B.outputid
                AND A.outputid = C.outputid AND A.outputkey = B.outputkey
                AND A.outputkey = C.outputkey AND A.outputid = outinstrid
                AND A.outputkey = outinstrkey AND A.ITEM_SEQ = B.ITEM_SEQ
                AND A.ITEM_SEQ = C.ITEM_SEQ AND A.HIERARCHY_LEVEL_ORDER = B.SORT_ORDER
              ORDER BY A.HIERARCHY_LEVEL_ORDER DESC)
     WHERE ROWNUMBER < rownumbereend
       AND ROWNUMBER >= rownumberstart;
    

    Since the ORDER BY is applied after the ROWNUM is assigned in this case, your query is requested for a period of 5000 lines any arbitrariness. It would be perfectly valid for a single line to return in each of your 200 different calls or for a line to return in any of them.

    You definitely want to do something in the sense of the canonical askTom wire

    select *
      from ( select a.*, rownum rnum
               from ( YOUR_QUERY_GOES_HERE -- including the order by ) a
              where rownum <= MAX_ROWS )
     where rnum >= MIN_ROWS
    

    That said, it seems inconceivable that Tibco is unable to manage a cursor that returns more than a certain number of lines. You do a ton of work to return the data pages that are certainly not necessary. Unless you're saying that you somehow paralyzed your installation of Tibco giving him a ridiculously small amount of memory to process, something doesn't look good. A slider is just a pointer - it holds that no data - so the number of lines that you can extract a slider should have no impact on the amount of memory on the client application needs.

    As others have already pointed out, your exception handler is almost certainly do more harm than good. Return the error codes and error messages as from the OUT parameters, instead of simply allowing the exception to propagate deletes a ton of useful information (such as the mistake of the stack) and makes your process much less robust.

    Justin

  • Pipelined table vs ref cursor in a function return

    Hi gurus,

    Everybody has discovered that a (subject) is faster on the other? Data will be primarily consumed from an external application (.net). What are the benefits? I can't decide if that use is

    Thank you very much.

    user12868294 wrote:
    Hi gurus,

    Everybody has discovered that a (subject) is faster on the other? Data will be primarily consumed from an external application (.net). What are the benefits? I can't decide if that use is

    Thank you very much.

    They are two different things.

    A pipeline table acts as an array, but you must always choose in it and so if your consumption that in .net, you would still use a Ref Cursor I guess to query this table in pipeline (I guess .net is not query the tables directly, but must use some sort of slider Ref?)

    Tables in pipeline can be fast, but it depends on what you need to. Is there a reason why you really need a feature in pipeline? If this is not the case, just use a normal query with a Ref Cursor, so your .net application only retrieves the data properly.

  • PL/SQL - Call Out Ref Cursor returned by a stored procedure

    Hello

    I am creating a procedure where a Ref Cursor is defined as an OUT parameter, my question is how to shout that the Ref Cursor when I run for example here is how I want to call my EXEC sql command:

    EXEC film_not_in_stock (2,2,vcur);

    Here is the procedure:

    CREATE OR REPLACE PROCEDURE film_not_in_stock (p_film_id in NUMBER, p_store_id number, vcur ON SYS_REFCURSOR)

    IS

    News sys_refcursor;

    v_cur inventory.inventory_id%TYPE--ou is the same type of column inventory_id

    v_cur sys_refcursor;

    BEGIN

    Heart OPEN to SELECT inventory_id

    INVENTORY

    WHERE film_id = p_film_id

    AND store_id = p_store_id

    AND inventory_id NOT IN (SELECT inventory_in_stock (inventory_id) FROM dual);

    News of FETCH in v_cur;

    OUTPUT WHEN heart % NOTFOUND;

    END;

    /

    I know there are typos in the procedure, you will appreciate if you can help me to put it right.

    Thank you very much!

    Tonya.

    In fact, based on your previous post, function inventory_in_stock returns 0 or 1. If so, there are:

    CREATE OR REPLACE

    PROCEDURE film_not_in_stock)

    p_film_id in NUMBERS

    p_store_id in NUMBERS

    p_cur ON SYS_REFCURSOR

    )

    IS

    News sys_refcursor;

    v_cur inventory.inventory_id%TYPE--ou is the same type of column inventory_id

    v_cur sys_refcursor;

    BEGIN

    OPEN p_cur

    FOR

    SELECT inventory_id

    INVENTORY

    WHERE film_id = p_film_id

    AND store_id = p_store_id

    AND inventory_in_stock (inventory_id) = 0;

    News of FETCH in v_cur;

    OUTPUT WHEN heart % NOTFOUND;

    END;

    /

    SY.

  • Converts the ref cursor effect of function table

    I have a function named fn_get_emp(), whose return type is sys_refcursor. When I select fn_get_emp of double; I get the output to the format of the cursor. I want to convert to the table like format so that I can use in my insert statements insert into foo select * from fn_get_emp();

    Please note that the columns in the output of the fn_get_emp() is not fixed.

    Published by: user10566312 on January 30, 2012 22:25

    Here are the steps in the package DBMS_SQL, you need to turn your Ref Cursor a cursor DBMS_SQL.

    http://docs.Oracle.com/CD/E11882_01/AppDev.112/e25788/d_sql.htm#CHDJDGDG

    and then the rest of the documentation of the DBMS_SQL package gives many examples of how to treat this a cursor dbms_sql.

    Here is an example to use the package dbms_sql to process a request and produce some projection of sql and the database is in the query to CSV file...

    As user sys:

    CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
    /
    GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
    /
    

    As myuser:

    CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
                                         ,p_dir IN VARCHAR2
                                         ,p_header_file IN VARCHAR2
                                         ,p_data_file IN VARCHAR2 := NULL) IS
      v_finaltxt  VARCHAR2(4000);
      v_v_val     VARCHAR2(4000);
      v_n_val     NUMBER;
      v_d_val     DATE;
      v_ret       NUMBER;
      c           NUMBER;
      d           NUMBER;
      col_cnt     INTEGER;
      f           BOOLEAN;
      rec_tab     DBMS_SQL.DESC_TAB;
      col_num     NUMBER;
      v_fh        UTL_FILE.FILE_TYPE;
      v_samefile  BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
    BEGIN
      c := DBMS_SQL.OPEN_CURSOR;
      DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
      d := DBMS_SQL.EXECUTE(c);
      DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
      FOR j in 1..col_cnt
      LOOP
        CASE rec_tab(j).col_type
          WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
          WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
          WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
        ELSE
          DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
        END CASE;
      END LOOP;
      -- This part outputs the HEADER
      v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
      FOR j in 1..col_cnt
      LOOP
        v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
      END LOOP;
      --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
      UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
      IF NOT v_samefile THEN
        UTL_FILE.FCLOSE(v_fh);
      END IF;
      --
      -- This part outputs the DATA
      IF NOT v_samefile THEN
        v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
      END IF;
      LOOP
        v_ret := DBMS_SQL.FETCH_ROWS(c);
        EXIT WHEN v_ret = 0;
        v_finaltxt := NULL;
        FOR j in 1..col_cnt
        LOOP
          CASE rec_tab(j).col_type
            WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
                        v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
            WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
                        v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
            WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
                        v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
          ELSE
            v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
          END CASE;
        END LOOP;
      --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
        UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
      END LOOP;
      UTL_FILE.FCLOSE(v_fh);
      DBMS_SQL.CLOSE_CURSOR(c);
    END;
    

    This allows the header line and the data to write into files separate if necessary.

    for example

    SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
    
    PL/SQL procedure successfully completed.
    

    Output.txt file contains:

    empno,ename,job,mgr,hiredate,sal,comm,deptno
    7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
    7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
    7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
    7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
    7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
    7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
    7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
    7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
    7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
    7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
    7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
    7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
    7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
    7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10
    

    The procedure allows for the header and the data to separate files if necessary. Just by specifying the file name "header" will put the header and the data in a single file.

    Adapt to the exit of styles and different types of data are needed.

  • Apply the function of a column of a Ref Cursor

    Hello

    I have a stored procedure that returns a refcursor. Is it possible to apply a function as for example SUBSTR LPAD and a custom on a column in the refcursor function returned? The problem is that I can't change the existing stored procedure and I was wondering if it would be possible to write somewhat a wrapper procedure (or an anonymous PL/SQL block) that would actually apply a scalar function and only then return the result to the client?

    Thanks and regards,
    Swear

    do you want after retrieve you records of Ref Cursor

    SQL> create or replace
      2  procedure rc_test (p_rc out sys_refcursor)
      3  is
      4    l_rc sys_refcursor;
      5  begin
      6     open l_rc for
      7     select dummy from dual;
      8     p_rc := l_rc;
      9  end;
     10  /
    
    Procedure created.
    
    SQL> declare
      2     r sys_refcursor;
      3     v varchar2(1);
      4  begin
      5     rc_test (r);
      6     fetch r into v;
      7     dbms_output.put_line (lower (v));
      8     close r;
      9  end;
     10  /
    x
    
    PL/SQL procedure successfully completed.
    

Maybe you are looking for