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

Internal Table Column Processing

Former Member
0 Likes
1,578

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
Read only

former_member183073
Active Participant
0 Likes
1,536

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
Read only

Former Member
0 Likes
1,536

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

Read only

former_member208149
Participant
0 Likes
1,536

Hi,

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

Regards.

Read only

Arun_Prabhu_K
Active Contributor
0 Likes
1,536

Hello Udaya Bhaskar.

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

Regards.

Read only

Former Member
0 Likes
1,536

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

Read only

former_member209120
Active Contributor
0 Likes
1,536

Hi  Udaya Bhaskar Perecharla,

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

Read only

Former Member
0 Likes
1,536

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.

Read only

Former Member
0 Likes
1,536

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.

Read only

former_member183073
Active Participant
0 Likes
1,537

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.

Read only

0 Likes
1,536

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.

Read only

0 Likes
1,536

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

Read only

Former Member
0 Likes
1,536

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.


Read only

Former Member
0 Likes
1,536

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

Read only

0 Likes
1,536

This message was moderated.