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

Itab

Former Member
0 Likes
1,208

Hello experts,

I need to append lines of 1 itab to another itab.

For eg:

There are 2 records in 1st itab with same document number.

1st itab:

<b>DocN0.|Year|type|Delivery|

-


911687|2007|PRT|8066335|

911687|2007|PRT|8066336|</b>

But there is only 1 record in 2nd itab with that document number.

2nd itab:

<b>DocN0.|Year|type|Delivery|

-


911687|2007|PRT|................ |</b>

Now I have to append the two records of the 1st itab into this 2nd itab. I know how to append the first record to the 2nd itab. But I am unable to do for the second record.

Could anybody of you please help in writing the code for this.

Thanks a lot for the help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,110

Hi Dev,

Loop at 1st itab.

read table 2nd itab with key docno = 1st itab-DocNo.

2nd itab-Delivery = ist itab- Delivery.

Modify 2nd Itab index Sy-tabix.

Endloop.

Hope this helps

14 REPLIES 14
Read only

Former Member
0 Likes
1,110

just write..

itab2[] = itab1[].

Thanks,

Santosh

Read only

Former Member
0 Likes
1,111

Hi Dev,

Loop at 1st itab.

read table 2nd itab with key docno = 1st itab-DocNo.

2nd itab-Delivery = ist itab- Delivery.

Modify 2nd Itab index Sy-tabix.

Endloop.

Hope this helps

Read only

0 Likes
1,110

thnx for the replies. I hve tried those tips. It didn't work.

Actually there are more records in these two itabs.

I'm giving the 2 itabs details once again for clear idea.

1st itab:

<b>DocN0.|Year|type|Delivery|

-


911685|2007|PRT|8066322|

911687|2007|PRT|8066335|

911687|2007|PRT|8066336|</b>

But there is only 1 record in 2nd itab with that document number.

2nd itab:

<b>DocN0.|Year|type|Delivery|

-


911684|2007|PRT|8055874|

911685|2007|PRT|8055896|

911686|2007|PRT|8056890|

911687|2007|PRT|............. |</b>

Now I have to append the 2 lines of the 1st itab to the 2nd itab when there is only 1 record with the DocNo. in that.

Sorry for not giving the clear details. Hope it is clear now.

Please help me.

Thanks.

Read only

0 Likes
1,110

Please check if this helps you:

loop at itab1 into wa1.

    read table itab2 into wa2 
      with key docno = wa1-docno
               year  = wa1-year
               type  = wa1-type
               del   = wa1-del.
    if sy-subrc ne 0.
       append wa1 to wa2.
    endif.

endloop.

Regards

Eswar

Read only

Former Member
0 Likes
1,110

Hi,

Check this..

DATA: LV_TABIX TYPE SYTABIX.

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY

DOCNO = ITAB1-DOCNO

YEAR = ITAB1-YEAR

TYPE = ITAB1-TYPE

DELIVERY = ITAB1-DELIVERY.

  • If record found then update the record.

IF SY-SUBRC = 0.

LV_TABIX = SY-TABIX.

ITAB2-DELIVERY = ITAB1-DELIVERY.

MODIFY ITAB2 INDEX LV_TABIX.

  • If not found then append the record.

ELSE.

MOVE ITAB1 TO ITAB2.

APPEND ITAB2.

ENDIF.

ENDLOOP.

Thanks,

Naren

Read only

0 Likes
1,110

Naren,

I guess your code does the same work as my code.

Though am not sure when the record is found in ITAB2 including the delivery number why do we need to update???

Regards

Eswar

Read only

0 Likes
1,110

Thanks all for your kind responses.

but there is a problem. I am very <b>sorry</b> that I forgot to mention there are some additional fields in the 2nd itab.

That's the reason when appending the records, its messing up the itab.

the actual 2nd itab format is:

-


<b>DocN0.|Year|type|Customer|Amount|Doc.Date|Delivery|

-


911684|2007|PRT|90012378|1000.00|20071010|8055874|

911685|2007|PRT|90018348|2000.00|20071009|8055896|

911686|2007|PRT|90019384|3000.00|20070203|8056890|

911687|2007|PRT|90037438|5000.00|20070207|............. |</b>

could you please tell me what changes to the code?

Once again sorry for misleading.

Thanks.

Read only

0 Likes
1,110

sorry i am bit confused on your replies though i am giving some coding it may work just check it.

sort itab1 by docno.

sort itab2 by docno.

loop at itab1.

loop at itab2 where docno = itab1-docno.

if itab2-year ne itab1-year or itab2-type ne itab1-type or itab2-deliv ne itab1-deliv.

move-corresponding itab1 to itab2.

append itab2.

endif.

endloop.

endloop.

regards

shiba dutta

Read only

ferry_lianto
Active Contributor
0 Likes
1,110

Hi,

Please try this and declare itab3 like itab1/itab2.


sort: itab1, itab2.

loop at itab2.
  loop at itab1 where docno = itab2-docno.
      move-coressponding itab1 to itab3.
     append itab3.      
  endloop.
endloop.

itab2[] = itab3[].

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,110

Hi Eswar,

You are correct...Modifying is not requried..I am kind of little confused about the requirement..

This can be another solution according to the data given by dev..

If both the structures are same..

APPEND LINES OF ITAB1 TO ITAB2.

SORT ITAB2.

DELETE ADJACENT DUPLICATES FROM ITAB2.

Thanks,

Naren

Read only

Former Member
0 Likes
1,110

Hi,

I believe Ferry's reply will work in this scenario..

Declare another internal table which is like itab2.

DATA: ITAB3 LIKE ITAB2 OCCURS 0 WITH HEADER LINE.

LOOP AT ITAB2.

MOVE-CORRESPONDING ITAB2 TO ITAB3.

LOOP AT ITAB1 WHERE DOCNO = ITAB2-DOCNO.

MOVE-CORRESPONDING ITAB1 TO ITAB3.

APPEND ITAB3.

ENDLOOP.

ENDLOOP.

Thanks,

Naren

Read only

0 Likes
1,110

Thanks ALL. Ferry that worked. But it's deleting the itab2 records which are extra to itab. If you see my example, i have extra records in itab2. i wanna keep them as it is.

Can you please suggest.

Thanks.

Read only

Former Member
0 Likes
1,110

Hi,

Try this....

DATA: ITAB3 LIKE ITAB2 OCCURS 0 WITH HEADER LINE.

LOOP AT ITAB2.

MOVE-CORRESPONDING ITAB2 TO ITAB3.

LOOP AT ITAB1 WHERE DOCNO = ITAB2-DOCNO.

MOVE-CORRESPONDING ITAB1 TO ITAB3.

APPEND ITAB3.

ENDLOOP.

<b>IF SY-SUBRC <> 0.

APPEND ITAB3.

ENDIF.</b>

ENDLOOP.

Thanks,

Naren

Read only

Former Member
0 Likes
1,110

hi,

instead of APPEND , use MODIFY.

this wont delete records in 2nd itab.

REGARDS,

Bijal