Wat's Plsql collections?

Can anyone help me about Plsql collections and why we need this?

Welcome to the forum.
Your question is quite broad.
A quick search through the Oracle docs will give you explanations and examples
http://www.oracle.com/pls/db112/search?remark=quick_search&word=collections
http://www.oracle.com/pls/db112/homepage
As well as a search on AskTom, for example:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:3170352229012
As well as a search on this forum.
why we need this?It depends. Perhaps you don't need them, perhaps you do.
Amongst others on your specific requirement and your datamodel.

Similar Messages

  • How to fetch from cursor into plsql collection

    Dear Friends,
    I am trying to understand PLSQL collections. I am trying with the following example.
    CREATE OR REPLACE TYPE emp_obj AS OBJECT
    (     empname          VARCHAR2(100),     empjob          VARCHAR2(50),     empsal          NUMBER);
    CREATE OR REPLACE TYPE emp_tbl IS TABLE OF emp_obj;
    CREATE OR REPLACE PACKAGE eg_collection AS
    -- Delcare ref cursor
    TYPE rc IS REF CURSOR;
    -- Procedure
    PROCEDURE eg_collection_proc (out_result OUT rc);
    END;
    CREATE OR REPLACE PACKAGE BODY eg_collection AS
    PROCEDURE eg_collection_proc( out_result OUT rc) AS
    emp_tdt     emp_tbl := emp_tbl(emp_obj('oracle','DBA',100));
    CURSOR c2 IS SELECT ename,job,sal FROM emp WHERE sal > 2000;
    -- Declare a record type to hold the records from cursor and then pass to the collection
    emp_rec emp_obj;
    BEGIN
         OPEN c2;
         LOOP FETCH c1 INTO emp_rec;
              EXIT WHEN c1%NOTFOUND;
              emp_tdt.extend;
    emp_tdt(emp_tdt.count) := emp_rec;
         END LOOP;
         CLOSE c2;
    OPEN out_result FOR SELECT * FROM TABLE(CAST(emp_tdt AS emp_tbl));
    END eg_collection_proc;
    END eg_collection;
    Executing the proc
    variable r refcursor;
    exec eg_collection.eg_collection_proc(:r);
    print r;
    But I am getting compilation error type mismatch found at emp_rec between fetch cursor into variable

    I am trying to understand PLSQL collections. I dont why the code is not working
    SQL> CREATE OR REPLACE TYPE emp_obj AS OBJECT
    2 (
    3      empname          VARCHAR2(100),
    4      empjob          VARCHAR2(50),
    5      empsal          NUMBER
    6 )
    7 /
    Type created.
    SQL> CREATE OR REPLACE TYPE emp_tbl IS TABLE OF emp_obj
    2 /
    Type created.
    SQL> DECLARE
    2      emp_tdt emp_tbl := emp_tbl ();
    3 BEGIN
    4
    5      emp_tdt.extend;
    6      SELECT emp_obj(ename, job, sal) BULK COLLECT INTO emp_tdt
    7      FROM emp WHERE sal < 4000;
    8
    9      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    10
    11      emp_tdt.extend;
    12      SELECT ename, job, sal INTO emp_tdt(1).empname, emp_tdt(1).empjob, emp_tdt(1).empsal
    13      FROM emp WHERE empno = 7900;
    14
    15      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    16
    17 END;
    18 /
    The total count is 13
    The total count is 14
    PL/SQL procedure successfully completed.
    SQL> DECLARE
    2      emp_tdt emp_tbl := emp_tbl ();
    3 BEGIN
    4
    5      emp_tdt.extend;
    6      SELECT ename, job, sal INTO emp_tdt(1).empname, emp_tdt(1).empjob, emp_tdt(1).empsal
    7      FROM emp WHERE empno = 7900;
    8
    9      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    10
    11      emp_tdt.extend;
    12      SELECT emp_obj(ename, job, sal) BULK COLLECT INTO emp_tdt
    13      FROM emp WHERE sal < 4000;
    14
    15      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    16 END;
    17 /
    DECLARE
    ERROR at line 1:
    ORA-06530: Reference to uninitialized composite
    ORA-06512: at line 6

  • PLSQL COLLECTIONS(10g)

    Hi People,
    I have a clarification in plsql collections.it is defined that plsql collections are used to collect values of same data type.but in 10g document it is told that you can also use INDEX BY tables along with %ROWTYPE.how this is possible?.%ROWTYPE is used to draw different data types.how to use this along with INDEX BY tables. Pls help me with some sample codes.
    with regards
    vids

    vidusnat wrote:
    In the above example plsql table is used with %ROWTYPE It is not a PL/SQL "+table+". There is no such thing. No such concept. It is INCORRECT terminology used by many, including Oracle. (Oracle has however corrected this terminology in recent documentation).
    That type defines an associative array.
    which is used to collect values of different data types.Correct. The +%RowType+ clause saves us time and effort in redefining a PL/SQL record structure that duplicates the structure (SQL projection) of a table, view or cursor.
    So in effect, instead of having to code this, we use +%RowType+ instead.
    declare
      type TEmployeeRecord is record(
        emp_id number,
        surname varchar2(80),
       .. etc ..
      type EmpTabTyp is table of TEmployeeRecord
        index by pls_integer;To manually define a structure like this each time for a table/view/cursor is lots of coding. Easy to get wrong. And if the underlying SQL projection changes, then we need to manually update the structure.
    So +%RowType+ saves us all the extra coding and maintenance of defining a PL/SQL struct that duplicates a SQL projection structure. If you know C/C++, think of it as a compiler macro that defines the relevant struct for you.
    but plsql collections are defined to collect values of same data type.You have not define a collection. A collection is a different type of structure and has different features. You have defined an associative array.
    using %ROWTYPE is contrary to plsql collection's definition.can anyone pls explain me how it is possible?There is no contradiction here. Just confusion it seems?
    A collection is not an associative array. An associative array is not a collection. Both can be based on the same structure. Both can be used to received SQL cursor output. However, there are differences in features - especially how one addresses the contents. In essence, an associative array is addressed via name. Each cell in the array is named - or in the case of the name being a numeric, a number value. The 1st cell in the associative array can be named (or numbered in your case) using "100" - as you did in your sample code. So to address the contents of the 1st cell in that array, you need to reference it by index value 100. Not 1.
    A collection is not addressed via a name of the cell. Instead, it is addressed by location/position of the cell. So the 1st cell will be addressed by 1, the 2nd cell by 2 and so on.

  • Plsql Collections help

    Database table has two columns category and products.
    Table: catalog
    category products
    fruit apple
    vegetable carrot
    soda pepsi
    vegetable potato
    fruit grapes
    fruit orange
    vegetable cabbage
    soda coke
    i need to read the records from table catalog and store them in some sort of plsql collection. I then have to read from that collection and write output as follows:
    Note: the categories and products should be sorted alphabetically in output.
    OUTPUT
    fruit apple, grapes, orange
    soda coke, pepsi
    vegetable cabbage, carrot, potato
    please help me as how this can be done?
    Thanks
    G

    Without collections:
    SQL> DECLARE
      2      CURSOR c
      3        IS
      4          WITH t AS (
      5                     SELECT 'fruit' category,'apple' product FROM dual UNION ALL
      6                     SELECT 'vegetable','carrot' FROM dual UNION ALL
      7                     SELECT 'soda','pepsi' FROM dual UNION ALL
      8                     SELECT 'vegetable','potato' FROM dual UNION ALL
      9                     SELECT 'fruit','grapes' FROM dual UNION ALL
    10                     SELECT 'fruit','orange' FROM dual UNION ALL
    11                     SELECT 'vegetable','cabbage' FROM dual UNION ALL
    12                     SELECT 'soda','coke' FROM dual
    13                    )
    14          SELECT  category,
    15                  ltrim(sys_connect_by_path(product,', '),', ') product_list
    16            FROM  (
    17                   SELECT  category,
    18                           product,
    19                           row_number() over(partition by category order by product) rn
    20                     FROM  t
    21                  )
    22            WHERE connect_by_isleaf = 1
    23            START WITH rn = 1
    24            CONNECT BY category = PRIOR category
    25                   AND rn = PRIOR rn + 1
    26            ORDER BY category;
    27  BEGIN
    28      FOR v_rec IN c LOOP
    29        DBMS_OUTPUT.PUT_LINE(rpad(v_rec.category,10) || v_rec.product_list);
    30      END LOOP;
    31  END;
    32  /
    fruit     apple, grapes, orange
    soda      coke, pepsi
    vegetable cabbage, carrot, potato
    PL/SQL procedure successfully completed.
    SQL> And in plain SQL:
    SQL> COLUMN PRODUCT_LIST FORMAT A50
    SQL> WITH t AS (
      2             SELECT 'fruit' category,'apple' product FROM dual UNION ALL
      3             SELECT 'vegetable','carrot' FROM dual UNION ALL
      4             SELECT 'soda','pepsi' FROM dual UNION ALL
      5             SELECT 'vegetable','potato' FROM dual UNION ALL
      6             SELECT 'fruit','grapes' FROM dual UNION ALL
      7             SELECT 'fruit','orange' FROM dual UNION ALL
      8             SELECT 'vegetable','cabbage' FROM dual UNION ALL
      9             SELECT 'soda','coke' FROM dual
    10            )
    11  SELECT  category,
    12          ltrim(sys_connect_by_path(product,', '),', ') product_list
    13    FROM  (
    14           SELECT  category,
    15                   product,
    16                   row_number() over(partition by category order by product) rn
    17             FROM  t
    18          )
    19    WHERE connect_by_isleaf = 1
    20    START WITH rn = 1
    21    CONNECT BY category = PRIOR category
    22           AND rn = PRIOR rn + 1
    23    ORDER BY category
    24  /
    CATEGORY  PRODUCT_LIST
    fruit     apple, grapes, orange
    soda      coke, pepsi
    vegetable cabbage, carrot, potato
    SQL> SY.

  • Select data from plsql collections

    Hi All,
    I am not a developer but working as a DBA, so not very much familiar with pl/sql, still gone through with documentation and could come up with some solution of my problem. I need some expert advice here.
    Problem : I am writing down some kind of plsql program for monitoring of some special batch job, I know we have lot of other option to do the same including db/grid control ..etc but for some
    reason i have to do this using plsql only.
    Requirement : my requirement is to select data from table in plsql and then should have ability to query it again and again. I would not prefer to go to table rather than directly from plsql..
    I wrote down below code for sample, bulk collect data into collection type and can print using for loop.
    Declare
    type ts is table of v$session%rowtype index by pls_integer;
    tsess ts;
    begin
    select * bulk collect into tsess from v$session ;
    for i in 1..tsess.count loop
    dbms_output.put_line(tsess(i).terminal);
    end loop;
    end;
    But, is there any way same collection ( tsess in above example ) can be queried using select statement like 'select * from table ( Tsess ) ' I have searched on net and found this can be done using creating type at database level. But my problem is I can not create any object in database as being it is a production one.
    I was looking for if is there any way same can be accomplished ... like cast / multiset .. however, I could not get it through.
    your help would be appreciated !!
    Regards,

    I don't think you need to use arrays here, only SQL, have a look at subquery factors and report back if that is insufficient...
    Edited by: BrendanP on 12-Feb-2012 03:07 to add an example:
    I gather that you want to 'requery' data that you have already got from the database purely to be able to use SQL functionality such as ORDER BY in multiple ways. Here is how you can do this in the original SQL for one particular example, where you want to query v$sql ordering by CPU time and by disk reads separately (I tested this but the output won't look good here, so omitting it):
    WITH v AS (
    SELECT
        Substr (sql_text,1,500)             sql_text,
        cpu_time/1000000                    cpu_seconds,
        disk_reads,
        buffer_gets,
        executions,
        CASE WHEN rows_processed != 0 THEN Round( buffer_gets / Nvl (Replace (rows_processed, 0, 1) ,1)) END Buffer_gets_rows_proc,
        Round (buffer_gets / Nvl (Replace (executions, 0, 1), 1)) Buffer_gets_executions,
        elapsed_time / 1000000              elapsed_second,
        module
    FROM v$sql s)
    SELECT
        'CPU'                order_by,
        cpu_seconds          order_val,
        sql_text,
        cpu_seconds,
        disk_reads,
        buffer_gets,
        executions,
        buffer_gets_rows_proc,
        buffer_gets_executions,
        elapsed_second,
        module
    FROM v
    UNION
    SELECT
        'Disk reads',
        disk_reads,
        sql_text,
        cpu_seconds,
        disk_reads,
        buffer_gets,
        executions,
        buffer_gets_rows_proc,
        buffer_gets_executions,
        elapsed_second,
        module
    FROM v
    ORDER BY order_by, order_val DESC

  • Plsql collections

    Hi,
    I am working with oracle 10g.
    Still i was not familiar with PLSQL Tables or collections.
    Please send me the link or any notes regarding PLSQL Tables.
    Thanks and Regards,
    Ansaf.

    Ansaf wrote:
    Hi,
    I am working with oracle 10g.
    Still i was not familiar with PLSQL Tables or collections.
    Please send me the link or any notes regarding PLSQL Tables.
    Thanks and Regards,
    Ansaf.when all else fail Read The Fine Manual
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/collections.htm#sthref1031

  • PLSQL Collections and record types

    Hi,
    I'm trying to use collection in my code in 10g database
    declare
    cursor c_cur
    is
    select  * from table;
    TYPE tbl_cur_lines IS TABLE OF c_cur%ROWTYPE
          INDEX BY BINARY_INTEGER;
          l_cur_rec    tbl_cur_lines;
    begin
    OPEN c_cur;
    FETCH c_cur BULK COLLECT INTO l_cur_rec;
    CLOSE c_open_trx;                       
    FOR i IN 0..l_cur_rec.COUNT-1
    LOOP
              --  do something
    END LOOP;
    end;if i use count for number of records its working
    declare
    cursor c_cur
    is
    select  * from table;
    TYPE tbl_cur_lines IS TABLE OF c_cur%ROWTYPE
          INDEX BY BINARY_INTEGER;
          l_cur_rec    tbl_cur_lines;
    begin
    OPEN c_cur;
    FETCH c_cur BULK COLLECT INTO l_cur_rec;
    CLOSE c_open_trx;                       
    FOR i IN l_cur_rec.FIRST..l_cur_rec.LAST
    LOOP
              --  do something
    END LOOP;
    end;If i use above code i'm getting following error
    ORA-06502: PL/SQL: numeric or value error
    Can some explain difference...
    Thanks in advance!
    Edited by: user641008 on Jun 3, 2011 2:31 PM

    FOR i IN 0..l_cur_rec.COUNT-1
    here, index "i" expect an integer value and it is getting and integer, hence not
    giving an error, though your cursor did not fetch any row.
    in the next case,
    FOR i IN l_cur_rec.FIRST..l_cur_rec.LAST
    your cursor did not fetch any row and hence
    first element in your collection is NULL( in other words, there is no index value in collection) while "i" is expecting an integer,
    thats why you are getting error "ORA-06502: PL/SQL: numeric or value error"
    It is always good practice, check collection count before actually processing by loop.
    Change as follows
    declare
    cursor c_cur
    is
    select  * from table;
    TYPE tbl_cur_lines IS TABLE OF c_cur%ROWTYPE
          INDEX BY BINARY_INTEGER;
          l_cur_rec    tbl_cur_lines;
    begin
    OPEN c_cur;
    FETCH c_cur BULK COLLECT INTO l_cur_rec;
    CLOSE c_cur;                       
    if  l_cur_rec.count>0 then      -- Add condition here
    FOR i IN l_cur_rec.FIRST..l_cur_rec.LAST
    LOOP
              --  do something
    END LOOP;
    end if;
    end;

  • BULK COLLECT in select query inside a function

    Hi All,
    My query is :
    SELECT col1,col2,col3 FROM table_a; --( consider this is a long running query with lot of joins)
    Need to know how can i get the output of the above query from a function using BULK COLLECT.
    and i tried this:
    CREATE OR REPLACE TYPE tab_a_row
    AS OBJECT (
    col1 number(20),
    col2 number(20),
    col2 number(20)) ;
    create or replace type tab_a_nt as table of tab_a_row;
    create or replace function get_table_a
    return sys_refcursor
    is
    tab_a_recs tab_a_nt;
    rv sys_refcursor;
    begin
    SELECT tab_a_row(col1,col2,col3) BULK COLLECT INTO tab_a_recs FROM table_a;
    open rv for select * from table(tab_a_recs);
    return rv;
    end;
    Function created successfully. and i exec this from sql plus using
    SQL> var rc refcursor;
    SQL> exec :rc := get_table_a;
    BEGIN :rc := get_table_a; END;
    ERROR at line 1:
    ORA-22905: cannot access rows from a non-nested table item
    ORA-06512: at "GET_TABLE_A", line 12
    ORA-06512: at line 1
    Kindly share your ideas on how to use bulk collect and get set of outputs from a function.
    Edited by: 887268 on Apr 18, 2013 3:10 AM

    >
    If i use refcursor , then the JAVA code needs to be changed accordinglyto get the refcursor output.
    >
    Well, of course. Java has to know what the sql projection is. How else will it know how many columns there are and their datatypes.
    But that is true no matter what method you use.
    >
    But if i use a PLSQL COLLECTION TYPE (nested tables ) , then i can get output as ,
    select * from table(function_name(input1,input2));
    >
    No - using the 'table' function mean you are calling a PIPELINED function.
    This is a sample of a PIPELINED procedure.
    -- type to match emp record
    create or replace type emp_scalar_type as object
      (EMPNO NUMBER(4) ,
       ENAME VARCHAR2(10),
       JOB VARCHAR2(9),
       MGR NUMBER(4),
       HIREDATE DATE,
       SAL NUMBER(7, 2),
       COMM NUMBER(7, 2),
       DEPTNO NUMBER(2)
    -- table of emp records
    create or replace type emp_table_type as table of emp_scalar_type
    -- pipelined function
    create or replace function get_emp( p_deptno in number )
      return emp_table_type
      PIPELINED
      as
       TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;
        emp_cv EmpCurTyp;
        l_rec  emp%rowtype;
      begin
        open emp_cv for select * from emp where deptno = p_deptno;
        loop
          fetch emp_cv into l_rec;
          exit when (emp_cv%notfound);
          pipe row( emp_scalar_type( l_rec.empno, LOWER(l_rec.ename),
              l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno ) );
        end loop;
        return;
      end;
    select * from table(get_emp(20))Or your function could return a collection like the example from this thread this morning.
    Example of Collection as datatype of a function’s return value
    CREATE OR REPLACE TYPE enamelist as VARRAY(20) of VARCHAR2(20)
    /* Formatted on 4/18/2013 4:06:47 PM (QP5 v5.126.903.23003) */
    CREATE OR REPLACE FUNCTION ename_fn
    RETURN enamelist
    AS
    v_cursor_main enamelist := enamelist ();
    BEGIN
    SELECT ename
    BULK COLLECT
    INTO v_cursor_main
    FROM emp;
    RETURN v_cursor_main;
    EXCEPTION
    WHEN OTHERS
    THEN
    RETURN v_cursor_main;
    END;
    select * from table(ename_fn()) from dual;
    COLUMN_VALUE
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER

  • 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;

  • How to declare and use multi dimensional VARRAYs in PLSQL

    Hi All,
    I am trying to create and use multidimensional varray in plsql code...
    can anyone let me know, how can I do this...
    Thanks
    Krishna

    Please do not confuse plsql collections with type objects.Well it's possible with persistent types but nested tables and varrays typically need to be explicitly initialized and extended.
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    SQL> CREATE OR REPLACE TYPE name_type_size IS VARRAY (3) OF VARCHAR2 (50);
      2  /
    Type created.
    SQL> CREATE OR REPLACE TYPE name_type_size_array IS VARRAY (200) OF name_type_size;
      2  /
    Type created.
    SQL> DECLARE
      2     i_name_type_size_array name_type_size_array :=  name_type_size_array ();
      3  BEGIN
      4     FOR count1 IN 1 .. 200 LOOP
      5
      6        i_name_type_size_array.EXTEND;
      7        i_name_type_size_array (count1) := name_type_size ();
      8
      9        FOR count2 IN 1 .. 3 LOOP
    10
    11           i_name_type_size_array (count1).EXTEND;
    12           i_name_type_size_array (count1) (count2) := count2;
    13
    14        END LOOP;
    15
    16     END LOOP;
    17  END;
    18  /
    PL/SQL procedure successfully completed.
    SQL>

  • Converting from JAVA HASHMAP to PLSQL object or CLOB

    Hello All,
    My oracle version in 10.2.0.4 on a solaris V880 machine.
    Our Application Java SDK is 1.4.
    I am trying to pass in a series of XML (considerably huge for XML type column) from java application plsql which stores them into an oracle table with column as external BLOB. Since the contents are so heavy for each objects , using XMLTYPE column is not feasible.
    The xml files comes as either java hash_map or string which i have to pass as parameter to an Oracle stored procedure which stores as external BLOB in OS through a table.
    My first doubt is this the correct approach.
    My second doubt here is how to interpret java hash_map object within PLSQL when it is passed to it.
    Apologies if the question is silly since i am a new comer to java.
    Thanks in advance ,
    Vijay G

    vijay S P G wrote:
    If i have plsql collection with a varchar2 and a blob as record type then can i directly pass the java hash map to the procedure having parameter datatype as the above said record type ?Of course not. Obviously the first reason for that is because the "blob" needs to be a file path and not a blob.
    Second reason is no databases that I know of take a hash data type. Some take arrays. But I doubt that is worth the trouble.
    You have a collection of name value (path) pairs. Iterate over the collection and for each name do an insert of the name and path.

  • How to implement a linked list in oracle ?

    HI All
    I want to know if there a way to implement a linked list in oracle ?
    Thanks in Advanced
    Naama

    A linked list is an array of the relevant data plus an indicator of the next and previous entries, right?
    Sounds easily achievable with any of the plsql collection types.
    The simplest would be a plsql associative array of a record
    Assignments of records in collections is always a bit hamfisted compared to sql objects types though (IMO).
    Something like ?
    DECLARE
    TYPE r_payload IS RECORD
    (col1 number,
      col2 number);
    TYPE r_array_entry is RECORD
    (prev_entry PLS_INTEGER,
      next_entry PLS_INTEGER,
      payload    r_payload);
    TYPE t_array IS TABLE OF r_array_entry INDEX BY PLS_INTEGER;
    v_array t_array;
    BEGIN
    NULL;
    END; 
      The use case for such a structure in properly coded SQL & PL/SQL is possibly the harder question.

  • Using dbms_xmlgen with a CAST statement

    The following code populates a plsql collection and then attempts to select from it using dbms_xmlgen.
    the following error results --
    DECLARE
    ERROR at line 1:
    ORA-19206: Invalid value for query or REF CURSOR parameter
    ORA-06512: at "SYS.DBMS_XMLGEN", line 83
    ORA-06512: at line 56
    The query is correct as the commented out select at end of block works fine.
    Can dbms_xmlgen be used in such a way and if not is there a workaround.
    Thanks
    DECLARE
    types declared on the database
    create or replace type bus_tab_obj AS object
    ( BUSINESS_MODEL_ID NUMBER(10)
    ,BUSINESS_MODEL_VER_NO NUMBER(5)
    ,BUSINESS_MODEL_NAME VARCHAR2(30));
    create or replace TYPE bus_table AS TABLE of bus_tab_obj;
    package level variable bus_table_recs in package get_values_pkg
    myxml sys.xmltype;
    queryCtx DBMS_XMLGen.ctxhandle;
    BEGIN
    -- initialise and delete
    get_values_pkg.bus_table_recs := bus_table(NULL,NULL,NULL);
    get_values_pkg.bus_table_recs.DELETE;
    -- select some records to put in the collection
    for x in ( select BUSINESS_MODEL_ID
    ,BUSINESS_MODEL_VER_NO
    ,BUSINESS_MODEL_NAME
    from BRK_BUSINESS_MODEL )
    LOOP
    get_values_pkg.bus_table_recs.extend;
    get_values_pkg.bus_table_recs := bus_table(bus_tab_obj(x.business_model_id
    ,x.business_model_ver_no
    ,x.business_model_name));
    END LOOP;
    -- retrieve the records from the collection as xml
    queryCtx := DBMS_XMLGen.newContext('select a.business_model_name data from THE ( select cast( get_values_pkg.bus_table_recs as bus_table ) from dual ) a order by data');
    dbms_xmlgen.setMaxRows(queryCtx,1);
    myxml := DBMS_XMLGen.getXMLType(queryCtx);
    dbms_output.put_line(myxml.extract('/*').getStringVal());
    this works!
    for x in ( select a.business_model_name data
    from THE ( select cast( get_values_pkg.bus_table_recs as bus_table ) from dual ) a
    order by data )
    loop
    dbms_output.put_line( x.data );
    end loop;
    END;
    /

    891794 wrote:
    Thank you all , see I am new to this database field , I am not a pro.Not a problem. However, one of the very first things you should learn is that dynamic SQL is seldom needed and invariably just plain wrong.
    If a table name is dynamic, or a column name is dynamic - then chances are excellent that your data model is already fubar'ed (or malema'ed to put it in a za.org perspective).
    And even when you are in the very exceptional situation where dynamic SQL is needed, SQL injection is a MAJOR security concern.. and almost always ignored by implementing shoddy code to create and execute the dynamic SQL.

  • How to implement Fetch First (SQL Server) in PL/SQL

    I am in process of converting SQL 2000 database to Oracle 9i. OMWB has commented "Fetch First" as
    /*[SPCONV-ERR(60)] FETCH FIRST is not supported in Oracle. */.
    Is there any work around for FETCH FIRST ( SQL 2000) in PL/SQL?

    Fetch First returns the first row of a query.
    The options in Oracle are either to close and reopen the cursor, or download (cache) the cursor fetches into, for example, a PLSQL structure sych as an PLSQL collections (Index-by tables, Nested tables or Varrays), to be accessed later in any order that is required.
    See PL/SQL User's Guide and Reference Release 2 (9.2) Part Number A96624-01, for further details.
    Regards,
    Turloch
    Oracle Migration Workbench Team

  • How to improve performance using bulk collects with plsql tables or arrays

    Hi All,
    my procedure is like this
    declare
    cursor c1 is select ----------------------
    begin
    assigning to variables
    validations on that variables
    --50 validations are here --
    insert into a table
    end;
    we have created indexes on primary keys,
    i want to use
    DECLARE
    CURSOR a_cur IS
    SELECT program_id
    FROM airplanes;
    TYPE myarray IS TABLE OF a_cur%ROWTYPE;
    cur_array myarray;
    BEGIN
    OPEN a_cur;
    LOOP
    FETCH a_cur BULK COLLECT INTO cur_array LIMIT 100;
    ***---------can i assign cursor data to the plsql table variables or array***
    ***validate on the pl sql variable as---***
    i
    nsert into a table
    EXIT WHEN a_cur%NOTFOUND;
    END LOOP;
    CLOSE a_cur;
    END;
    Edited by: Veekay on Oct 21, 2011 4:28 AM

    Fastest way often is this:
    insert /*+append */
    into aTable
    select * from airplanes;
    commit;The select and insert part can even be done in parallel if needed.
    However if the oparation is complex or the dataset is very very very very very large or the programmer is decent but not excellent then the bulk approach should be considered. It is often a pretty stable and linear scaling approach.
    The solution depends a little on the database version.
    LOOP
      FETCH a_cur BULK COLLECT INTO cur_array LIMIT 100;
      EXIT WHEN a_cur.count = 0;
      forall i in a_cur.first.. a_cur.last
      insert into aTable (id)
      values (a_cur(i));
    END LOOP;
    ...If you have more then one column then you might need a single collection for each column. Other possibilities depend on the db version.
    Also: do not exit using a_cur%NOTFOUND. This is wrong! You might loose records from the end of the data set.

Maybe you are looking for

  • Mail and Safari will not open after 10.4.6 update(s)

    Client's Ibook was running Jaguar 10.2.8, and Mail was locking up as it was overstuffed. Upgraded to 10.4.3 to fix the problem, and it did, in fact, fix it. Was then prompted to install all available updates, which I thought I would do as a courtesy

  • Hard reset is the only way to turn it on, stuck on repair loop. Tried everything. Tips?

    I'm going to be as descriptive as possible, in a hope to actually pinpoint an issue. Not too long ago, my "Lock" function starting acting wonky. It'd lock itself while "Unlocked", and vise versa. This happened for a few days, while all other function

  • Windows 7 Install and Thinkvantage Key Functionality

    Hello, I plan to install Windows 7 Home, however, I don't want to loose the Thinkvantage key and related functionality.  If someone could provide an outline to maintain Thinkvntage key functionality with Windows 7 install I would greatly appreciate i

  • Remove adjustment from multiple images?

    I have multiple images where I used Sharpen instead of Edge Sharpen. I know I can Lift & Stamp the Edge Sharpen adjustment to multiple images but I can't seem to find a way to remove the Sharpen adjustment from all of them first. Is there a way to ac

  • Publish Services Problem

    I tried to install the new 500px plugin and now none of my Publish Services will work. I get an error message "An internal error has occurred: ?:0 attempt to index field 'exportSettings' (a nil value)" when I try to access the publish services. In Li