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

help in populating ITAB

former_member206396
Active Participant
0 Likes
1,142

hai SDNs,

i have an internal table having, say 6 fields... In which some of the fields

are already populated. (say 4 fields).. now the question is i have to populate

the other fields too based on some condition.

condition ::

the other two fields are in some other table..

here i have to check two fields( conditions ) .. if both fields matches i have to

modify the first table populating these two fields..

otherwise.,,

i have to append the record with those two fields...all other fields should be

blank..

could any pls help me out

Thanking you

hai SDNs,

i have an internal table having, say 6 fields... In which some of the fields

are already populated. (say 4 fields).. now the question is i have to populate

the other fields too based on some condition.

condition ::

the other two fields are in some other table..

here i have to check two fields( conditions ) .. if both fields matches i have to

modify the first table populating these two fields..

otherwise.,,

i have to append the record with those two fields...all other fields should be

blank..

could any pls help me out

Thanking you

10 REPLIES 10
Read only

Former Member
0 Likes
1,110

1)spose itab1 have 6 field and u select 4 field from dbtab.

2)make an internal table itab2 with fields which u want to fill in internal table itab1 and a common field in internal table itab1 and internal table itab2.

3)<b>

sort itab1 .
loop at itab.
read table itab2 binary search with key field1 = itab1-field1.
 itab1-field5 = itab2-field2.
 itab2-field6 = itab2-field3.
modify itab1.
endloop.

</b>

Read only

0 Likes
1,110

hai kishan,

thank u for reply..

BUT

i am able to modify but i am unable to append the records..

help with coding if possible, because i have tried all the ways..

Read only

0 Likes
1,110

try this way...

suppose u have interanl table itab2 with new 2 filed which u want to modify in itab1 for some condn . if cond not match u want to append these two fields into itab1.

just reverse it and try this way.

u have internal table with two fields .

now modify it by key by internal table itab1.

sort itab1 .

loop at itab2.

read table itab1 binary search with key field1 = itab2-field1.

itab2-field1 = itab1-field1.

itab2-field2 = itab1-field2.

itab2-field3 = itab1-field3.

itab2-field4 = itab1-field4.

modify itab2.

endloop.

Read only

former_member202020
Participant
0 Likes
1,110

Hi Rama,

the previous code can be modified a little.

sort itab1 .

loop at itab1.

" check the condition

read table itab2 with key field1 = itab1-field1

binary search.

if sy-subrc = 0.

itab1-field5 = itab2-field2.

itab1-field6 = itab2-field3.

modify itab1.

else.

itab1-field5 = itab2-field2.

itab1-field6 = itab2-field3.

append itab1.

endloop.

Read only

0 Likes
1,110

hey kishan,

i have tried this toooo..

but is going to infinite loop..

( read -


if sy-subrc = 0.

modify...

else

append...

)

i did this, even not working fine (infinte loop )

Read only

0 Likes
1,110

ya u r rite... definatly it goes to infinite loop..

try the way i suggest in 2nd post.

or

make  interanl table itab3.
sort itab1 .
loop at itab1.
" check the condition 
read table itab2 binary search with key field1 = itab1-field1.
if sy-subrc = 0.
itab3-field1 = itab1-field1.
itab3-field2 = itab1-field2.
itab3-field3 = itab1-field3.
itab3-field4 = itab1-field4.
itab3-field5 = itab2-field5.
itab3-field6 = itab2-field6.
append itab3.
else.
itab3-field5 = itab2-field2.
itab3-field6 = itab2-field3.
append itab3.
endloop.

Read only

0 Likes
1,110

Hi RamaKrishna,

You declare one more internal table with the same structure of looping one.

for ex:

ltab,looping tab ( condition table)

jtab, " table that u wnat to modify

ktab like jtab ,new table.

loop at ltab.

read table jtab with key .

if sy-subrc = 0.

move corresponding of jtab to ktab.

  • the field sthat u want to modify conditionally.

ktab-field1 = jtab-field1.

ktab-field2 = jtab-field2.

append ktab.

clear ktab.

else.

ktab-field1 = jtab-field1.

ktab-field2 = jtab-field2.

append ktab.

clear ktab.

endif.

Let me know if u have any queries.

Regards,

Kiran I

Read only

Former Member
0 Likes
1,110

Hi RamaKrishna,

You can do this using Modify and Update Statement. Check the Code below for better understanding.

MODIFY for All Internal Tables

Changes the contents of lines in any type of internal table.

Syntax

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f 2>...].

Copies the work area <wa> into the line of the internal table with the same table key as <wa>. You can use the TRANSPORTING addition to specify the exact components that you want to change.

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f 2>... WHERE <logexp>.

Copies the work area <wa> into the lines of the internal table for which the logical expression is true. The first operand in each comparison of the logical expression must be a component of the line structure.

and for Update;

UPDATE

Modifies lines in database tables.

Syntax

UPDATE <dbtab> SET <si> = <f>

|<si> = <s i> + <f>

|<si> = <s i> - <f> [WHERE <cond>].

Sets the value in <si> to <f>, increases it by <f>, or decreases it by <f> for all selected lines. The WHERE addition determines the lines that are updated. If you omit the WHERE addition, all lines are updated.

Syntax

UPDATE <dbtab> FROM <wa>.

UPDATE <dbtab> FROM TABLE <itab>.

Overwrites the line with the same primary key as <wa> with the contents of <wa>, or all lines with the same primary key as a line in the internal table <itab> with the corresponding line of itab. The work area <wa> or the lines of the table <itab> must have at least the same length and the same alignment as the line structure of the database table.

Hope this might help you.

Regards,

Prashanth

Read only

former_member206396
Active Participant
0 Likes
1,110

hai uma devi and kishan

both of urs code is not working.

kishan ur 2nd post is going to short dump

Read only

0 Likes
1,110

Hello Rama,

You can look at the code below to give you an idea:

types: begin of itab1,

f1(20) type c,

f2(20) type c,

f3(20) type c,

f4(20) type c,

f5 type i,

f6 type i,

end of itab1.

types: begin of itab2,

f1(20) type c,

f2(20) type c,

f3(20) type c,

f4(20) type c,

f5 type i,

f6 type i,

end of itab2.

data: it_itab1 type table of itab1 with header line,

it_itab2 type table of itab2 with header line.

*Assume that you have existing records in it_itab1.

loop at it_itab1.

read table it_itab2 with key f1 = it_itab1-f1

f2 = it_itab1-f2.

if sy-subrc = 0.

it_itab1-f5 = it_itab2-f5.

it_itab1-f6 = it_itab2-f6.

else.

move space to it_itab1-f1.

move space to it_itab1-f2.

move space to it_itab1-f3.

move space to it_itab1-f4.

it_itab1-f5 = it_itab2-f5.

it_itab1-f6 = it_itab2-f6.

endif.

modify it_itab1.

endloop.

Hope I got your question right...

*you can use field-symbols, hashed table or sorted table to make your code faster.

P.S. Please award points for useful answers.