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: 

copying contents of one column in an ITAB to another with out loop.

Former Member
0 Kudos

Hi,

i need to copy contents of one column to another column in another internal table.

for eg.

internal table 1.

-


number____name___code

internal table 2.

-


code.

how to copy contents of column 'code' to internal table 2 WITH OUT A LOOP.

your help would be appreciated.

Thanks,

ravi.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

john,

here is the requirement.

i will have more than 50,000+ entries in internal table1.

it would be like this.

number(key)-name-code

000001-----abcd-C1

000002-----bcda-C1

000003-----bcda-C1

000004-----abcd-C2.

internal table 2(which will be populated from db table based on internal table 1)

code---link

C1-----L07

c2-----L09

now, i need to fetch values from another table whose primary key is 'code'.

if i use, SELECT....FOR ALL ENTRIES IN internal table1...

(OR) select..endselect in a loop...

there will be multiple reads on the data base

I would like to avoid that.

Thanks,

ravi.

8 REPLIES 8

Former Member
0 Kudos

Hi

you can't do it without reading the first table.

If the two tables were same you could use:

ITAB2[] = ITAB1[].

But not here.

You have to read the first table by LOOP statament or READ statament.

Max

Former Member
0 Kudos

It would be helpful if we understood the requirement. Why exactly do you not want to do a loop? Are you already inside a loop and don't want to affect it?

There is no one command that will do what you want. On top of that, how do you match the rows? Is it one-for-one and you just want to replace table 1 item 1 with table 2 item 1, for all items?

Either way, there will have to be some sort of loop process to update each row.

You can do a do loop as below (assuming the same number of entries in both tables):

Data: w_ix like sy-tabix.

clear sy-subrc.

do.

add 1 to w_ix.

read table itab2 index w_ix.

itab1-column_to_replace = itab2-new_data.

modify table itab1 transporting column_to_replace index w_ix.

enddo.

But of course, this is a loop.

Good luck,

John

Former Member
0 Kudos

john,

here is the requirement.

i will have more than 50,000+ entries in internal table1.

it would be like this.

number(key)-name-code

000001-----abcd-C1

000002-----bcda-C1

000003-----bcda-C1

000004-----abcd-C2.

internal table 2(which will be populated from db table based on internal table 1)

code---link

C1-----L07

c2-----L09

now, i need to fetch values from another table whose primary key is 'code'.

if i use, SELECT....FOR ALL ENTRIES IN internal table1...

(OR) select..endselect in a loop...

there will be multiple reads on the data base

I would like to avoid that.

Thanks,

ravi.

0 Kudos

Ravi - you just have to create a second internal table from the first one with the distinct values of 'code' in it. Then do a select for all entries on whatever table the code is a key for and put the results into a third internal table. You can the loop at the first table and do a binary read on the third table to get your translation. For better efficiency, only do the read when the code changes.

Rob

0 Kudos

Hi Ravi,

Depending on the number of entries in your code db table, you can either COLLECT the unique keys or select ALL of the rows. If you're going to need most of them anyway, you might as well just get them all. The key is to minimize database I/O. If you want to be really db efficient, you can create a unique list of keys by spinning through table 1 building the key list for the code table.

ie)

.loop at i_table1.

. i_table2_key-key = i_table1-code.

. collect i_table2_key.

.endloop.

.select code from db

. for all entries in table2_key

. into table i_table2.

.SORT I_TABLE2.

.loop at i_table1.

. if i_table1-code ne i_table2-code.

. read i_table2 with key code = i_table1-code.

. if sy-subrc ne 0.

. clear i_table2_code-values. (You don't want to clear the key or you will keep reading the blank).

. endif.

.endif.

  • Your code goes here...

.endloop.

0 Kudos

Hi

You can try to do this:

ITAB3[] = ITAB1[].

SORT ITAB3 BY CODE.

DELETE ADJACENT DUPLICATES FROM ITAB2 COMPARING CODE.

Now in ITAB3 you'll have record where field CODE isn't duplicated.

SELECT * FROM <TABLE> INTO TABLE .....

FOR ALL ENTRIES IN ITAB3 WHERE CODE = ITAB3-CODE.

or

DATA: BEGIN OF ITAB3 OCCURS 1,

CODE ..

END OF ITAB3.

LOOP AT ITAB1.

ITAB3-CODE = ITAB1-CODE.

COLLECT ITAB3.

ENDLOOP.

SELECT * FROM <TABLE> INTO TABLE .....

FOR ALL ENTRIES IN ITAB3 WHERE CODE = ITAB3-CODE.

But if ITAB1 have many records I don't know which solution is better for performance.

Try

Max

0 Kudos

Hi Max,

I can tell you that the loop with the collect absolutely screams. It will do 100k records in less than a second!

FYI,

John

Former Member
0 Kudos

Hi,

thanks all for your answers. i appreciate your response.

i tried another approach.

append lines of itab1 to itab2.

sort itab2.

delete adjacent duplicates from itab2.

select......for all entries in itab2.......

Thanks,

ravi.