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

move-corresponding

Former Member
0 Likes
929

i have a small pgm where there are two int tables. i give some data in first int tab and some other fields in the other tab. the field 'name' is same in both the itabs. so i am using the 'move-corresponding' option. but in the o/p, total records are shown as four whereas actually there should be only two. pls check and advise.

i give the pgm here:

REPORT ZDEMO3 .

data: begin of student occurs 0,

name(20) type c,

rollno type i,

percentage type p decimals 2,

end of student.

data : begin of student_details occurs 0,

name(20) type c,

fathers_name(20) type c,

address(45) type c,

Phone_no type i,

end of student_details.

student-name = 'Bhargav.M.D.'.

student-rollno = 10076036.

student-percentage = 80.

append student.

clear student.

student-name = 'Nishchal.T.K'.

student-rollno = 45782169.

student-percentage = 60.

append student.

clear student.

student_details-fathers_name = 'S/O Dayanda.M.D'.

student_details-address = 'Behind Mallige Thota, Magadi Road,Bangalore'.

student_details-phone_no = 23105901.

append student_details.

clear student_details.

student_details-fathers_name = 'S/O Kirsna Murty.T.K'.

student_details-address = 'K.R.Extension, Tiptur'.

student_details-phone_no = 252865.

append student_details.

clear student_details.

loop at student.

move-corresponding student to student_details.

append student_details.

clear student.

endloop.

loop at student.

write: / student-name, student-rollno, student-percentage.

endloop.

skip 2.

loop at student_details.

write : / student_details-name, student_details-fathers_name,

student_details-address, student_details-phone_no.

endloop.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
892

here is the modified code.

REPORT ZDEMO3 .

data: begin of student occurs 0,
name(20) type c,
rollno type i,
percentage type p decimals 2,
end of student.

data : begin of student_details occurs 0,
name(20) type c,
fathers_name(20) type c,
address(45) type c,
Phone_no type i,
end of student_details.

student-name = 'Bhargav.M.D.'.
student-rollno = 10076036.
student-percentage = 80.
append student.
clear student.

student-name = 'Nishchal.T.K'.
student-rollno = 45782169.
student-percentage = 60.
append student.
clear student.

<b>*--these 2 below append STUDENT_DETAILS are not needed as we  are appending in the below LOOP at STUDENT.</b>*student_details-fathers_name = 'S/O Dayanda.M.D'.
*student_details-address = 'Behind Mallige Thota, Magadi *Road,Bangalore'.
*student_details-phone_no = 23105901.
*append student_details.
*clear student_details.

*student_details-fathers_name = 'S/O Kirsna Murty.T.K'.
*student_details-address = 'K.R.Extension, Tiptur'.
*student_details-phone_no = 252865.
*append student_details.
*clear student_details.

<b>loop at student.
move-corresponding student to student_details.
  if sy-tabix = 1.
student_details-fathers_name = 'S/O Dayanda.M.D'.
student_details-address = 'Behind Mallige Thota, Magadi Road,Bangalore'.
student_details-phone_no = 23105901.
   elseif sy-tabix = 2.
student_details-fathers_name = 'S/O Kirsna Murty.T.K'.
student_details-address = 'K.R.Extension, Tiptur'.
student_details-phone_no = 252865.
  endif.
append student_details.
endloop.</b>
loop at student.
write: / student-name, student-rollno, student-percentage.
endloop.

skip 2.

loop at student_details.
write : / student_details-name, student_details-fathers_name,
student_details-address, student_details-phone_no.
endloop.

8 REPLIES 8
Read only

Former Member
0 Likes
892

<b>sort student.

loop at student_details.

read table student with key name = student-name

binary search.

student_details-name = student-name.

<b>modify</b> student_details.

endloop.</b>

Read only

Former Member
0 Likes
892

loop at student.

move-corresponding student to student_details.

<b>append student_details.</b>

clear student.

endloop.

as you already populated STUDENT_DETAILS before the above LOOP and again in this LOOP AT STUDENT, you are adding all the records of STUDENT TO STUDENT_DETAILS.

thats why 2+ 2 records are appearing for STUDENT_DETAILS .

regards

sriaknth

Read only

Former Member
0 Likes
892

The problem in the code is that you are appending the data into student_details rather than modifying it.

loop at student.

read table student_details with key name = student-name.

ltabix = sy-tabix.

move-corresponding student to student_details.

*append student_details.

MODIFY student_details index ltabix.

clear student.

endloop.

Read only

suresh_datti
Active Contributor
0 Likes
892

replace APPEND with MODIFY in your second loop.

~Suresh

Read only

Former Member
0 Likes
893

here is the modified code.

REPORT ZDEMO3 .

