Thread: Help using Cursor to move an array into a new table

  1. #1
    Registered User
    Join Date
    Apr 2008

    Help using Cursor to move an array into a new table

    Hi. Can anyone tell me how they would move an array into a empty properly formatted table using the CURSOR? For simplicity say the array is created with structure below:

    #define SIZE 100
     struct     myarray{
        char    firstname[20];
        char    middlename[20];
        char    lastname[40];
                  } myarray;
    and in program created an array called myarray2[SIZE]

    in the program the array was filled from array[0]...array[99]
    and the next step was to place the contents of the array into a new table that was already created with proper formatting for firstname,middlename,lastname how would it be accomplished using a CURSOR?

    Thanks for any advice.


  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    If by "table", you are referring to a DB2 table, I suspect you would simply iterate through your array in a FOR loop, using an index, issuing an INSERT each time.
    for (i = 0; i < SIZE ; i++) { 
    EXEC SQL INSERT INTO table VALUES( :myarray[i].firstname, :myarray[i].lastname, etc..... // check your syntax
    if success....
    else .... ;  
    But, perhaps you might have a block insert available on your platform??
    Last edited by Dino; 04-17-2008 at 10:16 AM. Reason: typo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008

    Thats the approach I took - Thanks but...

    The reords are not showing up in the table for some reason. I used the approach you listed and there are no compile errors but when I select * from the table there are 0 rows. I am looking into that now. Thanks.

     EXEC SQL INSERT INTO CDJ_COST2 (cc, cc_sn, cc_desc, cc_grp)
        VALUES (:cc_lkup2[y].cc, :cc_lkup2[y].cc_sn, :cc_lkup2[y].cc_desc, :cc_lkup2[y].cc_grp);
    I am using a while (1) loop until all rows are reached.

  4. #4
    Registered User
    Join Date
    Apr 2008

    Sql Commit Would Help!

    Forgot To Commit After Code. Sorry About That.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  5. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM