Using collections to insert

Hi ,
To improve the performance of the insert statement i was adviced to use the collections.
I'm not getting any idea on how to use collection concept in my code.
Could you please give me a skelton approach to do that.
The following is the sample code (the logic is same in prod code,instead of for 1 -100000 we have cursor in prod) without using collections
create table pop_tab (     col1 number ,col2 number ,col3 number ,col4 number ) 
create or replace package test_collect
is
  procedure proc_lvl_ld ;
  procedure proc_pop_tab (v_var1 number ,v_var2 number ,v_var3 number ,v_var4 number) ;
end test_collect;
create or replace package body test_collect
is
   procedure proc_lvl_ld
    is
      v_cnt number := 1 ;
      v_var1 NUMBER ;
      v_var2 NUMBER ;
      v_var3 NUMBER ;
      v_var4 NUMBER;
     begin
      for i in 1 .. 100000 loop
        v_var1 := v_cnt + 1;
        v_var2 := v_cnt + 2;
        v_var3 := v_cnt + 3;
        v_var4 := v_cnt + 4;
        v_cnt  := v_cnt + 1;
      proc_pop_tab (v_var1 ,v_var2,v_var3,v_var4);
     end loop;
      commit;
      exception when others then
       DBMS_OUTPUT.PUT_LINE ( 'proc_lvl_load'||sqlcode||','||sqlerrm );
    end proc_lvl_ld;
     procedure proc_pop_tab (v_var1 number ,v_var2 number ,v_var3 number ,v_var4 number)
      is
       begin
         insert into pop_tab (col1,col2,col3,col4)
                 values (v_var1,v_var2,v_var3,v_var4) ;
       exception when others then
       DBMS_OUTPUT.PUT_LINE ( 'proc_pop_tab'||sqlcode||','||sqlerrm );             
       end proc_pop_tab;