data: begin of student occurs 0,
name(20) type c,
rollno type i,
percentage type p decimals 2,
end of student.

data : begin of student_details occurs 0,
name(20) type c,
fathers_name(20) type c,
address(45) type c,
Phone_no type i,
end of student_details.

student-name = 'Bhargav.M.D.'.
student-rollno = 10076036.
student-percentage = 80.
append student.
clear student.

student-name = 'Nishchal.T.K'.
student-rollno = 45782169.
student-percentage = 60.
append student.
clear student.

<b>*--these 2 below append STUDENT_DETAILS are not needed as we  are appending in the below LOOP at STUDENT.</b>*student_details-fathers_name = 'S/O Dayanda.M.D'.
*student_details-address = 'Behind Mallige Thota, Magadi *Road,Bangalore'.
*student_details-phone_no = 23105901.
*append student_details.
*clear student_details.

*student_details-fathers_name = 'S/O Kirsna Murty.T.K'.
*student_details-address = 'K.R.Extension, Tiptur'.
*student_details-phone_no = 252865.
*append student_details.
*clear student_details.

<b>loop at student.
move-corresponding student to student_details.
  if sy-tabix = 1.
student_details-fathers_name = 'S/O Dayanda.M.D'.
student_details-address = 'Behind Mallige Thota, Magadi Road,Bangalore'.
student_details-phone_no = 23105901.
   elseif sy-tabix = 2.
student_details-fathers_name = 'S/O Kirsna Murty.T.K'.
student_details-address = 'K.R.Extension, Tiptur'.
student_details-phone_no = 252865.
  endif.
append student_details.
endloop.</b>
loop at student.
write: / student-name, student-rollno, student-percentage.
endloop.

skip 2.

loop at student_details.
write : / student_details-name, student_details-fathers_name,
student_details-address, student_details-phone_no.
endloop.

Read only

Former Member
0 Likes
892

Hi ...U have to read the second internal table based on some key from first internal table and then use move-corresponding..currently ur appending 4 records..to understand clearly debug ur code..

Move-corresponding only moves the value to corresponding field of internal table it will consider new entry and append that many rows..so u have read or loop itab based on some condition..

Read only

Former Member
0 Likes
892

Hi,

1. Already you have 2 records in each internal table Student and Student_details.

in table student have below 2 name records

1. student-name = 'Bhargav.M.D.'.

2. student-name = 'Nishchal.T.K'.

in the student_details table have below 2 records

1. student_details-fathers_name = 'S/O Dayanda.M.D'.

2. student_details-fathers_name = 'S/O Kirsna Murty.T.K'.

2. Then after you append again 2 records into internal table student_details, becuase in the o/p it shows 4 records from internal table student_details.

now student_details having 4 records,Becuase you are appending the records from student table to student_detaisl table.(Append means add the records to internal table)

Reward if helpfull.

regards,

Subbarao

Read only

Former Member
0 Likes
892

Hi Satish,

Yes, you have populated with another two records, and therefore you would have four records instead of two. However, I would suggest you to define a primary key in both internal tables (structure), please see below:

data: begin of student occurs 0,

<b>id(3) type c,</b>

name(20) type c,

rollno type i,

percentage type p decimals 2,

end of student.

data : begin of student_details occurs 0,

<b>id(3) type c,</b>

name(20) type c,

fathers_name(20) type c,

address(45) type c,

Phone_no type i,

end of student_details.

<b>student-id = '001'.</b>

student-name = 'Bhargav.M.D.'.

student-rollno = 10076036.

student-percentage = 80.

append student.

clear student.

<b>student-id = '002'.</b>

student-name = 'Nishchal.T.K'.

student-rollno = 45782169.

student-percentage = 60.

append student.

clear student.

<b>student_details-id = '001'.</b>

student_details-fathers_name = 'S/O Dayanda.M.D'.

student_details-address = 'Behind Mallige Thota, Magadi Road,Bangalore'.

student_details-phone_no = 23105901.

append student_details.

clear student_details.

<b>student_details-id = '002'.</b>

student_details-fathers_name = 'S/O Kirsna Murty.T.K'.

student_details-address = 'K.R.Extension, Tiptur'.

student_details-phone_no = 252865.

append student_details.

clear student_details.

<b>sort student by name.

loop at student_details.

read table student

with key id = student_details-id

binary search.

move-corresponding student to student_details.

modify student_details index sy-tabix.

clear student.

endloop.</b>

loop at student.

write: / student-name, student-rollno, student-percentage.

endloop.

skip 2.

loop at student_details.

write : / student_details-name, student_details-fathers_name,

student_details-address, student_details-phone_no.

endloop.

Hope the above helps.

Cheers,

Patrick