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

question on itabs

Former Member
0 Likes
918

friends i have scenario in which

i have to make sure there are no two entries in itab2 ie.

for every name entry in itab1 ,

check that a corresponding UNIQUE entry for that name exists in itab2. ie.

if itab1 has an entry john in name field, then itab should have exactly one entry as john in name entry.

not zero not 2, but exactly one.

i thought of following code. does any one have a better idea, with read or anything else.

count = 0.

loop at itab1.

loop at itab2 where name EQ itab1-name.

count = count +1.

endloop.

endloop.

if count >1.

write:/ ' error'.

endif.

9 REPLIES 9
Read only

Former Member
0 Likes
896

Hi Saritha,

i Find thats fine.

count = 0.

loop at itab1.

loop at itab2 where name EQ itab1-name.

count = count +1.

if count >1.

count = 0.

write:/ ' error'.

exit.

endif.

endloop.

endloop.

Try this and reward points if helpful.

Regards,

Ravi G

Read only

Former Member
0 Likes
896

Hi,

YOu can try this way...

Loop at ITAB1.

Read table ITAB2 where name = ITAB1-NAME.

if sy-subrc NE 0.

write:\ ' Error'.

Endif.

Endloop.

Regards,

sachin

Read only

anversha_s
Active Contributor
0 Likes
896

hi,

chk this. will improve perfromance.

count = 0.
clear count.
loop at itab1.
loop at itab2 where name EQ itab1-name.
count = count +1.
<b>if count >1.
exit.
endif.</b> 
endloop.
endloop.

if count >1.
write:/ ' error'.
endif.

Rgds

Anver

Read only

Former Member
0 Likes
896

hi saritha,

one small change

count = 0.

loop at itab1.

loop at itab2 where name EQ itab1-name.

count = count +1.

endloop.

endloop.

if count >1 and count = 0. " (u said know ther shuld be 1 not 0 & 2 )

write:/ ' error'.

endif.

With regards,

S.Barani

Read only

dev_parbutteea
Active Contributor
0 Likes
896

sort itab2 by name

sort itab1 by name

loop at itab1 into wa_area1.

read table itab2 into wa_area2 where name = wa_area1-name binary search.

clear v_index.

move sy-tabix to V_index.

if sy_subrc = 0. " i.e. 1 record exists.

-- read table itab2 into wa_area2 index v_index.

-


if sy_subrc = 0. " i.e 2nd record exists.

-


<b>error msg or watever</b>

-


else. " i.e. only 1 record exists.

-


<b>do processing</b>

-


endif.

else. " i.e. no record exists.

--<b>error msg or watever</b>

endif.

Regards,

Sooness.

Read only

0 Likes
896

hi, there's a small error:

.

.

if sy_subrc = 0. " i.e. 1 record exists.

V_index2 = V_index + 1.

-- read table itab2 into wa_area2 index <b>v_index2</b>.

-


if sy_subrc = 0. " i.e 2nd record exists.

.

.

Read only

Former Member
0 Likes
896

Hi,

You have to maintain a flag in itab1 for unique entry & make it X if there is corresponding 1 entry in itab2.

count = 0.

sort itab2 by name.

loop at itab1.

loop at itab2 where name EQ itab1-name.

count = count +1.

if count > 1.

exit.

endif.

endloop.

if count ne 1.

itab-flag = 'X'.

modify itab1.

endif.

clear : count,itab1,itab2.

endloop.

Thanks

Sandeep

Reward if helpful

Read only

Former Member
0 Likes
896

Hi Saritha,

The way u choose is ok. Let me tell u one more way.

You can use one more itab just for this purpose. Eg itab3.

then do a read to the itab3 with ur key, if u find Your record, delete that entry, then do a read again in itab3. At this stage u can decide the multiple occurance of the record.

Hope this will help u to avoid the looping.

Regards

Sarath

Read only

Former Member
0 Likes
896

hi saritha,

report xxx message-id msg1.

data:count type i value 0.

loop at itab1.

loop at itab2 where name EQ itab1-name.

if sy-subrc eq 0.

count = count +1.

endif.

else.

if count >1.

message e000 with ' error'.

endif.

endloop.

endloop.

if helpful reward some points.

with regards,

suresh babu aluri.