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

Basic ABAP question.

Former Member
0 Likes
809

Hi Experts,

I have to move data from one internal table itab_1 to itab_2 if a field by name posnr is empty in itab_1. posnr is of type c.

I did

loop itab_1.

if itab_1-posnr ne ' '.

move-corresponding itab_1 to itab_2.

endif.

endloop.

please check if this code is correct.

Thank you.

Regards,

Admir.

Points will be rewarded.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
792

Hi,

Instead of checking the condition inside the loop you can write like this

loop at itab_1 where posnr ne ' '.

itab_2 = itab_1.

append itab_2.

clear itab_2.

endloop.

performance wise it is good.

reward points if it is helpful

regards,

sangeetha.a

8 REPLIES 8
Read only

Former Member
0 Likes
792

field names should be matching in 2 tables.

By itab1-posnr ne '', you are checking if it is not empty.

Is that wat you wanna do?

If you want to check whether posnr is empty then,

you can use the condition <b>itab1-posnr is initial</b>

Read only

Former Member
0 Likes
792

Hi,

Try this code...Check NOT INITIAL instead of <> ' '.


loop itab_1.
if NOT itab_1-posnr IS INITIAL.
move-corresponding itab_1 to itab_2.
endif.
endloop.

Thanks,

Naren

Read only

0 Likes
792

you can also do like...

also its safe to move the fields field by field if the internal table is big rather than move-corresponding.

also instead of if condition you can include it in where clause of the loop statement itself.

Read only

Former Member
0 Likes
792

Are itab_1 and itab_2 similar in structure? If so simply do this.

itab_2[] = itab_1[].

DELETE itab_2 WHERE posnr = SPACE.

Read only

Former Member
0 Likes
793

Hi,

Instead of checking the condition inside the loop you can write like this

loop at itab_1 where posnr ne ' '.

itab_2 = itab_1.

append itab_2.

clear itab_2.

endloop.

performance wise it is good.

reward points if it is helpful

regards,

sangeetha.a

Read only

Former Member
0 Likes
792

Hi,

Put append statement.

loop at itab_1.

if itab_1-posnr ne ' '.

move-corresponding itab_1 to itab_2.

append itab2.

endif.

clear: itab2, itab1.

endloop.

Thnaks

Sandeep

Reward if helpful

Read only

Former Member
0 Likes
792

Hi Admir,

The code for your requirement is as follows:

data: itab1 type table of <...>,

itab2 type table of <....>.

data: wa_itab1 type <...>

start-of-selection.

loop at itab1 into wa_itab1.

if wa_itab1-posnr is INITIAL.

wa_itab2-<fieldname> = wa_itab1-<fieldname>

"Copy all the required fields in the above manner.

Append wa_itab2 to itab2.

endif.

endloop.

Note: Do not use move-coresponding, as it not considered efficient/good coding

Plz Reward Points if Helpful.

Read only

Former Member
0 Likes
792

hi admir,

loop itab_1.

if itab_1-posnr is not initial.

move itab_1-posnr to itab_2-posnr.

modify itab_2.

endif.

endloop.

if helpful reward some points.

with regards,

suresh babu aluri.