Cursor query

Can you help me to is there a best way to write the following cursor select queries.
 
  cursor p_date_diff_cur(p_date date) is 
    SELECT TRUNC(SYSDATE) + 1 - LEVEL AS p_today FROM DUAL CONNECT BY LEVEL <= (TRUNC(SYSDATE) - p_date) order by p_today;

 
   
cursor p_payment_cur(p_date date) is select nvl(sum(nvl(AMOUNT,0)),0) pamount
                            from tufs_web.mv_payment_holding
                           where company_id = i_company_id and program_key =  i_program_key and
                           report_period = i_report_period and trunc(received_date) = trunc(p_date);
                           
    cursor p_early_payment_cur(p_date date) is select nvl(sum(nvl(AMOUNT,0)),0) pamount
                            from tufs_web.mv_payment_holding
                           where company_id = i_company_id and program_key =  i_program_key and
                           report_period = i_report_period and trunc(received_date) <= trunc(p_date);   
1 cursor
p_date is the payment due date.
If payment due date is 01/12/2010

It will take all the dates of 12/01/2010 to sysdate. I don't know that's the best way to do

slider 2nd and 3rd slider
trunc (received_date) < = trunc (p_date); (is - that both sides of the query log is ok)?

Published by: sony June 6, 2011 14:02

Sony says:

Currently, I have created for received_date in the database index. So do I need to change it for the function-based index?

If you do not want to change the query to use the date of that publication of Ganesh arithmetic or use the interval of calculation that I posted and trunc (received_date) is sufficiently selective as an index on this expression would be the most effective way for the optimizer extract the lines in question, then you would benefit from the creation of an index based on a function on trunc (received_date).

If you don't mind change your syntax to use either approach Ganesh or I posted, the query can use the index on received_date.

If there is no other queries using the index on the received_date, then you could drop it and just keep the index based on a function on trunc (received_date)

Justin

Tags: Database

