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

How to Insert data from Table A to Table B

Former Member
0 Likes
1,468

Hi,

I have to insert data from Table A into Table B.

Table A is having 100 records and table B is having 10000 records.

Condition is if data in Table A is not in table B then I have to insert into Table B or else I have to skip that.

Can anyone help me with sample code.

Thanks in advance and points are awarded for usefull answers.

Thanks,

Kumar.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,268

THere are a couple ways of doing this. Here is one simple way. Of course, this is assuming that the tables have exactly the same structure

data: itaba type table of ztaba with header line.
data: itabb type table of ztabb with header line.

select * into table itaba from ztaba.
select * into table itabb from ztabb.

loop at itaba.

read table itabb with key fld1 = itaba-fld1
                          fld2 = itaba-fld2.
if sy-subrc <> 0.
 insert ztabb from itaba.
endif.

endloop.

Regards,

Rich Heilman

Hi,

I have to insert data from Table A into Table B.

Table A is having 100 records and table B is having 10000 records.

Condition is if data in Table A is not in table B then I have to insert into Table B or else I have to skip that.

Can anyone help me with sample code.

Thanks in advance and points are awarded for usefull answers.

Thanks,

Kumar.

5 REPLIES 5
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,269

THere are a couple ways of doing this. Here is one simple way. Of course, this is assuming that the tables have exactly the same structure

data: itaba type table of ztaba with header line.
data: itabb type table of ztabb with header line.

select * into table itaba from ztaba.
select * into table itabb from ztabb.

loop at itaba.

read table itabb with key fld1 = itaba-fld1
                          fld2 = itaba-fld2.
if sy-subrc <> 0.
 insert ztabb from itaba.
endif.

endloop.

Regards,

Rich Heilman

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,268

Hello Kumar

Assuming that tables A and B have the same structure you can use the same logic as change documents are prepared. Assuming both of your itabs are of structure struc_a. Then define the following type:

TYPES: BEGIN OF ty_s_itab_di.  
INCLUDE TYPE struc_a.
TYPES: CHIND  TYPE bu_chind.
TYPES: END OF ty_s_itab_di.
TYPES: ty_t_itab_di  TYPE STANDARD TABLE OF ty_s_itab_di                      
WITH DEFAULT KEY.
DATA:    gt_itab_old  TYPE ty_t_itab_di,
gt_itab_new TYPE ty_t_itab_di.

  • Fill itabs gt_Itab_old with the corresponding data of itab1 and gt_itab_new with the corresponding data of itab2.

*

  • Very important: sort you itabs either by all key fields or by all fields.

*

  • Call function <b>CHANGEDOCUMENT_PREPARE_TABLES</b> with the following parameters:

- CHECK_INDICATOR = ' '

- TABLE_NEW = gt_Itab_new

- TABLE_OLD = gt_itab_old

The function module will remove identical lines from both itabs. New entries in gt_itab_New will have CHIND = 'I' and deleted entries in gt_itab_old will have CHIND = 'D'. Modified entries are indicated by CHIND = 'U'.

Read the documentation of the function module and play around with it. You will see that this a quite easy yet powerful approach for comparing itabs.

Regards

Uwe

Read only

Former Member
0 Likes
1,268

Hello Kumar,

Not sure of the details of table A & B.

If they have the same structure.

Try - INSERT LINES OF itabA into itabB.

Then you could use DELETE ADJACENT DUPLICATES FROM itabB.

You may be able to find other options from HELP for INSERT.

Greg Kern

Read only

shishupalreddy
Active Contributor
0 Likes
1,268

Hi Kumar,

You can go with the solution provided by Rich if the two tables are related .

or You can choose the follwing.

Assume ITAB JTAB having the data .

use the sample code

APPEND LINES OF ITAB[] TO JTAB[].

SORT jtab.

delete adjacent duplicates from jtab.

Hope this will help you.

regards,

Read only

0 Likes
1,268

Hi Kumar,

May the following code help u.

Essentials before using:

the two itabs should be sorted.

DATA: bkpf_index LIKE sy-tabix,

bseg_index LIKE sy-tabix.

CLEAR: bkpf_reads,

bseg_reads.

SORT: bkpf_tab BY bukrs belnr gjahr,

bseg_tab BY bukrs belnr gjahr.

bseg_index = 1.

LOOP AT bkpf_tab

INTO bkpf_lin.

bkpf_reads = bkpf_reads + 1.

LOOP AT bseg_tab INTO bseg_lin FROM bseg_index.

IF bseg_lin-bukrs <> bkpf_lin-bukrs OR

bseg_lin-belnr <> bkpf_lin-belnr OR

bseg_lin-gjahr <> bkpf_lin-gjahr.

bseg_index = sy-tabix.

EXIT.

ELSE.

bseg_reads = bseg_reads + 1.

ENDIF.

ENDLOOP.

ENDLOOP.