08-30-2006 10:11 AM
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.
08-30-2006 10:20 AM
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.
08-30-2006 10:14 AM
<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>
08-30-2006 10:16 AM
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
08-30-2006 10:17 AM
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.
08-30-2006 10:18 AM
08-30-2006 10:20 AM
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.
08-30-2006 10:21 AM
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..
08-30-2006 10:25 AM
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
08-30-2006 10:42 AM
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