Application Development 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: 

Internal Table Column Processing

former_member184390
Participant
0 Kudos

Hi Friends,

I have a requirement where in i need to collate values in field1, field2 in a internal table in to a single field in another table.

Is there any other opton other than looping at itab1 and inserting the values of field1, field2 into itab2 one after the other.

Thank you.

Thanks,

Uday

For eg:

Itab1 has two columns Product1, Product2 and itab2 has one field Product.

M124, M126

M125, M127

M128, M129


Expected Result

-----------------------

iTAB2

M124

M126

M125

M127

M128

M129

1 ACCEPTED SOLUTION

former_member183073
Active Participant
0 Kudos

Hi Uday,

as you have ITAB2 field similar to the first field in ITAB1. For First column you can directly assign value of ITAB1 to ITAB2.

itab2[] = itab1[]. this copies the first column avoiding 1 append statement in code.

and then loop at itab1 and append the second column.

13 REPLIES 13

Former Member
0 Kudos

Hi.

Please try this logic.

Loop at Itab1 .

Itab2-Produc t= Itab1 -Product1.

Append itab2.

Itab2-Produc t= Itab1 –Product2.

Append itab2.

Endloop.

Regards.

JN

former_member208149
Participant
0 Kudos

Hi,

No there is no other alternative than LOOPing at the itab1.

Regards.

karun_prabhu
Active Contributor
0 Kudos

Hello Udaya Bhaskar.

     Code your business logic to fetch product names directly into ITAB2 instead of ITAB1

Regards.

former_member195402
Active Contributor
0 Kudos

Hi,

if you need the 1st internal table, too, then the only other way might be a selection from database.

SELECT ... INTO TABLE itab2 FOR ALL ENTRIES in ITAB1 WHERE product = itab1-product1 OR product = itab1-product2.

If OR doesn't work split SELECT into SELECT INTO TABLE itab1 FOR ALL ENTRIES in ITAB1 WHERE product1 ... and SELECT APPENDING TABLE itab1 FOR ALL ENTRIES in ITAB1 WHERE product2 ...

The LOOP and APPEND remains the easiest way to create itab2.

Regards,

Klaus

former_member209120
Active Contributor
0 Kudos

Hi  Udaya Bhaskar Perecharla,

I think there is no other way other than loop....

Former Member
0 Kudos

Hi Udaya,

Too Curious to know why you don't wish to use loop at internal table.

If there is Performance issue use parallel cursor method.

otherwise use read table.

But what is the problem with loop command Please Explain.

former_member339717
Active Participant
0 Kudos

DEAR UDAY,

TRY THIS PLEASE.

data: BEGIN OF itab1 OCCURS 0,

       col1 type char10,

       col2 type char10,

  END OF itab1.

  data: BEGIN OF itab2 OCCURS 0,

       product type matnr,

   END OF itab2.

   DATA: V_LINES TYPE I.

   DATA: COUNTER TYPE I.

   itab1-col1 = 'M124'.

   itab1-col2 = 'M126'.

   APPEND ITAB1.

   CLEAR: ITAB1.

   itab1-col1 = 'M125'.

   itab1-col2 = 'M127'.

   APPEND ITAB1.

   CLEAR: ITAB1.

     itab1-col1 = 'M128'.

   itab1-col2 = 'M129'.

   APPEND ITAB1.

   CLEAR: ITAB1.

   DESCRIBE TABLE ITAB1 LINES V_LINES.

DO V_LINES TIMES.

   COUNTER = COUNTER + 1.

   READ TABLE ITAB1 INDEX COUNTER.

   IF SY-SUBRC EQ 0.

     ITAB2-PRODUCT = ITAB1-COL1.

     APPEND ITAB2.

     CLEAR: ITAB2.

     ITAB2-PRODUCT = ITAB1-COL2.

     APPEND ITAB2.

     CLEAR: ITAB2.

   ENDIF.

   ENDDO.

   SORT ITAB2 BY PRODUCT.

   LOOP AT ITAB2.

   WRITE😕 ITAB2-PRODUCT.

   ENDLOOP.

former_member183073
Active Participant
0 Kudos

Hi Uday,

as you have ITAB2 field similar to the first field in ITAB1. For First column you can directly assign value of ITAB1 to ITAB2.

itab2[] = itab1[]. this copies the first column avoiding 1 append statement in code.

and then loop at itab1 and append the second column.

0 Kudos

Hi syed,

            I think the requirment is clear. The 2nd itab has just one coloum and he  wants to transfer two may be more than two coloumn value to a single coloum of single column of 2nd itab. ain't it.

0 Kudos

not sure why you felt that I am not clear with the requirement, i have proposed a solution.

former_member184569
Active Contributor
0 Kudos

I dont think a loop can be avoided.

However you can reduce the number of lines with this logic.


 
FIELD-SYMBOLS <FS1> TYPE ANY.

  ITAB2[] = ITAB1[].
 
ASSIGN WA_ITAB1-FIELD2 TO <FS1>.


 
LOOP AT ITAB1 INTO WA_ITAB1.
   
APPEND <FS1> TO ITAB2.
 
ENDLOOP.


former_member184390
Participant
0 Kudos

Thanks everyone for your quick response and sharing ideas on my query. I have got  some optimum approach in this thread.

0 Kudos

This message was moderated.