Similar Questions

  • Problem with cursor query (Oracle 8i)

    Hi all

    The following query works perfectly well if I test it in my development environment
    SELECT dt,
          (SELECT 'Y'
             FROM dual
            WHERE EXISTS (SELECT 'x'
                            FROM top_days t
                           WHERE t.date = dt))
    FROM (SELECT to_date(p_date_start, 'dd-mm-yyyy') + LEVEL - 1 dt
                       FROM dual
                     CONNECT BY LEVEL <=
                                to_date(p_date_end, 'dd-mm-yyyy') - to_date(p_date_start, 'dd-mm-yyyy') + 1
                            AND PRIOR dbms_random.value IS NOT NULL) --workaround for oracle 8i
                   ,
                    top_days t
    WHERE dt = t.date(+)
    ORDER BY dt;
    However, when I try to include it in my package (where it is used in a slider) I get a PLS-00103: encountered the symbol "SELECT" when expecting one of the following numbers: (- + mod null other < an ID > etc.)
    CURSOR c_holidays IS 
               SELECT dt,
                    (SELECT 'Y'
                       FROM dual
                      WHERE EXISTS (SELECT 'x'
                               FROM top_days t
                              WHERE t.date = dt))
               FROM (SELECT to_date(p_date_start, 'dd-mm-yyyy') + LEVEL - 1 dt
                       FROM dual
                     CONNECT BY LEVEL <=
                                to_date(p_date_end, 'dd-mm-yyyy') - to_date(p_date_start, 'dd-mm-yyyy') + 1
                            AND PRIOR dbms_random.value IS NOT NULL) --workaround for oracle 8i
                   ,
                    top_days t
              WHERE dt = t.date(+)
              ORDER BY dt;
    Is there something special I don't know for Oracle 8i which prevents me from using this query in a slider? Or am I just missing something blatantly obvious?
    Note: p_date_start and p_date_end are parameters that are filled in the service

    FYI: Oracle 8i release version 8.1.7.4.1 and PL/SQL Release 8.1.7.4.0

    Scalar subqueries in the select list have been introduced in SQL 8i but not in PL/SQL 8i. The only solution to use subqueries in the select list in PL/SQL 8i is dynamic SQL:

    OPEN v_ref_cursor FOR '
    SELECT dt,
          (SELECT 'Y'
             FROM dual
            WHERE EXISTS (SELECT 'x'
                            FROM top_days t
                           WHERE t.date = dt))
    FROM (SELECT to_date(p_date_start, 'dd-mm-yyyy') + LEVEL - 1 dt
                       FROM dual
                     CONNECT BY LEVEL <=
                                to_date(p_date_end, 'dd-mm-yyyy') - to_date(p_date_start, 'dd-mm-yyyy') + 1
                            AND PRIOR dbms_random.value IS NOT NULL) --workaround for oracle 8i
                   ,
                    top_days t
    WHERE dt = t.date(+)
    ORDER BY dt';
    

    SY.

  • By using a nested as a data source in a cursor query table?

    Hi all

    Can you please let me know if we can use a nested table created locally as a source of data in a query of cursor?

    I'm doing using the following logic, but on compliation failure with the error: "PL/SQL: ORA-00942: table or view does not exist.

    =================================================================================================

    DECLARE

    N_table_outage_dates TYPE IS an ARRAY OF DATE;
    nt_out n_table_outage_dates: = n_table_outage_dates();

    l_outage_date DATE;

    cursor l_temp_cursor is
    SELECT DISTINCT APS. VENDOR_ID
    OF AP_SUPPLIERS APS
    WHERE
    (
    Trunc (APS. BUS_CLASS_LAST_CERTIFIED_DATE) + To_Number (FND_PROFILE. Value('POS_BUS_CLASS_RECERT_PERIOD'))
    = Trunc (l_outage_date) + To_Number (FND_PROFILE. Value('POS_BUS_CLASS_RECERT_REMIND_DAYS'))
    AND l_outage_date IN (SELECT * FROM nt_out );

    BEGIN

    l_outage_date: = SYSDATE;

    FOR I IN 1.7 LOOP

    nt_out. EXTEND;
    nt_out (i): = l_outage_date;
    l_outage_date: = l_outage_date + 1;

    END LOOP;

    Open l_temp_cursor

    -other logic

    close l_temp_cursor;

    END

    =================================================================================================

    The other will be passing the value of 'l_outage_date' to a cursor in a loop and then storing the multiple outputs of the query in a separate table / v-table but it will lead to duplicated being filled, elements that will need some work to do.

    Please let me how can know I use the nested table created by user in the query cursor running regarding the above code? Is the statement to go wrong anywhere nested table?

    Thank you
    Sylvain

    You currently have

    TYPE n_table_outage_dates IS TABLE OF DATE;
    nt_out n_table_outage_dates := n_table_outage_dates();
    

    He declares nt_out as a type of PL/SQL... the engine SQL (the place that runs queries) can not understand / interpret what it is, because it is declared in a PL/SQL it is not inherited in any way in SQL.

    So you must either create the type in SQL as I showed earlier OR (now that we know your version) get grants on the object laid down by Solomon.

    AND THEN, you need to change

    (SELECT * FROM nt_out );
    

    TO

    (SELECT * FROM TABLE(CAST(nt_out as DEPENDS_ON_WHICH_OBJECT_YOU_CHOOSE_TO_USE) );
    

    As I mentioned earlier in my example.

    Published by: Tubby on December 23, 2009 11:23

  • Problems using trigger cursor query and post.

    Hello

    I use a block non-base of data/model to query records in a database block. My form has a point text 'report No.' that will be used to enter a valid number of reports. Based on the number of this report, I hit a button to search (also from the same block of data non-base) to view records that have this "report No.' in the database block. I think I need to use a cursor in a post for this request but I am not able to operate :( Help, please! I really need this point that nobody is helping me to work:(la figure)

    My search button - button the trigger:
    DECLARE
    NUMBER OF CHOICES;
    BEGIN
    IF: DUMMY_BLK. NWTFR IS NULL THEN
    ALERTS. STOP_ALERT ("Start Date is required", choice);
    RAISE FORM_TRIGGER_FAILURE;
    END IF;
    go_block ('T_VESSEL_BLK');
    execute_query;
    END;

    My post_query:

    DECLARE
    cursor vessel_cur is
    SELECT VES_RPTNO, VES_SENDER, VES_SENDER_MBOX, VES_INTCTRL_REF, VES_NAME, VES_VOYNO, VES_ARRDT
    SHIP
    WHERE VES_RPTNO =: DUMMY_BLK. NWTFR;
    var_mot vessel_cur % ROWTYPE;
    BEGIN
    Open vessel_cur;
    LOOP
    extract the vessel_cur in var_mot;
    EXIT WHEN vessel_cur % NOTFOUND;
    END LOOP;
    close vessel_cur;
    END;

    instead of execute_query you do now

    Why do you offer me something like that? Replacing the standard behavior that you use forms must be well thought out, because you have to manage a large number of possible problems. You can easily get the desired using the standard logic behavior:

    -Create an item number IT_COUNT in a control block, assign 'calculation mode' to 'Summary', 'Synthesis function' to 'Count' "Down block" and your DB-bloc and "just point' a null-element not contained in this block. Last series the block 'Interrogate all folders' to 'Yes' in your databaseblock. Now, in your code, you can just checkl the value of this element of count, as

    ...
    EXECUTE_QUERY;
    IF :BL_CONTROL.IT_COUNT=0 THEN
      error
    END IF;
    
  • Create the cursor using dynamic query

    Hello

    I declare a query:

    l_query VARCHAR2 (32000): = ' select distinct ' | : P_ITEM | "of OCM_CUSTOMER."

    How can I assign this cursor query?
    I tried, in vain.

    Declare
    l_query VARCHAR2 (32000): = ' select distinct ' | : P_ITEM | "of OCM_CUSTOMER."
    CURSOR l_cur IS l_query;
    Begin
    .......
    End

    Thanks for help on this.

    Hello

    You must use a REF CURSOR for that (there are a few examples in the Oracle documentation). Here is a code example:

    DECLARE
       TYPE EmpCurTyp IS REF CURSOR;
       emp_cv   EmpCurTyp;
       emp_rec  emp%ROWTYPE;
       sql_stmt VARCHAR2(200);
       my_job   VARCHAR2(15) := 'CLERK';
    BEGIN
       sql_stmt := 'SELECT * FROM emp WHERE job = :j';
       OPEN emp_cv FOR sql_stmt USING my_job;
       LOOP
          FETCH emp_cv INTO emp_rec;
          EXIT WHEN emp_cv%NOTFOUND;
          -- process record
       END LOOP;
       CLOSE emp_cv;
    END;
    

    Greetings,
    Roel

    http://roelhartman.blogspot.com/

  • How to optimize the select query executed in a cursor for loop?

    Hi friends,

    I run the code below and clocked at the same time for each line of code using DBMS_PROFILER.
    CREATE OR REPLACE PROCEDURE TEST
    AS
       p_file_id              NUMBER                                   := 151;
       v_shipper_ind          ah_item.shipper_ind%TYPE;
       v_sales_reserve_ind    ah_item.special_sales_reserve_ind%TYPE;
       v_location_indicator   ah_item.exe_location_ind%TYPE;
    
       CURSOR activity_c
       IS
          SELECT *
            FROM ah_activity_internal
           WHERE status_id = 30
             AND file_id = p_file_id;
    BEGIN
       DBMS_PROFILER.start_profiler ('TEST');
    
       FOR rec IN activity_c
       LOOP
          SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
                     INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
                     FROM ah_item --464000 rows in this table
                    WHERE item_id_edw IN (
                             SELECT item_id_edw
                               FROM ah_item_xref --700000 rows in this table
                              WHERE item_code_cust = rec.item_code_cust
                                AND facility_num IN (
                                       SELECT facility_code
                                         FROM ah_chain_div_facility --17 rows in this table
                                        WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
                                          AND div_id = (SELECT div_id
                                                          FROM ah_div --8 rows in this table 
                                                         WHERE division = rec.division)));
       END LOOP;
    
       DBMS_PROFILER.stop_profiler;
    EXCEPTION
       WHEN NO_DATA_FOUND
       THEN
          NULL;
       WHEN TOO_MANY_ROWS
       THEN
          NULL;
    END TEST;
    The SELECT inside the LOOP FOR cursor query took 773 seconds.
    I tried to use COLLECT in BULK instead of a cursor for loop, but it did not help.
    When I took the select query separately and executed with a value of the sample, and then he gave the results in a Flash of a second.

    All tables have primary key index.
    Any ideas what can be done to make this code more efficient?

    Thank you
    Raj.
    DECLARE
      v_chain_id ah_chain_div_facility.chain_id%TYPE := ah_internal_data_pkg.get_chain_id (p_file_id);
    
      CURSOR cur_loop IS
      SELECT * -- better off explicitly specifying columns
      FROM ah_activity_internal aai,
      (SELECT DISTINCT aix.item_code_cust, ad.division, ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
         INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
         FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
        WHERE ai.item_id_edw = aix.item_id_edw
          AND aix.facility_num = acdf.facility_code
          AND acdf.chain_id = v_chain_id
          AND acdf.div_id = ad.div_id) d
      WHERE aai.status_id = 30
        AND aai.file_id = p_file_id
        AND d.item_code_cust = aai.item_code_cust
        AND d.division = aai.division;         
    
    BEGIN
      FOR rec IN cur_loop LOOP
        ... DO your stuff ...
      END LOOP;
    END;  
    

    Published by: Dave hemming on December 4, 2008 09:17

  • 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

  • ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    Hello!

    I have a simple object type and a proecdure in which I am trying to use it to insert into another table

    -object

    CREATE ORREPLACETYPEmt_mtg ASOBJECT

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    CREATE ORREPLACETYPEREF_MTG ASTABLEOFMt_MTG ;

    -same structure as the use of sampletbl target table in the cursor query

    create table tbl_MT_MTG

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    -procedure

    CREATE ORREPLACEINTERIORTEST_PROCEDURE1

    AS

    ref_cur sys_refcursor ;

    REFR ref_mtg ;

    BEGIN

    OPEN ref_cur FOR

    Select acol,

    BCOL

    DE sampletbl rownum<10;

    Fetch ref_cur in bulk collectintorefr;

    Insert intotbl_MT_MTG(acol,bcol)selectacol,bcol fromtable(refr);

    commit;

    CLOSE Ref_cur;

    END;

    /

    When I run this procedure fails with

    ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    ORA-06512: at "TEST_PROCEDURE1", line 10

    ORA-06512: at line 2

    Any help on this please...

    Thanks to an OLD POST below

    so perfect helped me! Thank you

    Tubby

    After 5 years of more :-)

    How to store refcursor in collection How to store refcursor in collection

  • Plu SQL hang up when the cursor is fired.

    Hello
    I'm just a beginner in sql and plsql. I run the cursor query as below in my sql more window:

    Declare
    CURSOR emp_cursor IS SELECT * FROM employees WHERE employee_id = 100;
    employees emp_record % rowtype;
    Begin
    OPEN emp_cursor;
    LOOP
    LOOK INTO emp_record emp_cursor;
    DBMS_OUTPUT. Put_line (emp_record);
    END LOOP;
    NARROW Emp_cursor;
    END;

    While I try to run the plu SQL hang (the cursor (_) flashes to a new line in trying to retrieve the result). Even after 10-15 minutes the result is displayed and I cannot type in any other question, that the system is not ready. In short sql plu hangs up.

    Add the exit condition

    FETCH emp_cursor INTO emp_record;
    exit when emp_cursor%notfound;
    

    also your call to DBMS_OUTPUT. Procedure put_line() is not.
    It takes a VARCHAR2 as an input parameter, when you pass a variable with employees % rowtype.
    For me, it showed the following error:

    DBMS_OUTPUT.PUT_LINE(emp_record);
    *
    ERROR at line 8:
    ORA-06550: line 8, column 1:
    PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
    

    That should be:

    DBMS_OUTPUT.PUT_LINE(emp_record.employee_id); --Or whatever columns you want to display
    
  • If the mobile is valid or not by using the Pl/SQL cursor

    Hello

    Can someone please email me a cursor query to see if an mobileno is valid or not.

    Table name Cc_customer
    mobileno domain name
    length should be 10 and start by 9, length should be 12 and start with 919

    Do you mean something like this?

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select '9123456780' as mobile from dual union all
      2             select '919123456780' from dual union all
      3             select '12345' from dual union all
      4             select '1234567890' from dual union all
      5             select '123456789012' from dual)
      6  --
      7  -- END OF TEST DATA
      8  --
      9  select mobile
     10        ,case when regexp_like(mobile, '^((91)?9[0-9]{9})$') then 'valid'
     11         else 'invalid'
     12         end as chk
     13* from t
    SQL> /
    
    MOBILE       CHK
    ------------ -------
    9123456780   valid
    919123456780 valid
    12345        invalid
    1234567890   invalid
    123456789012 invalid
    
    SQL>
    
  • 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.

  • REF Cursor creating CSV files

    Hi guys

    I am writing a package to create several different CVS files of the database.
    I created a procedure for each files CVS to be created within the package.
    I set the cursor for each file to each intervention.
    Read slider and concatenate all the fields in a large chain of recording and write it to the file using the UTL_FILE utility.

    Is there a way I can create a common procedure and pass all other cases, file_name as a parameter and concatenate all the fields and records and write it to the file? I think the use of the REF cursor, but the problem is the cursor query is different for each file. Some files has 2 columns and some 200 columns.
    No idea how to make this work for any format?

    Your help will be very appreciated.

    Thanks and greetings

    Vijay

    Here is a generic procedure, I used for my part in the past...

    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.

  • Tuninng performance for cursor and insert in the procedure

    Hi all

    I need to set the oracle procedure. The procedure is as below.

    CREATE OR REPLACE PROCEDURE XYZ
    AS

    CURSOR ABCD_CUR IS
    SELECT a, b, c and d
    OF ABCD;
    RV_abcd ABCD_CUR % TYPE;

    BEGIN
    OPEN abcd_cur.
    loop
    extract the abcd_cur in rv_abcd;
    out on abcd_cur % NOTFOUND;
    BEGIN
    insert into one
    (
    SEQ_ID,
    A
    )
    VALUES
    (
    A_SEQ. NEXTVAL,
    rv_abcd. A
    );

    Insert into B
    (
    REF_SEQ_ID,
    SEQ_ID,
    B
    )
    VALUES
    (
    A_SEQ. CURVAL,
    B_SEQ. NEXTVAL,
    B
    );

    Insert into C
    (
    REF_SEQ_ID,
    SEQ_ID,
    C,
    D
    )
    values
    (
    A.SEQ. CURVAL,
    C_SEQ. NEXTVAL,
    C,
    D
    );
    EXCEPTION

    END;
    EXCEPTION
    END;


    the cursor query may sing either query or a query to join.

    For a single query, I used advice PARALLELS. but I'm not able to find out which suggests that I can use to join as well as what suspicion that I have to use for the INSERT statement.
    also, in addition to and delete, can what advice we use?

    Your entire code can be narrowed in a single SQL

    insert all
      into a(seq_id, a) values (a_seq.nextval, a)
      into b(REF_SEQ_ID, seq_id, b) values (a_seq.currval, b_seq.nextval, b)
      into c(ref_seq_id, seq_id, c, d) values (a_seq.currval, c_seq.nextval, c, d)
    select a, b, c, d
      from abcd
    
  • Cursor object and cursor for loop back

    I am beginner in Oracle and the serious database of programming in general. Right now I'm studying the sliders. I more or less understand how works when the cursor returns a record, but I can't understand why this does not work when the cursor return object and I use "cursor for loop.

    Here's the test code:

    CREATE or REPLACE TYPE typ_Test () AS OBJECT
    text VARCHAR2 (50)
    );

    CREATE TABLE t_Test to typ_Test;

    INSERT INTO t_Test VALUES (typ_Test('111'));
    INSERT INTO t_Test VALUES (typ_Test('222'));

    -work, simple loop
    DECLARE
    CURSOR cur IS SELECT VALUE (o) FROM t_Test o;
    typ_Test obj;
    BEGIN
    Heart OPEN;
    LOOP
    Fetch cur INTO obj;
    OUTPUT WHEN heart % NOTFOUND;
    dbms_output.put_line (obj. (Text);
    END LOOP;
    CLOSE cur;
    END;



    -doesn't work, cursor for loop
    DECLARE
    CURSOR cur IS SELECT VALUE (o) FROM t_Test o;
    BEGIN
    FOR news of obj IN LOOP
    dbms_output.put_line (obj. (Text);
    END LOOP;
    END;



    Error report:
    ORA-06550: line 5, column 30:
    PLS-00302: 'TEXT' element must be declared.
    ORA-06550: line 5, column 5:
    PL/SQL: Statement ignored
    06550 00000 - "line %s, column % s:\n%s".
    * Cause: Usually a PL/SQL compilation error.


    EDIT:
    In fact I found no examples on the net of its use "cursor for loop" with cursor that returns the object. Only when the record is returned...

    Published by: user10393567 on February 10, 2009 07:38

    user10393567 wrote:
    Now, in the 'loop' example for what looks "obj" is a folder, not an object, and 'x' is attribute of this recording... But the cursor must return an object, not a record... What Miss me?

    In the FOR loop "obj" is a pointer to the cursor/query, not a subject in its own right. It gives you just a reference in the query.

    So with that you are referring to the query, but then you need to refer to the column in the query that you are interested, so why there need a name (in my case I called it 'x'). 'x' is the object, and then you refer to the attribute of this object "text".

    In the first request that you simply select the value directly object in a variable, so no matter what it's called in the query values are returned by the position that is the first column in the query goes into the first variable in the INTO clause. Once you have that then the variable itself contains the object, and you can just refer to the attribute within that.

    Hope that makes sense.

  • 6i ref cursor no_data_found exception report

    I have a simple report with the cursor query a Ref on a scheme of database packed.

    The data model function has an exception handler for no_data_found.
    The exception sets a local variable in a program unit specification.
    A field with a message based on a column in support of place does not return.

    I try to give a message to the user and am not having any luck.

    Ideas?

    Thank you.

    You can create a text field with the text "no data found".
    This field gets a format trigger

    if  then
      return true;
    else
      return false;
    end;
    

Maybe you are looking for

  • Blutooth not stay connected 17 t J000 touchsmart victory 8.1

    I have a portable j000 17 t quad which is 2 months old.  Last week I tried to connect my phone to the computer via bluetooth.  I am running windows 8.1.   OK, I get 2 devices to connect for a few seconds to a minute, and then the computer says not co

  • Portege R100 will not start up CD

    I have a r100 and has now no OS and worse still it used to boot from external cd please can someone advise me how can I reload windows xp and have my computer back

  • store numbers in tabular form 2D production

    Hello I get the tension and position of a LUN in the form of numbers that are constantly changing. Now my question is that how can I keep these (marked in the image of red circle) numbers tabulated 2D? Thank you in advance.

  • HP pavilion 23xi support

    Hello I have a 23xi monitor and I love it! But I managed to scrape the stand on it while moving my PC arouund. I could find anywhere to buy a replacement stand! If someone has a link to one I would be very grateful! Thank you

  • How to get all the contacts in the group through the loop

    Hello world I know the name of grop, the address book,. How to loop all of the contacts in the Group and count them?