Cursor with bulk collect ...

Hi experts,
CURSOR ZIP_CUR IS SELECT * FROM ZIP_MASTER WHERE ..(SUBQUERY)
OPEN ZIP_CUR;
LOOP
     EXIT WHEN C1%NOTFOUND;
     FETCH ZIP_CUR BULK COLLECT INTO ZIP_REC LIMIT 1000;
          FOR I IN ZIP_REC.FIRST..ZIP_REC.LAST
          LOOP
END LOOP
END LOOP
If there is no data in cusor then the looping after bulk collect is throughing VALUE_ERROR
but why EXIT WHEN C1%NOTFOUND not terminating procedure??
now i am using the same select statement in cursor to find the data and raise user defined exception..
is there any other way to handle in execption??
Many thanks,
Kalinga..

You can try something like this -
satyaki>
satyaki>declare
  2   CURSOR c_emp
  3   IS
  4     SELECT *
  5     FROM emp_s
  6     WHERE deptno in (
  7                       SELECT deptno
  8                       from emp_s
  9                       where job = 'ANALYST'
10                     );
11               
12   type emp_tt is table of emp_s%rowtype index by pls_integer;
13  
14   emp_rec emp_tt;
15  begin
16     
17   OPEN c_emp;
18   LOOP
19    EXIT WHEN c_emp%NOTFOUND;
20    FETCH c_emp BULK COLLECT INTO emp_rec LIMIT 1000;
21    FOR I IN 1..emp_rec.COUNT
22    LOOP
23      dbms_output.put_line('Empno: '||emp_rec(I).empno);
24      dbms_output.put_line('Ename: '||emp_rec(I).ename);
25      dbms_output.put_line('Mgr: '||emp_rec(I).mgr);
26    END LOOP;
27   END LOOP;
28  exception
29    when others then
30      dbms_output.put_line(sqlerrm);
31  end;
32  /
Empno: 7902
Ename: FORD
Mgr: 7566
Empno: 7839
Ename: KING
Mgr: 7839
Empno: 7788
Ename: SCOTT
Mgr: 7566
Empno: 7698
Ename: BLAKE
Mgr: 7839
PL/SQL procedure successfully completed.
satyaki>Regards.
Satyaki De.