end test_collect;Now i tried a bit using colliection to improve the insert performance and stuck how to use the collections
create or replace package body test_collect
is
   procedure proc_lvl_ld
    is
       TYPE numtab1 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
       data1    numtab1;
       TYPE numtab2 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
       data2    numtab2;
       TYPE numtab3 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
       data3    numtab3;
       TYPE numtab4 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
       data4    numtab4;
      v_cnt number := 1 ;
     begin
      for i in 1 .. 100000 loop
        data1(data1.count +1) := v_cnt + 1;
        data2(data2.count +1) := v_cnt + 1;
        data3(data3.count +1) := v_cnt + 1;
        data4(data4.count +1) := v_cnt + 1;
        v_cnt  := v_cnt + 1;
      --proc_pop_tab (v_var1 ,v_var2,v_var3,v_var4);
     end loop;
       forall j in 1 ..data1.count
         insert into pop_tab
             values (  --- How to use  the above collection variables here
      commit;
      exception when others then
       DBMS_OUTPUT.PUT_LINE ( 'proc_lvl_load'||sqlcode||','||sqlerrm );
    end proc_lvl_ld;
end;Could you please help me out in this and let me know if i'm not clear
Edited by: Smile on Sep 7, 2012 11:37 AM

Use:
       forall j in 1 ..data1.count
         insert into pop_tab
             values (data1(j),data2(j),data3(j),data4(j));       Now:
SQL> create or replace package body test_collect
  2   is
  3     procedure proc_lvl_ld
  4      is
  5     TYPE numtab1 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
  6     data1    numtab1;
  7     TYPE numtab2 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
  8     data2    numtab2;
  9     TYPE numtab3 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
10     data3    numtab3;
11     TYPE numtab4 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
12     data4    numtab4;
13    v_cnt number := 1 ;
14   begin
15    for i in 1 .. 100000 loop
16      data1(data1.count +1) := v_cnt + 1;
17      data2(data2.count +1) := v_cnt + 1;
18      data3(data3.count +1) := v_cnt + 1;
19      data4(data4.count +1) := v_cnt + 1;
20      v_cnt  := v_cnt + 1;
21        --proc_pop_tab (v_var1 ,v_var2,v_var3,v_var4);
22       end loop;
23     forall j in 1 ..data1.count
24       insert into pop_tab
25       values (data1(j),data2(j),data3(j),data4(j));  
26    commit;
27    exception when others then
28     DBMS_OUTPUT.PUT_LINE ( 'proc_lvl_load'||sqlcode||','||sqlerrm );
29   
30      end proc_lvl_ld;
31  end;
32  /
Package body created.
SQL> exec test_collect.proc_lvl_ld;
proc_lvl_load-6502,ORA-06502: PL/SQL: numeric or value error: number precision too large
PL/SQL procedure successfully completed.
SQL> Why? You declared associative arrays as NUMBER(4) while values you are trying to assign their elements are in range:
for i in 1 .. 100000 loopIf I change it to:
for i in 1 .. 9998 loopThen:
SQL> create or replace package body test_collect
  2   is
  3     procedure proc_lvl_ld
  4      is
  5     TYPE numtab1 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
  6     data1    numtab1;
  7     TYPE numtab2 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
  8     data2    numtab2;
  9     TYPE numtab3 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
10     data3    numtab3;
11     TYPE numtab4 IS TABLE OF NUMBER (4) INDEX BY BINARY_INTEGER;
12     data4    numtab4;
13    v_cnt number := 1 ;
14   begin
15    for i in 1 .. 9998 loop
16      data1(data1.count +1) := v_cnt + 1;
17      data2(data2.count +1) := v_cnt + 1;
18      data3(data3.count +1) := v_cnt + 1;
19      data4(data4.count +1) := v_cnt + 1;
20      v_cnt  := v_cnt + 1;
21        --proc_pop_tab (v_var1 ,v_var2,v_var3,v_var4);
22       end loop;
23     forall j in 1 ..data1.count
24       insert into pop_tab
25       values (data1(j),data2(j),data3(j),data4(j));  
26    commit;
27    exception when others then
28     DBMS_OUTPUT.PUT_LINE ( 'proc_lvl_load'||sqlcode||','||sqlerrm );
29   
30      end proc_lvl_ld;
31  end;
32  /
Package body created.
SQL> exec test_collect.proc_lvl_ld;
PL/SQL procedure successfully completed.
SQL> select count(*) from pop_tab
  2  /
  COUNT(*)
      9998
SQL> SY.

Similar Messages

  • Error while using collection in insert...with...select stmt

    declare
    cursor c1 as select clb from clob_table;
    type t1_t is table of clob index by binary integer;
    t1 t1_t;
    begin
    open c1;
    fetch bulk collect into t1;
    close c1;
    insert into some_table
    (id,
    clob_field)
    with
    x (select rownum x, trunc(dbms_random.value(1,30)) rnd from all_objects where rownum < 11);
    select
    x, --id
    t1(rnd)
    from x;
    it gives error identifier 'RND' must be declared.
    Any suggessions ???
    Thanks for your time..

    I think this is enough code to generate error.
    error is comming because i am going to use rnd as index of collection t1.

  • Need a function to calcultate value using collections

    Hi
    I have a main table ab ( sno,empid,joindate,sal_mth, sal_yr ,run_date)
    a stage table cd (sno,empid,joindate,sal_mth).
    Now the catch is for every run( every day run), I need to get all data from ab and roll fwd them to the run date ( only change would be run_date)
    Also If in stage table cd we receive data for the combination sno,empid,joindate,sal_mth which is present in ab, we would
    change the sal_yr in ab - criteria would be sal_yr:=sal_yr + cd.sal_mth
    I have done it but in primitive way using union etc. Would prefer a function to do this using collection.
    Any suggestions?
    Regards
    SHUBH

    Something like below ? (to be turned into function as you didn't mention what to return)
    procedure roll_fwd(p_run_date in date) is
    begin
      insert into ab
      select a.sno,a.empid,a.joindate,a.sal_mth,a.sal_yr + nvl(c.sal_mth,0) sal_yr,p_run_date
        from ab a,cd c
       where a.run_date = p_run_date - 1
         and a.sno = c.sno(+)
         and a.empid = c.empid(+)
         and a.joindate = c.joindate(+)
         and a.sal_mth = c.sal_mth(+);
    end;Regards
    Etbin
    Edited by: Etbin on 20.2.2011 17:14
    No action taken on cd data - if they're lucky, they may get another increase next day ;)

  • How to use collect?

    hi all ,
    i want to add net value of all the line items which are in the same group
    am using collect but am unable to do that.
    plz suggest me what to do?
    DATA: BEGIN OF del_grp_data occurs 0,
            vbeln like vbap-vbeln,  " Sales document
            grkor like vbap-grkor,  " Delivery group
            netwr like vbap-netwr, "net value
            posnr like vbap-posnr,  " Sales document item
             End OF del_grp_data.
    SELECT vbeln grkor pstyv netwr
        posnr
       FROM   vbap
        INTO corresponding fields of  TABLE del_grp_data
        FOR ALL ENTRIES IN orders_vbeln
        WHERE vbeln eq orders_vbeln-vbeln.
    loop at del_grp_data.
    collect ord_grp_data .
    endloop.
    Regards,
    Amit.

    Basic form
    COLLECT [wa INTO] itab.
    Addition:
    ... SORTED BY f
    Cannot Use Short Forms in Line Operations.
    Effect
    COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key. (See also Defining Keys for Internal Tables). The key values are taken either from the header line of the internal table itab, or from the explicitly-specified work area wa. The line type of itab must be flat - that is, it cannot itself contain any internal tables. All the components that do not belong to the key must be numeric types ( ABAP Numeric Types).
    If the system finds an entry, the numeric fields that are not part of the table key (see ABAPNumeric Types) are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead.
    The way in which the system finds the entries depends on the type of the internal table:
    STANDARD TABLE:
    The system creates a temporary hash administration for the table to find the entries. This means that the runtime required to find them does not depend on the number of table entries. The administration is temporary, since it is invalidated by operations like DELETE, INSERT, MODIFY, SORT, ...). A subsequent COLLECT is then no longer independent of the table size, because the system has to use a linear search to find entries. For this reason, you should only use COLLECT to fill standard tables. U
    SORTED TABLE:
    The system uses a binary search to find the entries. There is a logarithmic relationship between the number of table entries and the search time.
    HASHED TABLE:
    The system uses the internal hash administration of the table to find records. Since (unlike standard tables), this remains intact even after table modification operations, the search time is always dependent on the number of table entries.
    For standard tables and SORTED TABLEs, the system field SY-TABIX contains the number of the existing or newly-added table entry after the APPEND. With HASHED TABLEs, SY-TABIX is set to 0.
    Notes
    COLLECT allows you to create a unique or summarized dataset, and you should only use it when this is necessary. If neither of these characteristics are required, or where the nature of the table in the application means that it is impossible for duplicate entries to occur, you should use INSERT [wa INTO] TABLE itab instead of COLLECT. If you do need the table to be unique or summarized, COLLECT is the most efficient way to achieve it.
    If you use COLLECT with a work area, the work area must be compatible with the line type of the internal table.
    If you edit a standard table using COLLECT, you should only use the COLLECT or MODIFY ... TRANSPORTING f1 f2 ... statements (where none of f1, f2, ... may be in the key) enthalten sein). Only then can you be sure that:
    -The internal table actually is unique or summarized
    -COLLECT runs efficiently. The check whether the dataset
    already contains an entry with the same key has a constant
    search time (hash procedure).
    If you use any other table modification statements, the check for entries in the dataset with the same key can only run using a linear search (and will accordingly take longer). You can use the function module ABL_TABLE_HASH_STATE to test whether the COLLECT has a constant or linear search time for a given standard table.
    Example
    Summarized sales figures by company:
    TYPES: BEGIN OF COMPANY,
            NAME(20) TYPE C,
            SALES    TYPE I,
          END OF COMPANY.
    DATA: COMP    TYPE COMPANY,
          COMPTAB TYPE HASHED TABLE OF COMPANY
                                    WITH UNIQUE KEY NAME.
    COMP-NAME = 'Duck'.  COMP-SALES = 10. COLLECT COMP INTO COMPTAB.
    COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.
    COMP-NAME = 'Duck'.  COMP-SALES = 30. COLLECT COMP INTO COMPTAB.
    Table COMPTAB now has the following contents:
               NAME    | SALES
              Duck    |   40
              Tiger   |   20
    Addition
    ... SORTED BY f
    Effect
    COLLECT ... SORTED BY f is obsolete, and should no longer be used. It only applies to standard tables, and has the same function as APPEND ... SORTED BY f, which you should use instead. (See also Obsolete Language Elements).
    Note
    Performance:
    Avoid unnecessary assignments to the header line when using internal tables with a header line. Whenever possible, use statements that have an explicit work area.
    For example, " APPEND wa TO itab." is approximately twice as fast as " itab = wa. APPEND itab.". The same applies to COLLECT and INSERT.
    The runtime of a COLLECT increases with the width of the table key and the number of numeric fields whose contents are summated.
    Note
    Non-Catchable Exceptions:
    COLLECT_OVERFLOW: Overflow in an integer field during addition
    COLLECT_OVERFLOW_TYPE_P: Overflow in a type P field during addition.
    TABLE_COLLECT_CHAR_IN_FUNCTION: COLLECT on a non-numeric field.
    Related
    APPEND, WRITE ... TO, MODIFY, INSERT
    Additional help
    Inserting SummarizedTable Lines

  • Using Collections

    HI EXPERTS
    i want to implement the below procedure using pl/sql collections can any one suggest for the followinG
    create or replace procedure test_proc
    is
    cursor c1 is select * from temp;
    cursor c1 is select * from MONITORING;
    i temp%rowtype;
    j MONITORING%type;
    begin
    open c1
    loop
    if (I.RES_CODE = '02' and i.CUST_ACCNO = j.CUST_ACCNO and i.CUST_MMID = j.CUST_MMID and i.CUST_MOBNO = j.CUST_MOBNO) then
    Insert into APP
    (CUST_ACCNO,CUST_MOBNO,CUST_MMID,REMARKS,RES_CODE)
    SELECT I.CUST_ACCNO,I.CUST_MOBNO,I.CUST_MMID,i.RES_CODE,'03'
    FROM temp I
    WHERE EXISTS (SELECT NULL FROM MONITORING M WHERE M.CUST_ACCNO = I.CUST_ACCNO AND M.CUST_MOBNO = I.CUST_MOBNO AND M.CUST_MMID = I.CUST_MMID)
    AND I.RES_CODE = '02';
    elsif (I.RES_CODE = '02' and (i.CUST_ACCNO != j.CUST_ACCNO or i.CUST_MMID != j.CUST_MMID or i.CUST_MOBNO != j.CUST_MOBNO)) then
    Insert into APP
    (CUST_ACCNO,CUST_MOBNO,CUST_MMID,REMARKS,RES_CODE)
    SELECT I.CUST_ACCNO,I.CUST_MOBNO,I.CUST_MMID,i.RES_CODE,'04'
    FROM temp I
    WHERE NOT EXISTS (SELECT NULL FROM MONITORING M WHERE M.CUST_ACCNO = I.CUST_ACCNO AND M.CUST_MOBNO = I.CUST_MOBNO AND M.CUST_MMID = I.CUST_MMID)
    AND I.RES_CODE = '02';
    elsif I.RES_CODE != '02' then
    Insert into APP
    (CUST_ACCNO,CUST_MOBNO,CUST_MMID,REMARKS,RES_CODE)
    SELECT I.CUST_ACCNO,I.CUST_MOBNO,I.CUST_MMID,i.RES_CODE,'02'
    FROM temp I
    WHERE I.RES_CODE != '02';
    exit whenc1
    end loop;
    close c1;
    end;
    /

    Didn't you have a discussion just yesterday about Re: Collections:?
    As was discussed in your previous thread, if the goal is to improve efficiency, it would be more efficient to get rid of the cursors and to just issue SQL statements to insert all the rows at once. Using collections may be an improvement over slow row-by-row processing but set-based processing will be even more efficient.
    In yesterdays thread, multiple people showed you how to use collections. Is there some specific problem/ question that you have? Presumably, the goal of you posting here is to learn how to use collections on your own, not to post all of your procedures one at a time and ask others to rewrite them using collections.
    The code you posted can't possibly compile. For example, you have two different declarations of the c1 cursor that query two completely different tables. There is an "exit whenc1" call that doesn't appear to match to any loop. Your code is also rather hard to follow since it is not formatted. If you put the tag \ (6 characteers all lower case) immediately before and after a code snippet, the forum will maintain all the spacing.  That makes the code much, much easier to read.
    Justin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to use collections

    Hi
    i'm still quite the novice on APEX so bare with me.
    my problem is as follows:
    I have to develop a page for mass registration of sales, in APEX, and the client would like a tabular form with 20 empty rows in which the client can insert sales data.
    i'm developing on apex version 3.0.0.00.20 on a 10g Enterprise Edition Release 10.1.0.5.0 database.
    i've tried using collections for the job, but my problem is that i can't get the code to save any changes to the collection??
    i've created a on load before header process
    that creates a collection named SALGSBUFREG using apex_collection.create_collection_from_query where no rows are returned.
    the table i query is the table that the sales data eventually is inserted into.
    /** on-load before header process start */
    declare
    c_coll_name constant varchar2(100) := 'SALGSBUFREG';
    c_number_of_rows constant number := 20;
    begin
    if apex_collection.collection_exists(p_collection_name=>c_coll_name) then
    apex_collection.delete_collection(p_collection_name=>c_coll_name);
    end if;
    apex_collection.create_collection_from_query(
    p_collection_name=>c_coll_name,
    p_query=>'SELECT varenummer,
    antal,
    aip_omsaetning,
    apotekskode ,
    salgsdato,
    salgsperiode,
    dlibruger_id,
    salgsbatch_id,
    sygehuskode,
    afvist,
    indsat,
    updateret,
    registreringsdato
    FROM salgsbuffere
    WHERE 1 = 2');
    /** create c_number_of_rows empty rows*/
    for i in 1 .. c_number_of_rows
    loop
    apex_collection.add_member (p_collection_name => c_coll_name,
    p_c001 => ' ',
    p_c002 => ' ',
    p_c003 => ' ',
    p_c004 => ' ',
    p_c005 => null,
    p_c006 => ' ',
    p_c007 => ' ',
    p_c008 => ' ',
    p_c009 => ' ',
    p_c010 => ' ',
    p_c011 => ' ',
    p_c012 => ' ',
    p_c013 => null
    end loop;
    end;
    /** on-load before header process stop */
    i've created a report region called salgs_buf based on the following query:
    select c001, c002, c003, c004, c005, c006, c007, c008,c009,c010,c011, c012,c013
    from apex_collections
    where collection_name = 'SALGSBUFREG'
    i've made the report attributes c001 - c005 editable.
    finally i've made a on submit - after computations and validations process with the following content:
    declare
    c pls_integer := 0;
    c_coll_name constant varchar2(100) := 'SALGSREGBUF';
    begin
    for c1 in (
    select seq_id from apex_collections
    where collection_name = c_coll_name
    order by seq_id) loop
    c := c+1;
    apex_collection.update_member_attribute (p_collection_name=> c_coll_name,
    p_seq=> c1.seq_id,p_attr_number =>4,p_attr_value=>wwv_flow.g_f01(c));
    apex_collection.update_member_attribute (p_collection_name=> c_coll_name,
    p_seq=> c1.seq_id,p_attr_number =>5,p_attr_value=>wwv_flow.g_f02(c));
    apex_collection.update_member_attribute (p_collection_name=> c_coll_name,
    p_seq=> c1.seq_id,p_attr_number =>6,p_attr_value=>wwv_flow.g_f03(c));
    apex_collection.update_member_attribute (p_collection_name=> c_coll_name,
    p_seq=> c1.seq_id,p_attr_number =>7,p_attr_value=>wwv_flow.g_f04(c));
    end loop;
    end;
    For some reason the collection dosen't get updated????
    any idears why, or am I using the wrong apporach.

    first off thanks for your help.
    I seem to have cracked the nut so to speak, i'm just posting my solution
    I've changed the process so that exisisting collections aren't deleted.
    /** before header process start**/
    DECLARE
    c_coll_name CONSTANT VARCHAR2 (100) := 'SALGSBUFREG';
    c_number_of_rows CONSTANT NUMBER := 20;
    v_row_count_diff NUMBER;
    v_row_count NUMBER;
    BEGIN
    IF NOT apex_collection.collection_exists (c_coll_name)
    THEN
    apex_collection.create_collection_from_query
    (p_collection_name => c_coll_name,
    p_query => 'SELECT varenummer,
    antal,
    aip_omsaetning,
    apotekskode,
    salgsdato,
    salgsperiode,
    dlibruger_id,
    salgsbatch_id,
    sygehuskode,
    afvist,
    indsat,
    updateret,
    registreringsdato
    FROM salgsbuffere
    WHERE 1 = 2'
    -- create c_number_of_rows empty rows
    FOR i IN 1 .. c_number_of_rows
    LOOP
    apex_collection.add_member (p_collection_name => c_coll_name,
    p_c001 => ' ',
    /** vare nr*/
    p_c002 => ' ',
    /** antal */
    p_c003 => ' ',
    /** aip_omsaetning*/
    p_c004 => ' ',
    /** apotekskode */
    p_c005 => NULL,
    /** salgsdato*/
    p_c006 => ' ',
    /** salgsperiode */
    p_c007 => ' ',
    /** dlibruger_id*/
    p_c008 => ' ',
    /** salgsbatch_id*/
    p_c009 => ' ',
    /** sygehuskode*/
    p_c010 => ' ',
    /** afvist*/
    p_c011 => ' ',
    /** indsat*/
    p_c012 => ' ',
    /** updateret*/
    p_c013 => NULL
    /** registrerings dato*/
    END LOOP;
    -- the empty collection is set to unchanged
    apex_collection.reset_collection_changed (c_coll_name);
    END IF;
    END;
    /** before header process stop**/
    i've made a on submit and before computation process that populates the collection with the values that i input in the updatable rport region on my page, and it works just fine:
    /** on submit and before computation start**/
    /* Formatted on 2008/06/11 09:37 (Formatter Plus v4.8.8) */
    DECLARE
    c_coll_name CONSTANT VARCHAR2 (100) := 'SALGSBUFREG';
    c_row_count CONSTANT NUMBER := 20;
    v_member_count NUMBER;
    BEGIN
    IF apex_collection.collection_exists (c_coll_name)
    THEN
    FOR i IN 1 .. apex_collection.collection_member_count (c_coll_name)
    LOOP
    apex_collection.update_member_attribute
    (p_collection_name => c_coll_name,
    p_seq => i,
    p_attr_number => 1,
    p_attr_value => apex_application.g_f01
    (i)
    apex_collection.update_member_attribute
    (p_collection_name => c_coll_name,
    p_seq => i,
    p_attr_number => 2,
    p_attr_value => apex_application.g_f02
    (i)
    apex_collection.update_member_attribute
    (p_collection_name => c_coll_name,
    p_seq => i,
    p_attr_number => 3,
    p_attr_value => apex_application.g_f03
    (i)
    apex_collection.update_member_attribute
    (p_collection_name => c_coll_name,
    p_seq => i,
    p_attr_number => 4,
    p_attr_value => apex_application.g_f04
    (i)
    apex_collection.update_member_attribute
    (p_collection_name => c_coll_name,
    p_seq => i,
    p_attr_number => 5,
    p_attr_value => apex_application.g_f05
    (i)
    END LOOP;
    END IF;
    END;
    /** on submit and before computation start**/

  • How can I use multiple row insert or update into DB in JSP?

    Hi all,
    pls help for my question.
    "How can I use multiple rows insert or update into DB in JSP?"
    I mean I will insert or update the multiple records like grid component. All the data I enter will go into the DB.
    With thanks,

    That isn't true. Different SQL databases have
    different capabilities and use different syntax, That's true - every database has its own quirks and extensions. No disagreement there. But they all follow ANSI SQL for CRUD operations. Since the OP said they wanted to do INSERTs and UPDATEs in batches, I assumed that ANSI SQL was sufficient.
    I'd argue that it's best to use ANSI SQL as much as possible, especially if you want your JDBC code to be portable between databases.
    and there are also a lot of different ways of talking to
    SQL databases that are possible in JSP, from using
    plain old java.sql.* in scriptlets to using the
    jstlsql taglib. I've done maintenance on both, and
    they are as different as night and day.Right, because you don't maintain JSP and Java classes the same way. No news there. Both java.sql and JSTL sql taglib are both based on SQL and JDBC. Same difference, except that one uses tags and the other doesn't. Both are Java JDBC code in the end.
    Well, sure. As long as you only want to update rows
    with the same value in column 2. I had the impression
    he wanted to update a whole table. If he only meant
    update all rows with the same value in a given column
    with the same value, that's trivial. All updates do
    that. But as far as I know there's know way to update
    more than one row where the values are different.I used this as an example to demonstrate that it's possible to UPDATE more than one row at a time. If I have 1,000 rows, and each one is a separate UPDATE statement that's unique from all the others, I guess I'd have to write 1,000 UPDATE statements. It's possible to have them all either succeed or fail as a single unit of work. I'm pointing out transaction, because they weren't coming up in the discussion.
    Unless you're using MySQL, for instance. I only have
    experience with MySQL and M$ SQL Server, so I don't
    know what PostgreSQL, Oracle, Sybase, DB2 and all the
    rest are capable of, but I know for sure that MySQL
    can insert multiple rows while SQL Server can't (or at
    least I've never seen the syntax for doing it if it
    does).Right, but this syntax seems to be specific to MySQL The moment you use it, you're locked into MySQL. There are other ways to accomplish the same thing with ANSI SQL.
    Don't assume that all SQL databases are the same.
    They're not, and it can really screw you up badly if
    you assume you can deploy a project you've developed
    with one database in an environment where you have to
    use a different one. Even different versions of the
    same database can have huge differences. I recommend
    you get a copy of the O'Reilly book, SQL in a
    Nutshell. It covers the most common DBMSes and does a
    good job of pointing out the differences.Yes, I understand that.
    It's funny that you're telling me not to assume that all SQL databases are the same. You're the one who's proposing that the OP use a MySQL-specific extension.
    I haven't looked at the MySQL docs to find out how the syntax you're suggesting works. What if one value set INSERT succeeds and the next one fails? Does MySQL roll back the successful INSERT? Is the unit of work under the JDBC driver's control with autoCommit?
    The OP is free to follow your suggestion. I'm pointing out that there are transactions for units of work and ANSI SQL ways to accomplish the same thing.

  • Using SQL DML INSERT how can I put a ref atrib with out a select

    What i am trying to do is use the insert statment to insert into a object table that has an atribute that is scope to another table as a REF of that type.
    something like this:
    INSERT INTO TABLE_AAA (NAME, SOMETHING) VALUES ('AAA','BBB');
    SELECT REF(P),P.NAME FROM TABLE_AAA WHERE P.NAME='AAA';
    IT GIVES ME THE REF THAT ARE 85 NUMBER AND LETTERS AND THE NAME=AAA
    SO WITH THIS INFORMATION I COPY THE REF AND PUT IT IN TABLE_BBB AS:
    INSERT INTO TABLE_BBB (REF_TYPE_AAA, SOMETHING)
    VALUES(1234567890123456789012345678901234567890123456789012345678901234567890123456789012345,'QQQ');
    THIS IS NOT WORKING I TRY DIFERENT THINGS, BUT NOTHING WORKS
    WHAT I DONT WANT IS TO USE THE SELECT IN THE INSERT, I JUST WANT TO PUT THE VALUE OF THE REF IN THE INSERT.
    WHAT FUNTION DO I NEED TO CAST OR CONVERT TO A REF DOES NUMBERS AND LETTERS THAT I HAVE, THAT I CAN USE A SIMPLE INSERT COMMAND.
    THANK IN ADVANCE FOR YOUR KIND

    Hi,
    Could you give structures of objects (tables/type objects)?
    It may help to clarify your doubt.
    Regards,
    Sailaja

  • Use Spry to insert data into a database?

    I'm new to Spry, so I have a question:
    Can I use Spry to insert data into a MySQL database without
    reloading the site?
    Reading data from XML file works fine, but I don't know if
    writing is possible..

    I don't get it... I tried this:
    <script type="text/javascript" src="spry/xpath.js"
    /></script>
    <script type="text/javascript" src="spry/SpryData.js"
    /></script>
    <script type="text/javascript">
    /* <![CDATA[ */
    function subscribe() {
    var subscribe;
    var info =
    document.getElementById("subscription_info").value;
    if (document.getElementsByName("subscription")[0].checked ==
    true) {
    subscribe = "ja";
    else {
    subscribe = "nein";
    var dsSubscribe = new
    Spry.Data.XMLDataSet("include/inc_subscribe.php?subscribe="+subscribe+"&info="+info+"&sit e=<?=$_GET['site'];?>",
    "subscription/ok");
    /* ]]> */
    </script>
    <div id="infos">
    <input type="radio" name="subscription" value="ja" />
    dabei
    <input type="radio" name="subscription" value="nein"
    /> nicht dabei
    <input type='text' class='text'
    id='subscription_info_text' />
    <input type='submit' id="subscription_submit"
    value='Eintragen' onClick="subscribe()" />
    </div>
    But the data isn't inserted into the mysql database. When I
    start the php script directly, it works, so I think it's not a php
    problem.

  • In Oracle 10g Error while using COLLECT

    I getting error while using collect in 10g
    SQL> ed
    Wrote file afiedt.buf
    1 SELECT deptno
    2 , COLLECT(ename) AS emps
    3 FROM emp
    4 GROUP BY
    5* deptno
    SQL> /
    , COLLECT(ename) AS emps
    ERROR at line 2:
    ORA-00932: inconsistent datatypes: expected NUMBER got -
    Please give me the solution.

    you are using old version of SQL*Plus. if you use later version it will give you correct result.
    Edited by: unus on Mar 14, 2010 4:25 AM

  • SELECT * cannot be used in an INSERT INTO query when the source or destination table contains a multivalued field

    Hi,
    I am using Access 2013 and I have the following VBA code, 
    strSQL = "INSERT INTO Master SELECT * from Master WHERE ID = 1"
     DoCmd.RunSQL (strSQL)
    when the SQL statement is run, I got this error.
    SELECT * cannot be used in an INSERT INTO query when the source or destination table contains a multivalued field
    Any suggestion on how to get around this?
    Please advice and your help would be greatly appreciated!

    Rather than modelling the many-to-many relationship type by means of a multi-valued field, do so by the conventional means of modelling the relationship type by a table which resolves it into two one-to-many relationship types.  You give no indication
    of what is being modelled here, so let's assume a generic model where there is a many-to-many relationship type between Masters and Slaves, for which you'd have the following tables:
    Masters
    ....MasterID  (PK)
    ....Master
    Slaves
    ....SlaveID  (PK)
    ....Slave
    and to model the relationship type:
    SlaveMastership
    ....SlaveID  (FK)
    ....MasterID  (FK)
    The primary key of the last is a composite one of the two foreign keys SlaveID and MasterID.
    You appear to be trying to insert duplicates of a subset of rows from the same table.  With the above structure, to do this you would firstly have to insert rows into the referenced table Masters for all columns bar the key, which, presuming this to be
    an autonumber column, would be assigned new values automatically.  To map these new rows to the same rows in Slaves as the original subset you would then need to insert rows into SlaveMastership with the same SlaveID values as those in Slaves referenced
    by those rows in Slavemastership which referenced the keys of the original subset of rows from Masters, and the MasterID values of the rows inserted in the first insert operation.  This would require joins to be made between the original and the new subsets
    of rows in two instances of Masters on other columns which constitute a candidate key of Masters, so that the rows from SlaveMastership can be identified.
    You'll find examples of these sort of insert operations in DecomposerDemo.zip in my public databases folder at:
    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
    In this little demo file non-normalized data from Excel is decomposed into a set of normalized tables.  Unlike your situation this does not involve duplication of rows into the same table, but the methodology for the insertion of rows into a table which
    models a many-to-many relationship type is broadly the same.
    The fact that you have this requirement to duplicate a subset of rows into the same table, however, does make me wonder about the validity of the underlying logical model.  I think it would help us if you could describe in detail just what in real world
    terms is being modelled by this table, and the purpose of the insert operation which you are attempting.
    Ken Sheridan, Stafford, England

  • Acrobat x pro crashes when I try to flatten a pdf after using typewriter to insert a date

    acrobat x pro crashes when I try to flatten a pdf after using typewriter to insert a date.
    Has anybody else seen this problem? Is there a fix?
    I wanted to download the demo of acrobat XI but it says the previous version will be removed and deactivate the license. That sucks!

    Yes it is up to date.
    I can use the stamp tool and flatten it after with no problems, but the typewriter tool crashes it upon attempting to flatten.

  • Problem  in using COLLECT statment

    Hi experts,
    getting problem in using COLLECT statement.  attaching my code below.
    LOOP AT it_final1 INTO wa_final1.
         LOOP AT it_final INTO wa_final WHERE vagrp = wa_final1-vagrp AND verwmerkm = wa_final1-verwmerkm.
           wa_final1-anzwertg = wa_final-anzwertg.
           COLLECT wa_final1 INTO it_final1.
         ENDLOOP.
         CLEAR: wa_final1, wa_final.
       ENDLOOP.
    here , i want to use collect statement only for  "anzwertg" field from internal table. but 2 more fields also getting  compressed .
    attaching screen shot
    here, original_input and max fields also getting compressed.
    could anybody please help me out in this.
    Thanks in advance.
    Regards
    satish

    Hi satish....
    collect statement works based on char fields..
    by default it will taken char fields as a key fields .........
    if any char field is repeated then corresponding numc values will be added....
    other wise it will act as a APPEND statement......
    Here in ur code there is no char field then how it will caliculate that values...
    just check it once..
    Regards,
    Vamsi....

  • Inserting multiple rows using a single Insert statement without using dual

    Hi all,
    i am trying to insert multiple rows using a single insert statement like the below one.
    The below one works fine..
    But is there any other change that can be done in the below one without using dual...
    insert all
    into ps_hd_samp (num1,num2) values (1,1)
    into ps_hd_samp (num1,num2) values (2,2)
    into ps_hd_samp (num1,num2) values (3,3)
    select 1 from dual;

    NiranjanSe wrote:
    Hi all,
    i am trying to insert multiple rows using a single insert statement like the below one.
    The below one works fine..
    But is there any other change that can be done in the below one without using dual...
    insert all
    into ps_hd_samp (num1,num2) values (1,1)
    into ps_hd_samp (num1,num2) values (2,2)
    into ps_hd_samp (num1,num2) values (3,3)
    select 1 from dual;
    SQL> create table ps_hd_samp (num1 number,num2 number);
    Table created.
    SQL> insert all
      2  into ps_hd_samp (num1,num2) values (1,1)
      3  into ps_hd_samp (num1,num2) values (2,2)
      4  into ps_hd_samp (num1,num2) values (3,3)
      5  select count(*)
      6  from ps_hd_samp;
    3 rows created.
    SQL> select * from ps_hd_samp;
          NUM1       NUM2
             1          1
             2          2
             3          3

  • How to use collect statement for below

    data : begin of itab,
             n(3) type c,
          n1 type n,
          k(5) type c,
          end of itab.
    select n n1 from into itab table /zteest.
    *internal table has
    n      n1    k
    gar    100  uji
    hae    90   iou
    gar    90   uji
    hae    87   iou
    I want
    gar 190
    hae 177
    How to use collect statement as n1 is n ..?
    let me know..
    Thanks

    try this..
    DATA : BEGIN OF itab OCCURS 0,
    n(3) TYPE c,
    n1(3) TYPE p DECIMALS 2,
    k(5) TYPE c,
    END OF itab.
    itab-n = 'gar'.
    itab-n1 = 100.
    itab-k = 'uji'.
    COLLECT itab .CLEAR itab.
    itab-n = 'hae'.
    itab-n1 = 90.
    itab-k = 'iou'.
    COLLECT itab .CLEAR itab.
    itab-n = 'gar'.
    itab-n1 = 90.
    itab-k = 'uji'.
    COLLECT itab .CLEAR itab.
    itab-n = 'hae'.
    itab-n1 = 87.
    itab-k = 'iou'.
    COLLECT itab .CLEAR itab.

Maybe you are looking for

  • Inserting tabbed navigation code makes my web page not display in design view..

    Hi everyone.. I am on Dreamweaver 8 for MAC, and was working on a couple of web pages..these pages included divs and tables. But when I tried inserting a tabbed navigation I found at: http://www.dynamicdrive.com/dynamicindex17/tabcontent.htm In a nut

  • Default Sort Order for Library View

    It would be fantastic if there were a user defined preference for a default sort order in Library view. For example, some users may prefer to always view the images by File Name, or Rating, etc, without having to change the sort order for each indivi

  • Where are web-clip widgets stored in Mountain Lion?  Want to use my webpage in iBooks.

    I made a webpage that has a custom map that I want to put into an iBook.  The map works great as a Dashboard Widget and I'd like to find that Widget and put it into my iBook.  For the life of me I cannot find where it is Stored in Mountain Lion.  Any

  • Final cut x import window freezes

    Import from camera works, but FCP import window freezes.  6-core Mac Pro, plenty of RAM, fast drives, worked before.

  • LMS 4.2 MIBs Polled

    Hi All, Want to understand the poller management and how many MIBs are polled by each interface if we are using interface utilization template. I have created the poller with adding the interface utilization template and there were 10 interfaces adde