10-01-2013 7:04 AM
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
10-01-2013 2:12 PM
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.
10-01-2013 7:10 AM
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
10-01-2013 7:13 AM
Hi,
No there is no other alternative than LOOPing at the itab1.
Regards.
10-01-2013 7:15 AM
Hello Udaya Bhaskar.
Code your business logic to fetch product names directly into ITAB2 instead of ITAB1
Regards.
10-01-2013 8:04 AM
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
10-01-2013 8:23 AM
Hi Udaya Bhaskar Perecharla,
I think there is no other way other than loop....
10-01-2013 12:27 PM
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.
10-01-2013 1:48 PM
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.
10-01-2013 2:12 PM
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.
10-01-2013 2:55 PM
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.
10-01-2013 5:29 PM
not sure why you felt that I am not clear with the requirement, i have proposed a solution.
10-01-2013 3:28 PM
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.
10-02-2013 5:32 AM
Thanks everyone for your quick response and sharing ideas on my query. I have got some optimum approach in this thread.
10-02-2013 7:14 AM