Similar Messages

  • Opening two cursors using open cursor with bulk collect on colections ..

    Is it possible to have the implementatiion of using bulk collect with collections using two open cursors ..
    first c1
    second c2
    open c1
    loop
    open c2
    loop
    end loop
    close c2
    end loop;
    close c1
    what i found is for every outer loop of cursor c1 , cursor c2 is open and closed for every record.
    is this willl imporove the performace .?
    EXAMPLE:-
    NOTE: The relatoin between finc and minc is one to many ..finc is parent and minc is child
    function chk_notnull_blank ( colname IN number ) return number is
    BEGIN
    if ( colname is NOT NULL and colname not in ( -8E14, -7E14, -6E14, -5E14, -4E14, -3E14, -2E14, -1E14, -1E9 )) then
    RETURN colname ;
    else
    RETURN 0;
    end if;
    END chk_notnull_blank;
    procedure Proc_AnnualFmlyTotIncSummary is
    CURSOR c_cur_finc IS SELECT FAMID FROM FINC ;
    CURSOR c_cur_minc IS SELECT FAMID, MEMBNO , ANFEDTX, ANGOVRTX, ANPRVPNX, ANRRDEDX, ANSLTX, SALARYX, SALARYBX, NONFARMX, NONFRMBX , FARMINCX, FRMINCBX, RRRETIRX, RRRETRBX, SOCRRX, INDRETX, JSSDEDX, SSIX, SSIBX from MINC minc WHERE FAMID IN ( SELECT FAMID FROM FINC finc WHERE minc.FAMID = finc.FAMID );
    v_tot_fsalaryx number := 0;
    v_tot_fnonfrmx number := 0;
    v_tot_ffrmincx number := 0;
    v_tot_frretirx number := 0;
    v_tot_findretx number := 0;
    v_tot_fjssdedx number := 0;
    v_tot_fssix number := 0;
    v_temp_sum_fsalaryx number := 0;
    v_temp_sum_fnonfrmx number := 0;
    v_temp_sum_ffrmincx number := 0;
    v_temp_sum_frretirx number := 0;
    v_temp_sum_findretx number := 0;
    v_temp_sum_fjssdedx number := 0;
    v_temp_sum_fssix number := 0;
    TYPE minc_rec IS RECORD (FAMID MINC.FAMID%TYPE, MEMBNO MINC.MEMBNO%TYPE , ANFEDTX MINC.ANFEDTX%TYPE, ANGOVRTX MINC.ANGOVRTX%TYPE , ANPRVPNX MINC.ANPRVPNX%TYPE , ANRRDEDX MINC.ANRRDEDX%TYPE , ANSLTX MINC.ANSLTX%TYPE, SALARYX MINC.SALARYX%TYPE , SALARYBX MINC.SALARYBX%TYPE , NONFARMX MINC.NONFARMX%TYPE , NONFRMBX MINC.NONFRMBX%TYPE, FARMINCX MINC.FARMINCX%TYPE , FRMINCBX MINC.FRMINCBX%TYPE , RRRETIRX MINC.RRRETIRX%TYPE , RRRETRBX MINC.RRRETRBX%TYPE, SOCRRX MINC.SOCRRX%TYPE , INDRETX MINC.INDRETX%TYPE , JSSDEDX MINC.JSSDEDX%TYPE , SSIX MINC.SSIX%TYPE , SSIBX MINC.SSIBX%TYPE );
    v_flag_boolean boolean := false;
    v_famid number ;
    v_stmt varchar2(3200) ;
    v_limit number := 50;
    v_temp_FAMTFEDX number := 0 ;
    v_temp_FGOVRETX number := 0 ;
    v_temp_FPRIVPENX number := 0 ;
    v_temp_FRRDEDX number := 0 ;
    v_temp_FSLTAXX number := 0 ;
    v_temp_FSALARYX number := 0 ;
    v_temp_FNONFRMX number := 0 ;
    v_temp_FFRMINCX number := 0 ;
    v_temp_FRRETIRX number := 0 ;
    v_temp_FINDRETX number := 0 ;
    v_temp_FJSSDEDX number := 0 ;
    v_temp_FSSIX number := 0 ;
    BEGIN
    OPEN c_cur_finc ;
    LOOP
         FETCH c_cur_finc BULK COLLECT INTO famid_type_tbl LIMIT v_limit;
         EXIT WHEN famid_type_tbl.COUNT = 0;
         FOR i in famid_type_tbl.FIRST..famid_type_tbl.LAST
         LOOP
         OPEN c_cur_minc ;
         LOOP
         FETCH c_cur_minc BULK COLLECT INTO minc_rec_type_tbl LIMIT v_limit;
         EXIT WHEN minc_rec_type_tbl.COUNT = 0;
              FOR j IN minc_rec_type_tbl.FIRST..minc_rec_type_tbl.LAST
              LOOP
              if ( famid_type_tbl(i) = minc_rec_type_tbl(j).FAMID ) THEN
              v_temp_FAMTFEDX := v_temp_FAMTFEDX + chk_notnull_blank(minc_rec_type_tbl(j).ANFEDTX );
              v_temp_FGOVRETX := v_temp_FGOVRETX + chk_notnull_blank(minc_rec_type_tbl(j).ANGOVRTX);
              v_temp_FPRIPENX := v_temp_FPRIPENX + chk_notnull_blank(minc_rec_type_tbl(j).ANPRVPNX);
              v_temp_FRRDEDX := v_temp_FRRDEDX + chk_notnull_blank(minc_rec_type_tbl(j).ANRRDEDX);
              v_temp_FSLTAXX := v_temp_FSLTAXX + chk_notnull_blank(minc_rec_type_tbl(j).ANSLTX );
              v_temp_FSALARYX := v_temp_FSALARYX + chk_notnull_blank(minc_rec_type_tbl(j).SALARYX ) + chk_notnull_blank(minc_rec_type_tbl(j).SALARYBX);
              v_temp_FNONFRMX := v_temp_FNONFRMX + chk_notnull_blank(minc_rec_type_tbl(j).NONFARMX) + chk_notnull_blank(minc_rec_type_tbl(j).NONFRMBX);
              v_temp_FFRMINCX := v_temp_FFRMINCX + chk_notnull_blank(minc_rec_type_tbl(j).FARMINCX) + chk_notnull_blank(minc_rec_type_tbl(j).FRMINCBX );
              v_temp_FRRETIRX := v_temp_FRRETIRX + chk_notnull_blank(minc_rec_type_tbl(j).RRRETIRX) + chk_notnull_blank(minc_rec_type_tbl(j).RRRETRBX ) + chk_notnull_blank(minc_rec_type_tbl(j).SOCRRX);
              v_temp_FINDREXT := v_temp_FINDRETX + chk_notnull_blank(minc_rec_type_tbl(j).INDRETX);
              v_temp_FJSSDEDX := v_temp_FJSSDEDX + chk_notnull_blank(minc_rec_type_tbl(j).JSSDEDX);
              v_temp_FSSIX := v_temp_FSSIX + chk_notnull_blank(minc_rec_type_tbl(j).SSIX ) + chk_notnull_blank(minc_rec_type_tbl(j).SSIBX);
              END IF;
              END LOOP;
         END LOOP ;
         CLOSE c_cur_minc;
         UPDATE FINC SET FAMTFEDX = v_temp_FAMTFEDX WHERE FAMID = famid_type_tbl(i);
         END LOOP;
    END LOOP;
    CLOSE c_cur_finc;
    END;
    EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    v_err_code := SQLCODE;
    v_err_msg := substr(SQLERRM, 1, 200);
    INSERT INTO audit_table (error_number, error_message) VALUES (v_err_code, v_err_msg);
    error_logging(p_error_code => substr(sqlerrm,1,9), p_error_message => substr(sqlerrm,12), p_package =>'PKG_FCI_APP',p_procedure => 'Proc_Annual_Deductions_FromPay ' , p_location => v_location);
    end Proc_AnnualFmlyTotIncSummary ;
    Is the proga efficient and free from compilation errors ..?
    thanks/kumar
    Edited by: kumar73 on Sep 22, 2010 12:48 PM

    function chk_notnull_blank ( colname IN number ) return number is Maybe this function should have its own forum:
    how to use case in this program
    Re: how to declare a formal parameter in a function of type record and access ?
    Re: how to define a function with table type parameter
    Re: creation of db trigger with error ..
    Re: How to write a trigger for the below scenario
    how to improve the code using advanced methods
    yours advice in improving the coding ..
    How to use bulk in multiple cursors !!
    ;-)

  • Use of FOR Cursor and BULK COLLECT INTO

    Dear all,
    in which case we prefer to use FOR cursor and cursor with BULK COLLECT INTO? The following contains two block that query identically where one is using FOR cursor, the other is using BULK COLLECT INTO . Which one that performs better given in the existing task? How do we measure performance between these two?
    I'm using sample HR schema:
    declare
    l_start number;
    BEGIN
    l_start:= DBMS_UTILITY.get_time;
    dbms_lock.sleep(1);
    FOR employee IN (SELECT e.last_name, j.job_title FROM employees e,jobs j
    where e.job_id=j.job_id and  e.job_id LIKE '%CLERK%' AND e.manager_id > 120 ORDER BY e.last_name)
    LOOP
      DBMS_OUTPUT.PUT_LINE ('Name = ' || employee.last_name || ', Job = ' || employee.job_title);
    END LOOP;
    DBMS_OUTPUT.put_line('total time: ' || to_char(DBMS_UTILITY.get_time - l_start) || ' hsecs');
    END;
    declare
    l_start number;
    type rec_type is table of varchar2(20);
    name_rec rec_type;
    job_rec rec_type;
    begin
    l_start:= DBMS_UTILITY.get_time;
    dbms_lock.sleep(1);
    SELECT e.last_name, j.job_title bulk collect into name_rec,job_rec FROM employees e,jobs j
    where e.job_id=j.job_id and  e.job_id LIKE '%CLERK%' AND e.manager_id > 120 ORDER BY e.last_name;
    for j in name_rec.first..name_rec.last loop
      DBMS_OUTPUT.PUT_LINE ('Name = ' || name_rec(j) || ', Job = ' || job_rec(j));
    END LOOP;
    DBMS_OUTPUT.put_line('total time: ' || to_char(DBMS_UTILITY.get_time - l_start) || ' hsecs');
    end;
    /In this code, I put timestamp in each block, but they are useless since they both run virtually instantaneous...
    Best regards,
    Val

    If you want to get 100% benifit of bulk collect then it must be implemented as below
    declare
         Cursor cur_emp
         is
         SELECT     e.last_name, j.job_title
         FROM     employees e,jobs j
         where     e.job_id=j.job_id
                   and  e.job_id LIKE '%CLERK%'
                   AND e.manager_id > 120
         ORDER BY e.last_name;
         l_start number;
         type rec_type is table of varchar2(20);
         name_rec rec_type;
         job_rec rec_type;
    begin
         l_start:= DBMS_UTILITY.get_time;
         dbms_lock.sleep(1);
         /*SELECT e.last_name, j.job_title bulk collect into name_rec,job_rec FROM employees e,jobs j
         where e.job_id=j.job_id and  e.job_id LIKE '%CLERK%' AND e.manager_id > 120 ORDER BY e.last_name;
         OPEN cur_emp;
         LOOP
              FETCH cur_emp BULK COLLECT INTO name_rec LIMIT 100;
              EXIT WHEN name_rec.COUNT=0;
              FOR j in 1..name_rec.COUNT
              LOOP
                   DBMS_OUTPUT.PUT_LINE ('Name = ' || name_rec(j) || ', Job = ' || job_rec(j));          
              END LOOP;
              EXIT WHEN cur_emp%NOTFOUND;
         END LOOP;
            CLOSE cur_emp;
         DBMS_OUTPUT.put_line('total time: ' || to_char(DBMS_UTILITY.get_time - l_start) || ' hsecs');
    end;
    /

  • Trouble with Bulk Collect to Ref cursor

    I'm trying to open a ref cursor to a dynamic query, and the fetch the cursor (BULK COLLECT)to the table type variable.But I keep getting the compilation error as 'PLS-00597: expression 'EMP_RESULTSET' in the INTO list is of wrong type'
    But when I use a simple select from a table and Bulk Collect directly to the table type variable it works. But that is not what I want.
    Can someone tell me where I have gone wrong in this stored proc I have listed below.
    your help will be highly appreciated.
    PROCEDURE SP_TEST_EMP_TABLE_TYPE (
          p_resultset OUT result_cursor          -- ref cursor as out parameter
       AS
        TYPE TYPE_REC_EMP is RECORD(EMPNO EMPLOYEE.EMPNO%TYPE, JOIN_DATE EMPLOYEE.JOIN_DATE%TYPE, SALARY EMPLOYEE.SALARY%TYPE); -- declare record type
        TYPE TYPE_TAB_EMP IS TABLE OF TYPE_REC_EMP;    -- declare table type
        EMP_RESULTSET TYPE_TAB_EMP; -- declare variable of type type_calendar_avail_resultset  
        SQUERY VARCHAR2(32000):='';
       BEGIN
        SQUERY:='SELECT EMPNO,JOIN_DATE,SALARY FROM EMPLOYEE WHERE EMPNO= 1001 AND JOIN_DATE=''20070530'' ';
        --select EMPNO,JOIN_DATE,SALARY BULK COLLECT INTO EMP_RESULTSET from  EMPLOYEE WHERE EMPNO=1001 AND JOIN_DATE='20070525';
        OPEN p_resultset FOR SQUERY;
          FETCH p_resultset BULK COLLECT INTO EMP_RESULTSET;
      EXCEPTION
          WHEN NO_DATA_FOUND
          THEN
             NULL;
          WHEN OTHERS
          THEN
             DBMS_OUTPUT.put_line (SQLERRM (SQLCODE));   
       END SP_TEST_EMP_TABLE_TYPE ;

    > i) I use a ref cursor to return back to the java
    front end, so I had to use a ref cursor.
    What is a ref cursor? It is not a data set. It is a pointer to a "SQL program". This program is created by the SQL Engine and the CBO that parses the SQL source code and determine an execution plan to get to the requested rows.
    When the client fetches from a (ref) cursor, the client is running this program and it find and returns the next row.
    There is no such thing as a physical result set for a cursor - no "copy" of the rows found for the source code SQL is made as a result set.
    > ii) I also use a dynamic sql, but I was thinking it
    wasn't useful for this posting, so tried to write a
    simple sql
    What is dynamic SQL? SQL where object names (e.g name of the table) is only known at run-time. Or where the filter (e.g. WHERE conditions) can only be determined at run time.
    If these are known, the SQL is static SQL.
    For both static and dynamic SQL, bind variables are critical. It is the biggest performance mistake (in Oracle) to hardcode values and literals into a SQL.
    > ii) I use a Bulk Collect to the table type
    collection, since I use a loop, for which I had to
    collect the results from each loop and finally send
    the resultset thru the ref cursor.
    Impossible. Nor does it make any sense. As stated, a ref cursor is a program and not a result set.
    What you intend to do is run a SQL against data. Copy this data into private/local PL/SQL memory. Construct another SQL against this data - which means that it needs to be shipped back to the SQL engine as it cannot use/read local PL/SQL memory structures. And the pass that SQL cursor to the client.
    What for?
    > I had earlier used the logic to for this using a
    temporary table, which works perfectly fine, but our
    DBA says we should avoid temporary tables since it
    makes additional read/write to the disk. This is the
    reason I'm using table type collection here.
    Your DBA is correct. One should so a single pass through a data set. Which is why simply passing a ref cursor for a SQL statement to the client is the simplest.
    It makes no sense copying SQL data into a collection and then copying that back into the SQL engine in order to throw a ref cursor around it.
    Basic client-server fundamentals.
    And why RTFM the Oracle manuals I've indicated is important. You need to understand the memory and processing architectures in Oracle in order to make an informed and correct decision.

  • Need help with Bulk Collect ForAll Update

    Hi - I'm trying to do a Bulk Collect/ForAll Update but am having issues.
    My declarations look like this:
         CURSOR cur_hhlds_for_update is
            SELECT hsh.household_id, hsh.special_handling_type_id
              FROM compas.household_special_handling hsh
                 , scr_id_lookup s
             WHERE hsh.household_id = s.id
               AND s.scr = v_scr
               AND s.run_date = TRUNC (SYSDATE)
               AND effective_date IS NULL
               AND special_handling_type_id = 1
               AND created_by != v_user;
         TYPE rec_hhlds_for_update IS RECORD (
              household_id  HOUSEHOLD_SPECIAL_HANDLING.household_id%type,
              spec_handl_type_id HOUSEHOLD_SPECIAL_HANDLING.SPECIAL_HANDLING_TYPE_ID%type
         TYPE spec_handling_update_array IS TABLE OF rec_hhlds_for_update;
         l_spec_handling_update_array  spec_handling_update_array;And then the Bulk Collect/ForAll looks like this:
           OPEN cur_hhlds_for_update;
           LOOP
            FETCH cur_hhlds_for_update BULK COLLECT INTO l_spec_handling_update_array LIMIT 1000;
            EXIT WHEN l_spec_handling_update_array.count = 0;
            FORALL i IN 1..l_spec_handling_update_array.COUNT
            UPDATE compas.household_special_handling
               SET effective_date =  TRUNC(SYSDATE)
                 , last_modified_by = v_user
                 , last_modified_date = SYSDATE
             WHERE household_id = l_spec_handling_update_array(i).household_id
               AND special_handling_type_id = l_spec_handling_update_array(i).spec_handl_type_id;
              l_special_handling_update_cnt := l_special_handling_update_cnt + SQL%ROWCOUNT;         
          END LOOP;And this is the error I'm receiving:
    ORA-06550: line 262, column 31:
    PLS-00436: implementation restriction: cannot reference fields of BULK In-BIND table of records
    ORA-06550: line 262, column 31:
    PLS-00382: expression is of wrong type
    ORA-06550: line 263, column 43:
    PL/SQL: ORA-22806: not an object or REF
    ORA-06550: line 258, column 9:
    PL/SQL: SQMy problem is that the table being updated has a composite primary key so I have two conditions in my where clause. This the the first time I'm even attempting the Bulk Collect/ForAll Update and it seems like it would be straight forward if I was only dealing with a single-column primary key. Can anyone please help advise me as to what I'm missing here or how I can accomplish this?
    Thanks!
    Christine

    You cannot reference a column inside a record when doin a for all. You need to refer as a whole collection . So you will need two collections.
    Try like this,
    DECLARE
       CURSOR cur_hhlds_for_update
       IS
          SELECT hsh.household_id, hsh.special_handling_type_id
            FROM compas.household_special_handling hsh, scr_id_lookup s
           WHERE hsh.household_id = s.ID
             AND s.scr = v_scr
             AND s.run_date = TRUNC (SYSDATE)
             AND effective_date IS NULL
             AND special_handling_type_id = 1
             AND created_by != v_user;
       TYPE arr_household_id IS TABLE OF HOUSEHOLD_SPECIAL_HANDLING.household_id%TYPE
                                   INDEX BY BINARY_INTEGER;
       TYPE arr_spec_handl_type_id IS TABLE OF HOUSEHOLD_SPECIAL_HANDLING.SPECIAL_HANDLING_TYPE_ID%TYPE
                                         INDEX BY BINARY_INTEGER;
       l_household_id_col         arr_household_id;
       l_spec_handl_type_id_col   arr_spec_handl_type_id;
    BEGIN
       OPEN cur_hhlds_for_update;
       LOOP
          FETCH cur_hhlds_for_update
            BULK COLLECT INTO l_household_id_col, l_spec_handl_type_id_col
          LIMIT 1000;
          EXIT WHEN cur_hhlds_for_update%NOTFOUND;
          FORALL i IN l_household_id_col.FIRST .. l_household_id_col.LAST
             UPDATE compas.household_special_handling
                SET effective_date = TRUNC (SYSDATE),
                    last_modified_by = v_user,
                    last_modified_date = SYSDATE
              WHERE household_id = l_household_id_col(i)
                AND special_handling_type_id = l_spec_handl_type_id_col(i);
       --l_special_handling_update_cnt := l_special_handling_update_cnt + SQL%ROWCOUNT; -- Not sure what this does.
       END LOOP;
    END;G.

  • Problem with BULK COLLECT with million rows - Oracle 9.0.1.4

    We have a requirement where are supposed to load 58 millions of rows into a FACT Table in our DATA WAREHOUSE. We initially planned to use Oracle Warehouse Builder but due to performance reasons, decided to write custom code. We wrote a custome procedure which opens a simple cursor and reads all the 58 million rows from the SOURCE Table and in a loop processes the rows and inserts the records into a TARGET Table. The logic works fine but it took 20hrs to complete the load.
    We then tried to leverage the BULK COLLECT and FORALL and PARALLEL options and modified our PL/SQL code completely to reflect these. Our code looks very simple.
    1. We declared PL/SQL BINARY_INDEXed Tables to store the data in memory.
    2. We used BULK COLLECT into FETCH the data.
    3. We used FORALL statement while inserting the data.
    We did not introduce any of our transformation logic yet.
    We tried with the 600,000 records first and it completed in 1 min and 29 sec with no problems. We then doubled the no. of rows to 1.2 million and the program crashed with the following error:
    ERROR at line 1:
    ORA-04030: out of process memory when trying to allocate 16408 bytes (koh-kghu
    call ,pmucalm coll)
    ORA-06512: at "VVA.BULKLOAD", line 66
    ORA-06512: at line 1
    We got the same error even with 1 million rows.
    We do have the following configuration:
    SGA - 8.2 GB
    PGA
    - Aggregate Target - 3GB
    - Current Allocated - 439444KB (439 MB)
    - Maximum allocated - 2695753 KB (2.6 GB)
    Temp Table Space - 60.9 GB (Total)
    - 20 GB (Available approximately)
    I think we do have more than enough memory to process the 1 million rows!!
    Also, some times the same program results in the following error:
    SQL> exec bulkload
    BEGIN bulkload; END;
    ERROR at line 1:
    ORA-03113: end-of-file on communication channel
    We did not even attempt the full load. Also, we are not using the PARALLEL option yet.
    Are we hitting any bug here? Or PL/SQL is not capable of mass loads? I would appreciate any thoughts on this?
    Thanks,
    Haranadh
    Following is the code:
    set echo off
    set timing on
    create or replace procedure bulkload as
    -- SOURCE --
    TYPE src_cpd_dt IS TABLE OF ima_ama_acct.cpd_dt%TYPE;
    TYPE src_acqr_ctry_cd IS TABLE OF ima_ama_acct.acqr_ctry_cd%TYPE;
    TYPE src_acqr_pcr_ctry_cd IS TABLE OF ima_ama_acct.acqr_pcr_ctry_cd%TYPE;
    TYPE src_issr_bin IS TABLE OF ima_ama_acct.issr_bin%TYPE;
    TYPE src_mrch_locn_ref_id IS TABLE OF ima_ama_acct.mrch_locn_ref_id%TYPE;
    TYPE src_ntwrk_id IS TABLE OF ima_ama_acct.ntwrk_id%TYPE;
    TYPE src_stip_advc_cd IS TABLE OF ima_ama_acct.stip_advc_cd%TYPE;
    TYPE src_authn_resp_cd IS TABLE OF ima_ama_acct.authn_resp_cd%TYPE;
    TYPE src_authn_actvy_cd IS TABLE OF ima_ama_acct.authn_actvy_cd%TYPE;
    TYPE src_resp_tm_id IS TABLE OF ima_ama_acct.resp_tm_id%TYPE;
    TYPE src_mrch_ref_id IS TABLE OF ima_ama_acct.mrch_ref_id%TYPE;
    TYPE src_issr_pcr IS TABLE OF ima_ama_acct.issr_pcr%TYPE;
    TYPE src_issr_ctry_cd IS TABLE OF ima_ama_acct.issr_ctry_cd%TYPE;
    TYPE src_acct_num IS TABLE OF ima_ama_acct.acct_num%TYPE;
    TYPE src_tran_cnt IS TABLE OF ima_ama_acct.tran_cnt%TYPE;
    TYPE src_usd_tran_amt IS TABLE OF ima_ama_acct.usd_tran_amt%TYPE;
    src_cpd_dt_array src_cpd_dt;
    src_acqr_ctry_cd_array      src_acqr_ctry_cd;
    src_acqr_pcr_ctry_cd_array     src_acqr_pcr_ctry_cd;
    src_issr_bin_array      src_issr_bin;
    src_mrch_locn_ref_id_array     src_mrch_locn_ref_id;
    src_ntwrk_id_array      src_ntwrk_id;
    src_stip_advc_cd_array      src_stip_advc_cd;
    src_authn_resp_cd_array      src_authn_resp_cd;
    src_authn_actvy_cd_array      src_authn_actvy_cd;
    src_resp_tm_id_array      src_resp_tm_id;
    src_mrch_ref_id_array      src_mrch_ref_id;
    src_issr_pcr_array      src_issr_pcr;
    src_issr_ctry_cd_array      src_issr_ctry_cd;
    src_acct_num_array      src_acct_num;
    src_tran_cnt_array      src_tran_cnt;
    src_usd_tran_amt_array      src_usd_tran_amt;
    j number := 1;
    CURSOR c1 IS
    SELECT
    cpd_dt,
    acqr_ctry_cd ,
    acqr_pcr_ctry_cd,
    issr_bin,
    mrch_locn_ref_id,
    ntwrk_id,
    stip_advc_cd,
    authn_resp_cd,
    authn_actvy_cd,
    resp_tm_id,
    mrch_ref_id,
    issr_pcr,
    issr_ctry_cd,
    acct_num,
    tran_cnt,
    usd_tran_amt
    FROM ima_ama_acct ima_ama_acct
    ORDER BY issr_bin;
    BEGIN
    OPEN c1;
    FETCH c1 bulk collect into
    src_cpd_dt_array ,
    src_acqr_ctry_cd_array ,
    src_acqr_pcr_ctry_cd_array,
    src_issr_bin_array ,
    src_mrch_locn_ref_id_array,
    src_ntwrk_id_array ,
    src_stip_advc_cd_array ,
    src_authn_resp_cd_array ,
    src_authn_actvy_cd_array ,
    src_resp_tm_id_array ,
    src_mrch_ref_id_array ,
    src_issr_pcr_array ,
    src_issr_ctry_cd_array ,
    src_acct_num_array ,
    src_tran_cnt_array ,
    src_usd_tran_amt_array ;
    CLOSE C1;
    FORALL j in 1 .. src_cpd_dt_array.count
    INSERT INTO ima_dly_acct (
         CPD_DT,
         ACQR_CTRY_CD,
         ACQR_TIER_CD,
         ACQR_PCR_CTRY_CD,
         ACQR_PCR_TIER_CD,
         ISSR_BIN,
         OWNR_BUS_ID,
         USER_BUS_ID,
         MRCH_LOCN_REF_ID,
         NTWRK_ID,
         STIP_ADVC_CD,
         AUTHN_RESP_CD,
         AUTHN_ACTVY_CD,
         RESP_TM_ID,
         PROD_REF_ID,
         MRCH_REF_ID,
         ISSR_PCR,
         ISSR_CTRY_CD,
         ACCT_NUM,
         TRAN_CNT,
         USD_TRAN_AMT)
         VALUES (
         src_cpd_dt_array(j),
         src_acqr_ctry_cd_array(j),
         null,
         src_acqr_pcr_ctry_cd_array(j),
              null,
              src_issr_bin_array(j),
              null,
              null,
              src_mrch_locn_ref_id_array(j),
              src_ntwrk_id_array(j),
              src_stip_advc_cd_array(j),
              src_authn_resp_cd_array(j),
              src_authn_actvy_cd_array(j),
              src_resp_tm_id_array(j),
              null,
              src_mrch_ref_id_array(j),
              src_issr_pcr_array(j),
              src_issr_ctry_cd_array(j),
              src_acct_num_array(j),
              src_tran_cnt_array(j),
              src_usd_tran_amt_array(j));
    COMMIT;
    END bulkload;
    SHOW ERRORS
    -----------------------------------------------------------------------------

    do you have a unique key available in the rows you are fetching?
    It seems a cursor with 20 million rows that is as wide as all the columnsyou want to work with is a lot of memory for the server to use at once. You may be able to do this with parallel processing (dop over 8) and a lot of memory for the warehouse box (and the box you are extracting data from)...but is this the most efficient (and thereby fastest) way to do it?
    What if you used a cursor to select a unique key only, and then during the cursor loop fetch each record, transform it, and insert it into the target?
    Its a different way to do a lot at once, but it cuts down on the overall memory overhead for the process.
    I know this isnt as elegant as a single insert to do it all at once, but sometimes trimming a process down so it takes less resources at any given moment is much faster than trying to do the whole thing at once.
    My solution is probably biased by transaction systems, so I would be interested in what the data warehouse community thinks of this.
    For example:
    source table my_transactions (tx_seq_id number, tx_fact1 varchar2(10), tx_fact2 varchar2(20), tx_fact3 number, ...)
    select a cursor of tx_seq_id only (even at 20 million rows this is not much)
    you could then either use a for loop or even bulk collect into a plsql collection or table
    then process individually like this:
    procedure process_a_tx(p_tx_seq_id in number)
    is
    rTX my_transactions%rowtype;
    begin
    select * into rTX from my_transactions where tx_seq_id = p_tx_seq_id;
    --modify values as needed
    insert into my_target(a, b, c) values (rtx.fact_1, rtx.fact2, rtx.fact3);
    commit;
    exception
    when others
    rollback;
    --write to a log or raise and exception
    end process_a_tx;
    procedure collect_tx
    is
    cursor tx is
    select tx_seq_id from my_transactions;
    begin
    for rTx in cTx loop
    process_a_tx(rtx.tx_seq_id);
    end loop;
    end collect_tx;

  • SAVE EXCEPTIONS when fetching from cursors by BULK COLLECT possible?

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
    Hello,
    I'm using an Cursor's FETCH by BULK COLLECT INTO mydata...
    Is it possible to SAVE EXCEPTIONS like with FORALL? Or is there any other possibility to handle exceptions during bulk-fetches?
    Regards,
    Martin

    The cursor's SELECT-statement uses TO_DATE(juldat,'J')-function (for converting an julian date value to DATE), but some rows contain an invalid juldat-value (leading to ORA-01854).
    I want to handle this "rows' exceptions" like in FORALL.
    But it could also be any other (non-Oracle/self-made) function within "any" BULK instruction raising (un)wanted exceptions... how can I handle these ones?
    Martin

  • Any way to use cursor values inside other cursor by bulk collect?

    hi,
    Is there any way to use cursor get_tables value insdide loop get column if i am using bulk collect in both cursors?
    I tried a lot but i am nt able to do it.kindly help...
    create or replace procedure MULTIPLE_CURSORS_PROC is
    v_owner varchar2(40);
    v_table_name varchar2(40);
    v_column_name varchar2(100);
    cursor get_tables is
    select distinct tbl.owner, tbl.table_name
    from all_tables tbl
    where tbl.owner = 'SYSTEM';
    cursor get_columns is
    select distinct col.column_name
    from all_tab_columns col
    where col.owner = v_owner
    and col.table_name = v_table_name;
    begin
    open get_tables;
    loop
    fetch get_tables into v_owner, v_table_name;
    open get_columns;
    loop
    fetch get_columns into v_column_name;
    end loop;
    close get_columns;
    end loop;
    close get_tables;
    end ;

    hi there
    Refer this
    CREATE OR REPLACE PROCEDURE MULTIPLE_CURSORS_PROC
    IS
       TYPE scol IS VARRAY (10000) OF VARCHAR2 (32767);
       v_table_name    scol;
       v_column_name   scol;
       TYPE curtyp IS REF CURSOR;
       get_columns     curtyp;
       CURSOR get_tables
       IS
          SELECT   DISTINCT tbl.table_name
            FROM   all_tables tbl
           WHERE   tbl.owner = 'SYSTEM';
    BEGIN
       OPEN get_tables;
       LOOP
          FETCH get_tables BULK COLLECT INTO   v_table_name;
          FOR indx IN v_table_name.FIRST .. v_table_name.LAST
          LOOP
             SELECT   DISTINCT col.column_name
               BULK   COLLECT
               INTO   v_column_name
               FROM   all_tab_columns col
              WHERE   col.table_name = v_table_name (indx);
             FOR ind IN v_column_name.FIRST .. v_column_name.LAST
             LOOP
                DBMS_OUTPUT.put_line (v_column_name (ind));
             END LOOP;
          END LOOP;
          EXIT WHEN get_tables%NOTFOUND;
       END LOOP;
       CLOSE get_tables;
    END MULTIPLE_CURSORS_PROC;regards
    Hitesh

  • ORA-01722: invalid number error with Bulk collect

    Hi ,
    I have been using the script to delete old seasonal data from my application DB tables. The stored procedure has been created successfully but when i try to run the proc it has been throwing 'ORA-01722: invalid number' exception at line 'FETCH C1_CUR BULK COLLECT INTO C1_TYPE_VAR LIMIT v_bulklimit;'.
    Could you please help me here?
    Below is the stored proc:
    CREATE OR REPLACE PROCEDURE clean_old_season_data(P_SEASON VARCHAR2) AS
    CURSOR C1_CUR IS SELECT ROWID RID,pro.* FROM PROPS pro where pro.ITEMPK IN
    (SELECT sve.pk FROM SAVEDVALUEENTRY sve WHERE sve.p_parent IN
    (SELECT s.pk FROM SAVEDVALUES s WHERE s.P_MODIFIEDITEM IN
    (SELECT a.PK
    FROM products a
    WHERE a.p_season IN (select s.pk from Seasons s where s.P_code=P_SEASON)
    ) ) ) and rownum<5;
    CURSOR C2_DEL IS SELECT RID FROM PROPS_HISTORY;
    TYPE C1_TYPE IS TABLE OF C1_CUR%ROWTYPE;
    C1_TYPE_VAR C1_TYPE;
    TYPE C2_TYPE IS TABLE OF UROWID;
    C2_TYPE_VAR C2_TYPE;
    ex_dml_errors EXCEPTION;
    PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);
    l_error_count NUMBER;
    err_num NUMBER;
    err_msg VARCHAR2 (300);
    COMMIT_VARIABLE PLS_INTEGER:=0;
    v_bulklimit NUMBER:=2;
    BEGIN
    /*------------------ Data Selection and INSERTION IN HISTORY TABLE ---------------------------------------*/
    OPEN C1_CUR;
    LOOP
    DBMS_OUTPUT.put_line('Cursor opend now in loop');
    FETCH C1_CUR BULK COLLECT INTO C1_TYPE_VAR LIMIT v_bulklimit;//ERROR OCCURS HERE
    DBMS_OUTPUT.put_line('Cursor count is'|| C1_TYPE_VAR.COUNT);
    FORALL I IN 1..C1_TYPE_VAR.COUNT SAVE EXCEPTIONS
    INSERT INTO PROPS_HISTORY VALUES C1_TYPE_VAR(I);
    COMMIT_VARIABLE := COMMIT_VARIABLE + v_bulklimit;
    DBMS_OUTPUT.put_line('Commit variable'|| COMMIT_VARIABLE.COUNT);
    IF COMMIT_VARIABLE = v_bulklimit THEN
    COMMIT;
    COMMIT_VARIABLE := 0;
    END IF;
    EXIT WHEN C1_CUR%NOTFOUND;
    END LOOP;
    DBMS_OUTPUT.put_line('Cursor closed now in loop and data inserted in history table');
    CLOSE C1_CUR;
    /*------------------ Data Selection and DELETION IN Live TABLE ---------------------------------------*/
    COMMIT_VARIABLE := 0;
    OPEN C2_DEL;
    LOOP
    FETCH C2_DEL BULK COLLECT INTO C2_TYPE_VAR LIMIT 2;
    FORALL I IN 1..C2_TYPE_VAR.COUNT SAVE EXCEPTIONS
    DELETE FROM PROPS WHERE ROWID = C2_TYPE_VAR(I);
    COMMIT_VARIABLE := COMMIT_VARIABLE + 2;
    IF COMMIT_VARIABLE = 2 THEN
    COMMIT;
    COMMIT_VARIABLE := 0;
    END IF;
    EXIT WHEN C2_DEL%NOTFOUND;
    END LOOP;
    CLOSE C2_DEL;
    END;

    Although there are many things which should not have been done in the posted code, I could not find any reason why the Invalid number error should occur at the Fetch clause.
    I would suggest you to Insert into Table by providing the Order of Columns i.e. Insert into table (col1, ... colN) values (coll(i).col1...col(i).colN);
    I tested below code and it did not give any errors.
    drop table test_table;
    create table test_Table
      rid   varchar2(100),
      emp_id  number(5),
      fname   varchar2(20),
      lname   varchar2(50)
    set serveroutput on;
    declare
      cursor c_cur is
          select rowid rid, e.*
            from employees e
           where rownum < 10;
      type typ_cur is table of c_cur%rowtype;
      typ typ_cur;
      l_bulk_limit    number := 5;
    begin
      open c_cur;
      loop
        fetch c_cur bulk collect into typ limit l_bulk_limit;
        dbms_output.put_line('Collection Count :: ' || typ.count);
        forall i in 1..typ.count --typ.first..typ.last
          insert into test_Table (rid, emp_id, fname, lname) values (typ(i).rid,typ(i).employee_id,typ(i).first_name,typ(i).last_name);
        dbms_output.put_line('Processed ' || l_bulk_limit || ' records.');
        exit when c_cur%notfound;
      end loop;
      commit;
    end;
    select * from test_table;PS:- 1. When you are processing only 4 Records, then why are you breaking them in 2 Loops?
    2. Why Commit every time you are processing a DML? Why not maintain an Error Flag and Rollback the Transaction as soon as error is encountered?
    3. Use "{code}" (Exclude Double Quotes) to format the code. I am not sure if works.
    Regards,
    P.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Collection with bulk collect , statement is not executed..

    DECLARE
              CURSOR cur_upt IS SELECT ts.user_id, ts.lot_id, ts.ml_ac_no, ts.td_prs_dt, ts.unit_cost, ts.cost_basis
              FROM tb_xop_sharelot_frac_snap fs, tb_xop_sharelot ts
              WHERE fs.lot_id=ts.lot_id AND fs.user_id=ts.user_id;
    TYPE tx_tab IS TABLE OF tb_xop_sharelot.user_id%TYPE;
    ltab tx_tab;
    TYPE tx_tab1 IS TABLE OF tb_xop_sharelot.lot_id%TYPE;
    ltab1 tx_tab1;
    TYPE tx_tab2 IS TABLE OF tb_xop_sharelot.ml_ac_no%TYPE;
    ltab2 tx_tab2;
    TYPE tx_tab3 IS TABLE OF tb_xop_sharelot.td_prs_dt%TYPE;
    ltab3 tx_tab3;
    TYPE tx_tab4 IS TABLE OF tb_xop_sharelot.unit_cost%TYPE;
    ltab4 tx_tab4;
    TYPE tx_tab5 IS TABLE OF tb_xop_sharelot.cost_basis%TYPE;
    ltab5 tx_tab5;
    BEGIN
              INSERT INTO tb_xop_sharelot_frac_snap (lot_id, jemq_num, lot_qy, activity_type, LOT_SL_CREATE_DT,
                   LOT_SL_CLOSE_DT, lot_status, frac_recon, hist_flag, create_dt, user_id)
                   (SELECT lot_id, jemq_num, lot_qy, activity_type, LOT_SL_CREATE_DT,
                   LOT_SL_CLOSE_DT,lot_status, frac_recon, hist_flag, create_dt, user_id FROM tb_xop_sharelot_fraction);
              OPEN Cur_upt;
    LOOP
              FETCH Cur_upt BULK COLLECT INTO ltab, ltab1, ltab2, ltab3, ltab4, ltab5 LIMIT 5000;
    EXIT WHEN cur_upt%NOTFOUND;
              END LOOP;
              CLOSE cur_upt;
              FORALL i IN ltab.FIRST..ltab.LAST
    UPDATE tb_xop_sharelot_frac_snap SET ml_ac_no=ltab2(i)/*, td_prs_dt=ltab3(i),
              unit_cost=ltab4(i), cost_basis=ltab5(i)*/ WHERE user_id=ltab(i) AND lot_id=ltab1(i);
              COMMIT;
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLCODE|| ' ' ||SQLERRM);
    END;

    This is the third question you have posted just putting one short subject and only posting code not formatted.
    I suggest you to read SQL and PL/SQL FAQ and avoid posting your question in this way as they will be ignored.
    Please:
    a) post sample data
    b) post your code formatted
    i.e.:
    {noformat}{noformat}
    SELECT ...
    {noformat}{noformat}
    c) explain what problem you are facing in details (including oracle errors)
    d) explain the logic you want to have
    e) post your expected output.
    Regards.
    Al

  • Problem with Bulk Collect ... FORALL

    I've written following code to bulk collect records from a cursor into a collection and insert it into a table using FORALL loop
    OPEN x;
        LOOP
          FETCH x BULK COLLECT INTO v_collection LIMIT 1000;
          FORALL i IN 1..v_collection.count
              INSERT INTO tablename(column1, column2) VALUES(v_collection(i).val1, v_collection(i).val2);
              COMMIT;
          EXIT WHEN x%NOTFOUND;
        END LOOP;I have verified that query which is executed by cursor returns records but when my procedure is executed the insert statement inside the FORALL loop never executes. Am I missing something over here?
    Regards,
    Fahad

    Yes, the cursor is returning a row.
    I've found the solution myself. There was a trigger which was deleting data on commit. Due to this, the records were not inserting.

  • Forall with bulk collect .. getting error

    it's 10 g.
    gettting this error.
    drop table t2;
    create table t2
    ( seq_id number,
      act number,
       is_p varchar2(1),
      other varchar2(20)
    insert into t2 values(1,2,'N','Test 1');
    drop table t3;
    create table t3
    ( seq_id number
    -- ,act number
    --  ,is_p varchar2(1)
    set serveroutput on
    declare
    type t2_r is table of t2%ROWTYPE;
    cursor c1 is select * from t2 ;
      t t2_r;
    begin
    open c1;
    fetch c1 BULK collect into t;
    forall i in 1..t.count
       insert into t3(seq_id) values (t(i).seq_id);
    end;
    ORA-06550: line 10, column 35:
    PLS-00436: implementation restriction: cannot reference fields of BULK In-BIND table of records
    ORA-06550: line 10, column 35:
    PLS-00382: expression is of wrong type
    ORA-06550: line 10, column 35:
    PL/SQL: ORA-22806: not an object or REF
    ORA-06550: line 10, column 4:
    PL/SQL: SQL Statement ignored

    The PLS-00436 is an annoying error message indeed.
    On 10g:
    rwijk@ORA10GR2> create table t2
      2  ( seq_id number,
      3    act number,
      4     is_p varchar2(1),
      5    other varchar2(20)
      6  );
    Tabel is aangemaakt.
    rwijk@ORA10GR2> insert into t2 values(1,2,'N','Test 1');
    1 rij is aangemaakt.
    rwijk@ORA10GR2> create table t3
      2  ( seq_id number
      3   -- ,act number
      4   --  ,is_p varchar2(1)
      5  );
    Tabel is aangemaakt.
    rwijk@ORA10GR2> declare
      2   type t2_r is table of t2%ROWTYPE;
      3   cursor c1 is select * from t2 ;
      4    t t2_r;
      5  begin
      6    open c1;
      7    fetch c1 BULK collect into t;
      8    forall i in 1..t.count
      9     insert into t3(seq_id) values (t(i).seq_id);
    10    close c1;
    11  end;
    12  /
       insert into t3(seq_id) values (t(i).seq_id);
    FOUT in regel 9:
    .ORA-06550: Regel 9, kolom 35:
    PLS-00436: Implementatierestrictie: kan niet verwijzen naar velden van BULK In-BIND-recordtabel..
    ORA-06550: Regel 9, kolom 35:
    PLS-00382: Uitdrukking heeft onjuist type..
    ORA-06550: Regel 9, kolom 35:
    PL/SQL: ORA-22806: Geen object of REF..
    ORA-06550: Regel 9, kolom 4:
    PL/SQL: SQL Statement ignored.You can code it slightly different using SQL object types like this, to make the code work in 10g:
    rwijk@ORA10GR2> create type t2_otype is object
      2  ( seq_id number
      3  , act number
      4  , is_p varchar2(1)
      5  , other varchar2(20)
      6  );
      7  /
    Type is aangemaakt.
    rwijk@ORA10GR2> create type t2s is table of t2_otype;
      2  /
    Type is aangemaakt.
    rwijk@ORA10GR2> declare
      2    cursor c1 is select t2_otype(seq_id,act,is_p,other) from t2 ;
      3    t t2s;
      4  begin
      5    open c1;
      6    fetch c1 BULK collect into t;
      7    forall i in 1..t.count
      8      insert into t3(seq_id) values (treat(t(i) as t2_otype).seq_id);
      9    close c1;
    10  end;
    11  /
    PL/SQL-procedure is geslaagd.On 11g, as said, you don't have to modify your code:
    rwijk@ORA11G> create table t2
      2  ( seq_id number,
      3    act number,
      4     is_p varchar2(1),
      5    other varchar2(20)
      6  );
    Tabel is aangemaakt.
    rwijk@ORA11G> insert into t2 values(1,2,'N','Test 1');
    1 rij is aangemaakt.
    rwijk@ORA11G> create table t3
      2  ( seq_id number
      3   -- ,act number
      4   --  ,is_p varchar2(1)
      5  );
    Tabel is aangemaakt.
    rwijk@ORA11G> declare
      2   type t2_r is table of t2%ROWTYPE;
      3   cursor c1 is select * from t2 ;
      4    t t2_r;
      5  begin
      6    open c1;
      7    fetch c1 BULK collect into t;
      8    forall i in 1..t.count
      9     insert into t3(seq_id) values (t(i).seq_id);
    10    close c1;
    11  end;
    12  /
    PL/SQL-procedure is geslaagd.Regards,
    Rob.

  • How can I see error when  execute UPDATE with BULK COLLECT

    Hi
    I have a code
       CURSOR c_renov_eleg IS
            SELECT t2.ROWID
              FROM mytable  t2
         WHERE  BLA BLA BLA
    OPEN c_renov_eleg;
          LOOP
            FETCH c_renov_eleg BULK COLLECT
              INTO w_rowid LIMIT 1000;
            FORALL i IN 1 .. w_rowid.COUNT
              UPDATE mytable
                 SET id_baixa_elegibilidade = K_SCNOB_BXA_RENOVACAO,
                      column01         = arr_ciclo(1),
                     dt_ultima_atualizacao  = SYSDATE,
                     cd_usuario_atualizacao = USER
               WHERE ROWID = w_rowid(i);
            COMMIT;
            EXIT WHEN c_renov_eleg%NOTFOUND;
          END LOOP;How can I to sse when and where have some error ? , in Exception return what record ?

    How can I to sse when and where have some error
    ?Have a look at
    [url=http://download.oracle.com/docs/cd/B28359_01/appd
    ev.111/b28370/tuning.htm#i49099]Handling FORALL
    Exceptions (%BULK_EXCEPTIONS Attribute).btw. just never ever commit in a for loop ;)
    (Tubby is just too fast ;))
    Hi , thank all
    btw. just never ever commit in a for loopWhen I must to COMMIT using BULK COLLECT ?

  • UNION operator with BULK COLLECT for a collection type

    Hi all,
    I created a table type as below:
    create or replace type coltest is table of number;
    Below are 3 PL/SQL blocks that populate data into variables of the above mentioned table type:
    BLOCK 1:
    DECLARE
    col1 coltest := coltest(1, 2, 3, 4, 5, 11);
    col2 coltest := coltest(6, 7, 8, 9, 10);
    col3 coltest := coltest();
    BEGIN
    SELECT * BULK COLLECT
    INTO col1
    FROM (SELECT *
    FROM TABLE(CAST(col1 AS coltest))
    UNION ALL
    SELECT * FROM TABLE(CAST(col2 AS coltest)));
    dbms_output.put_line('col1');
    dbms_output.put_line('col1.count: ' || col1.COUNT);
    FOR i IN 1 .. col1.COUNT
    LOOP
    dbms_output.put_line(col1(i));
    END LOOP;
    END;
    OUPUT:
    col1
    col1.count: 5
    6
    7
    8
    9
    10
    BLOCK 2:
    DECLARE
    col1 coltest := coltest(1, 2, 3, 4, 5, 11);
    col2 coltest := coltest(6, 7, 8, 9, 10);
    col3 coltest := coltest();
    BEGIN
    SELECT * BULK COLLECT
    INTO col2
    FROM (SELECT *
    FROM TABLE(CAST(col1 AS coltest))
    UNION ALL
    SELECT * FROM TABLE(CAST(col2 AS coltest)));
    dbms_output.put_line('col2');
    dbms_output.put_line('col2.count: ' || col2.COUNT);
    FOR i IN 1 .. col2.COUNT
    LOOP
    dbms_output.put_line(col2(i));
    END LOOP;
    END;
    OUTPUT:
    col2
    col2.count: 6
    1
    2
    3
    4
    5
    11
    BLOCK 3:
    DECLARE
    col1 coltest := coltest(1, 2, 3, 4, 5, 11);
    col2 coltest := coltest(6, 7, 8, 9, 10);
    col3 coltest := coltest();
    BEGIN
    SELECT * BULK COLLECT
    INTO col3
    FROM (SELECT *
    FROM TABLE(CAST(col1 AS coltest))
    UNION ALL
    SELECT * FROM TABLE(CAST(col2 AS coltest)));
    dbms_output.put_line('col3');
    dbms_output.put_line('col3.count: ' || col3.COUNT);
    FOR i IN 1 .. col3.COUNT
    LOOP
    dbms_output.put_line(col3(i));
    END LOOP;
    END;
    OUTPUT:
    col3
    col3.count: 11
    1
    2
    3
    4
    5
    11
    6
    7
    8
    9
    10
    Can anyone please explain the output of BLOCK 1 and 2? Why doesn't bulk collect into col1 and col2 return 11 as count?

    Consider the following code from your block.
    select *
      bulk collect into col1
      from (
             select *
               from table(cast(col1 as coltest))
              union
                all
             select *
               from table(cast(col2 as coltest))
           ); Now my assumption would be like this.
    PL/SQL engine would recognize the SQL part and will be sending it to SQL Engine for processing. Before doing that it will try to initialize the SQL collection variable COL1. Now when SQL Engine process the SELECT, COL1 would be empty already. And thats the reason you don't get the as 5 and not 11.

  • Need to increase performance-bulk collect in cursor with limit and in the for loop inserting into the trigger table

    Hi all,
    I have a performance issue in the below code,where i am trying to insert the data from table_stg into target_tab and in parent_tab tables and then to child tables via cursor with bulk collect .the target_tab and parent_tab are huge tables and have a row wise trigger enabled on it .the trigger is mandatory . This timetaken for this block to execute is 5000 seconds.Now my requirement is to reduce it to 5 to 10 mins.
    can someone please guide me here.Its bit urgent .Awaiting for your response.
    declare
    vmax_Value NUMBER(5);
      vcnt number(10);
      id_val number(20);
      pc_id number(15);
      vtable_nm VARCHAR2(100);
      vstep_no  VARCHAR2(10);
      vsql_code VARCHAR2(10);
      vsql_errm varchar2(200);
      vtarget_starttime timestamp;
      limit_in number :=10000;
      idx           number(10);
              cursor stg_cursor is
             select
                   DESCRIPTION,
                   SORT_CODE,
                   ACCOUNT_NUMBER,
                     to_number(to_char(CORRESPONDENCE_DATE,'DD')) crr_day,
                     to_char(CORRESPONDENCE_DATE,'MONTH') crr_month,
                     to_number(substr(to_char(CORRESPONDENCE_DATE,'DD-MON-YYYY'),8,4)) crr_year,
                   PARTY_ID,
                   GUID,
                   PAPERLESS_REF_IND,
                   PRODUCT_TYPE,
                   PRODUCT_BRAND,
                   PRODUCT_HELD_ID,
                   NOTIFICATION_PREF,
                   UNREAD_CORRES_PERIOD,
                   EMAIL_ID,
                   MOBILE_NUMBER,
                   TITLE,
                   SURNAME,
                   POSTCODE,
                   EVENT_TYPE,
                   PRIORITY_IND,
                   SUBJECT,
                   EXT_PRD_ID_TX,
                   EXT_PRD_HLD_ID_TX,
                   EXT_SYS_ID,
                   EXT_PTY_ID_TX,
                   ACCOUNT_TYPE_CD,
                   COM_PFR_TYP_TX,
                   COM_PFR_OPT_TX,
                   COM_PFR_RSN_CD
             from  table_stg;
    type rec_type is table of stg_rec_type index by pls_integer;
    v_rt_all_cols rec_type;
    BEGIN
      vstep_no   := '0';
      vmax_value := 0;
      vtarget_starttime := systimestamp;
      id_val    := 0;
      pc_id     := 0;
      success_flag := 0;
              vstep_no  := '1';
              vtable_nm := 'before cursor';
        OPEN stg_cursor;
              vstep_no  := '2';
              vtable_nm := 'After cursor';
       LOOP
              vstep_no  := '3';
              vtable_nm := 'before fetch';
    --loop
        FETCH stg_cursor BULK COLLECT INTO v_rt_all_cols LIMIT limit_in;
                  vstep_no  := '4';
                  vtable_nm := 'after fetch';
    --EXIT WHEN v_rt_all_cols.COUNT = 0;
        EXIT WHEN stg_cursor%NOTFOUND;
    FOR i IN 1 .. v_rt_all_cols.COUNT
      LOOP
       dbms_output.put_line(upper(v_rt_all_cols(i).event_type));
        if (upper(v_rt_all_cols(i).event_type) = upper('System_enforced')) then
                  vstep_no  := '4.1';
                  vtable_nm := 'before seq sel';
              select PC_SEQ.nextval into pc_id from dual;
                  vstep_no  := '4.2';
                  vtable_nm := 'before insert corres';
              INSERT INTO target1_tab
                           (ID,
                            PARTY_ID,
                            PRODUCT_BRAND,
                            SORT_CODE,
                            ACCOUNT_NUMBER,
                            EXT_PRD_ID_TX,         
                            EXT_PRD_HLD_ID_TX,
                            EXT_SYS_ID,
                            EXT_PTY_ID_TX,
                            ACCOUNT_TYPE_CD,
                            COM_PFR_TYP_TX,
                            COM_PFR_OPT_TX,
                            COM_PFR_RSN_CD,
                            status)
             VALUES
                            (pc_id,
                             v_rt_all_cols(i).party_id,
                             decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                             v_rt_all_cols(i).sort_code,
                             'XXXX'||substr(trim(v_rt_all_cols(i).ACCOUNT_NUMBER),length(trim(v_rt_all_cols(i).ACCOUNT_NUMBER))-3,4),
                             v_rt_all_cols(i).EXT_PRD_ID_TX,
                             v_rt_all_cols(i).EXT_PRD_HLD_ID_TX,
                             v_rt_all_cols(i).EXT_SYS_ID,
                             v_rt_all_cols(i).EXT_PTY_ID_TX,
                             v_rt_all_cols(i).ACCOUNT_TYPE_CD,
                             v_rt_all_cols(i).COM_PFR_TYP_TX,
                             v_rt_all_cols(i).COM_PFR_OPT_TX,
                             v_rt_all_cols(i).COM_PFR_RSN_CD,
                             NULL);
                  vstep_no  := '4.3';
                  vtable_nm := 'after insert corres';
        else
              select COM_SEQ.nextval into id_val from dual;
                  vstep_no  := '6';
                  vtable_nm := 'before insertcomm';
          if (upper(v_rt_all_cols(i).event_type) = upper('REMINDER')) then
                vstep_no  := '6.01';
                  vtable_nm := 'after if insertcomm';
              insert into parent_tab
                 (ID ,
                 CTEM_CODE,
                 CHA_CODE,            
                 CT_CODE,                           
                 CONTACT_POINT_ID,             
                 SOURCE,
                 RECEIVED_DATE,                             
                 SEND_DATE,
                 RETRY_COUNT)
              values
                 (id_val,
                  lower(v_rt_all_cols(i).event_type), 
                  decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                  'Email',
                  v_rt_all_cols(i).email_id,
                  'IADAREMINDER',
                  systimestamp,
                  systimestamp,
                  0);  
         else
                vstep_no  := '6.02';
                  vtable_nm := 'after else insertcomm';
              insert into parent_tab
                 (ID ,
                 CTEM_CODE,
                 CHA_CODE,            
                 CT_CODE,                           
                 CONTACT_POINT_ID,             
                 SOURCE,
                 RECEIVED_DATE,                             
                 SEND_DATE,
                 RETRY_COUNT)
              values
                 (id_val,
                  lower(v_rt_all_cols(i).event_type), 
                  decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                  'Email',
                  v_rt_all_cols(i).email_id,
                  'CORRESPONDENCE',
                  systimestamp,
                  systimestamp,
                  0); 
            END if; 
                  vstep_no  := '6.11';
                  vtable_nm := 'before chop';
             if (v_rt_all_cols(i).ACCOUNT_NUMBER is not null) then 
                      v_rt_all_cols(i).ACCOUNT_NUMBER := 'XXXX'||substr(trim(v_rt_all_cols(i).ACCOUNT_NUMBER),length(trim(v_rt_all_cols(i).ACCOUNT_NUMBER))-3,4);
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 'IB.Correspondence.AccountNumberMasked',
                 v_rt_all_cols(i).ACCOUNT_NUMBER);
             end if;
                  vstep_no  := '6.1';
                  vtable_nm := 'before stateday';
             if (v_rt_all_cols(i).crr_day is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Day',
                 'IB.Crsp.Date.Day',
                 v_rt_all_cols(i).crr_day);
             end if;
                  vstep_no  := '6.2';
                  vtable_nm := 'before statemth';
             if (v_rt_all_cols(i).crr_month is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Month',
                 'IB.Crsp.Date.Month',
                 v_rt_all_cols(i).crr_month);
             end if;
                  vstep_no  := '6.3';
                  vtable_nm := 'before stateyear';
             if (v_rt_all_cols(i).crr_year is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Year',
                 'IB.Crsp.Date.Year',
                 v_rt_all_cols(i).crr_year);
             end if;
                  vstep_no  := '7';
                  vtable_nm := 'before type';
               if (v_rt_all_cols(i).product_type is not null) then
                  insert into child_tab
                     (COM_ID,                                            
                     KEY,                                                                                                                                        
                     VALUE)
                  values
                    (id_val,
                     'IB.Product.ProductName',
                   v_rt_all_cols(i).product_type);
                end if;
                  vstep_no  := '9';
                  vtable_nm := 'before title';         
              if (trim(v_rt_all_cols(i).title) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE )
              values
                (id_val,
                 'IB.Customer.Title',
                 trim(v_rt_all_cols(i).title));
              end if;
                  vstep_no  := '10';
                  vtable_nm := 'before surname';
              if (v_rt_all_cols(i).surname is not null) then
                insert into child_tab
                   (COM_ID,                                            
                   KEY,                                                                                                                                          
                   VALUE)
                values
                  (id_val,
                  'IB.Customer.LastName',
                  v_rt_all_cols(i).surname);
              end if;
                            vstep_no  := '12';
                            vtable_nm := 'before postcd';
              if (trim(v_rt_all_cols(i).POSTCODE) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Customer.Addr.PostCodeMasked',
                  substr(replace(v_rt_all_cols(i).POSTCODE,' ',''),length(replace(v_rt_all_cols(i).POSTCODE,' ',''))-2,3));
              end if;
                            vstep_no  := '13';
                            vtable_nm := 'before subject';
              if (trim(v_rt_all_cols(i).SUBJECT) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Correspondence.Subject',
                  v_rt_all_cols(i).subject);
              end if;
                            vstep_no  := '14';
                            vtable_nm := 'before inactivity';
              if (trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) is null or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '3' or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '6' or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '9') then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Correspondence.Inactivity',
                  v_rt_all_cols(i).UNREAD_CORRES_PERIOD);
              end if;
                          vstep_no  := '14.1';
                          vtable_nm := 'after notfound';
        end if;
                          vstep_no  := '15';
                          vtable_nm := 'after notfound';
        END LOOP;
        end loop;
                          vstep_no  := '16';
                          vtable_nm := 'before closecur';
        CLOSE stg_cursor;
                          vstep_no  := '17';
                          vtable_nm := 'before commit';
        DELETE FROM table_stg;
      COMMIT;
                          vstep_no  := '18';
                          vtable_nm := 'after commit';
    EXCEPTION
    WHEN OTHERS THEN
      ROLLBACK;
      success_flag := 1;
      vsql_code := SQLCODE;
      vsql_errm := SUBSTR(sqlerrm,1,200);
      error_logging_pkg.inserterrorlog('samp',vsql_code,vsql_errm, vtable_nm,vstep_no);
      RAISE_APPLICATION_ERROR (-20011, 'samp '||vstep_no||' SQLERRM:'||SQLERRM);
    end;
    Thanks

    Its bit urgent
    NO - it is NOT urgent. Not to us.
    If you have an urgent problem you need to hire a consultant.
    I have a performance issue in the below code,
    Maybe you do and maybe you don't. How are we to really know? You haven't posted ANYTHING indicating that a performance issue exists. Please read the FAQ for how to post a tuning request and the info you need to provide. First and foremost you have to post SOMETHING that actually shows that a performance issue exists. Troubleshooting requires FACTS not just a subjective opinion.
    where i am trying to insert the data from table_stg into target_tab and in parent_tab tables and then to child tables via cursor with bulk collect .the target_tab and parent_tab are huge tables and have a row wise trigger enabled on it .the trigger is mandatory . This timetaken for this block to execute is 5000 seconds.Now my requirement is to reduce it to 5 to 10 mins.
    Personally I think 5000 seconds (about 1 hr 20 minutes) is very fast for processing 800 trillion rows of data into parent and child tables. Why do you think that is slow?
    Your code has several major flaws that need to be corrected before you can even determine what, if anything, needs to be tuned.
    This code has the EXIT statement at the beginning of the loop instead of at the end
        FETCH stg_cursor BULK COLLECT INTO v_rt_all_cols LIMIT limit_in;
                  vstep_no  := '4';
                  vtable_nm := 'after fetch';
    --EXIT WHEN v_rt_all_cols.COUNT = 0;
        EXIT WHEN stg_cursor%NOTFOUND;
    The correct place for the %NOTFOUND test when using BULK COLLECT is at the END of the loop; that is, the last statement in the loop.
    You can use a COUNT test at the start of the loop but ironically you have commented it out and have now done it wrong. Either move the NOTFOUND test to the end of the loop or remove it and uncomment the COUNT test.
    WHEN OTHERS THEN
      ROLLBACK;
    That basically says you don't even care what problem occurs or whether the problem is for a single record of your 10,000 in the collection. You pretty much just throw away any stack trace and substitute your own message.
    Your code also has NO exception handling for any of the individual steps or blocks of code.
    The code you posted also begs the question of why you are using NAME=VALUE pairs for child data rows? Why aren't you using a standard relational table for this data?
    As others have noted you are using slow-by-slow (row by row processing). Let's assume that PL/SQL, the bulk collect and row-by-row is actually necessary.
    Then you should be constructing the parent and child records into collections and then inserting them in BULK using FORALL.
    1. Create a collection for the new parent rows
    2. Create a collection for the new child rows
    3. For each set of LIMIT source row data
      a. empty the parent and child collections
      b. populate those collections with new parent/child data
      c. bulk insert the parent collection into the parent table
      d. bulk insert the child collection into the child table
    And unless you really want to either load EVERYTHING or abandon everything you should use bulk exception handling so that the clean data gets processed and only the dirty data gets rejected.

