Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Insert data from internal table into database

Former Member
0 Likes
11,496

Hi,

I am not able to insert the internal table content into the database, properly.

I have a DB table and an IT of same structure. This is my code:


LOOP AT ltb_factuur.
*  INSERT INTO z4968_factuur VALUES ltb_factuur.
*  INSERT z4968_factuur FROM ltb_factuur.
*z4968_factuur[] = ltb_factuur[].
    MOVE-CORRESPONDING ltb_factuur TO z4968_factuur.
    IF sy-subrc <> 0.
      it_err_msgs-errors = 'Error inserting record in database'.
      APPEND it_err_msgs.
      EXIT.
    ENDIF.
    APPEND z4968_factuur.
*  MODIFY z4968_factuur.

  ENDLOOP.

There are about 100 records in the IT, but I can see only 2 records in the DB table! Can anybody let me know where am I going wrong here? I used INSERT too before using MOVE_CORRESPONDING, this too failed.

Thank you,

Warm regards,

Deepak

14 REPLIES 14
Read only

Former Member
0 Likes
3,555

Have you checked that the data is unique to the keys that you have defined on the table. If not you will lose some records as they overwrite existing data.

If it's updating 2 records only then this would be the first thing that I would check out.

Cherrs

Jules

Read only

Former Member
0 Likes
3,555

Hi,

Try to use Work Area in MOVE-CORRESPONDING statement.

Hope it is helps.

Regards,

T.Durai murugan.

Read only

Former Member
0 Likes
3,555

Hi Deepak,

Try to use the Insert statement..this might solve your program

Read only

Former Member
0 Likes
3,555

hiii

create an internal table with the same structure like the database table.get data into that internal table.

Now use Modify Query to modify the database table.

like below

Modify (database-table) from (internal-table)

regards

twinkal

Read only

Former Member
0 Likes
3,555

Hi deepak,

use the below statement.

insert dbtab from table itab.

Reward if useful.

Cheers,

Balaji

Read only

Former Member
0 Likes
3,555

Hi Deepak,

Use-

loop at ltb_factuur into wa_factuur.

MODIFY z4968_factuur from wa_factuur.

endloop.

Regards,

Aparna Gaikwad.

Read only

Former Member
0 Likes
3,555

Hi,

Try this code.

data: wa_ltb_factuur like line of ltb_factuur,

wa_z4968_factuur like line of z4968_factuur.

loop at ltb_factuur into wa_ltb_factuur.

move wa_ltb_factuur to wa_z4968_factuur.

insert z4968_factuur from wa_z4968_factuur.

clear wa_z4968_factuur.

endloop.

Read only

Former Member
0 Likes
3,555

Hi,

1. From your code it seems the

internal table = ltb_factuur (with header line)

database table = z4968_factuur

2. This simple logic will also work.


Loop at ltb_factuur.
MODIFY z4968_factuur from ltb_factuur.
Endloop.

MODIFY will automatically take care of appending or modifying

the records in the database, based upon the primary key combination from the work area and availability of that record

in the database table.

regards,

amit m.

Read only

Former Member
0 Likes
3,555

pass the data from internal table to ztable workarea and use

insert ztable.

may be u have duplicate entries in the internal table.

check the key fields in the internal table.

if they are unique u can get that in ztable.

Read only

former_member182354
Contributor
0 Likes
3,555

Hi Deepak,

Use Update statement.

Raghav

Read only

former_member787646
Contributor
0 Likes
3,555

Hi

Try the following...

INSERT INTO z4968_factuur FROM ltb_factuur.

( * No need to loop the internal table. )

Hope it helps.

Murthy

Edited by: Kalyanam Seetha Rama Murthy on Jul 31, 2008 9:42 AM

Read only

Former Member
0 Likes
3,555

Hi,

First of all create an internal table with the same structure like the database table you haveand also create a work area of same structure, then get data into that internal table.

Then use MODIFY statement . MODIFY statement takes care of inserting a now record or updating a record.

Try this piece of code:

data: wa_ltb_factuur like line of ltb_factuur,
wa_z4968_factuur like line of z4968_factuur.

Loop at ltb_factuur into wa_ltb_factuur.

move wa_ltb_factuur to wa_z4968_factuur.
MODIFY z4968_factuur from wa_z4968_factuur.
clear wa_z4968_factuur.

 IF sy-subrc EQ 0.
        COMMIT WORK.
      ELSE.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

Endloop.

hope this helps.

thanx,

dhanashri.

Edited by: Dhanashri Pawar on Jul 31, 2008 9:29 AM

Read only

0 Likes
3,555

Thank you so much everyone for helping me out. I tried almost all possibilities, but its inserting only ONE record in DB! I have 121 records in the Internal Table.

Hi Sathya,

I tried the piece of code that you had suggested. Unlike other options that have been mentioned here, this code is inserting more than one record. But its stopping after inserting 47 records. Its not inserting all the 121 records.

I tried debugging, but unfortunately am not able to see the records being filled in the DB table in debug mode!

Where am I going wrong?

Thank you,

Warm regards,

Deepak

Read only

Former Member
0 Likes
3,555

clear z4968_factuur

After Append statement.

or

first check structure of both.

Regards,

Swarup