Maybe you are looking for

  • Problem with PDF printing

    Hi All, I have recently applied the 63 security notes (pending from one year) recommended by SAP in report RSECNOTE. After applying the note I am facing the problem of printing PDF documents. When I am trying to do print preview, its showing message

  • Why MENU is missing from my form?

    Hello, I am developing an VISITOR Adobe Interactive Form, i have Designer 8.1 and Reader 9.0 in my Windows 7 Laptop. When i checked the menu EDIT-->form properties --> Compatability, its Adobe XML form File (XDP) When i opened this VISITOR interactiv

  • How to make sure the thread is completed?

    Hi there, Say I have a thread (A) and it's running. is there anyway I can hold all other tasks/threads until the thread (A) is competed? My GUI hasa button and once clicked some events happens. I would like to restrict the user from clicking the butt

  • Supersession

    Hi Gurus I am trying to implement Parts supersession in SD and one of the requirement is that the configuation for the Sales Order should show all the sub item . In Standard SAP this is set to shown only main item and you have to Va02- Edit- Display

  • Error in running adstats.sql after applying patch 6810189 on 10.2.0.2

    following error faced when run the script adstats.sql through sys user after starting the database with restrict option: SQL> @/backup/adstats.sql; Connected. --- adstats.sql started at 2010-09-13 21:44:26 --- Checking for the DB version and collecti