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

Read table - identify no match

Former Member
0 Likes
1,274

Hi

I have got a read table statement with a where clause. The thing is that I have do one thing if there is exactly one match and another thing if there is not match.

As I understand the sy-subrc returns 0 if the query was executed correct. Due to this I will get a sy-subrc=0 if there is no match. So how do I check weather there is one or no matches in the read table statement?

Regards

Torben

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
993

To Check if there is exactly one match or no mat at all:

read table itab with key field = <value>.

if sy-subrc = 0.

write:/ 'One match'.

else.

write:/ 'No match'.

endif.

Check for Not even one match/ atleast one match

loop at itab where field = <value>.

exit.

endloop.

if sy-subrc = 0.

write:/ 'Atleast one match'.

else.

write:/ 'No matches at all'.

endif.

Regards,

Ravi

Hi

I have got a read table statement with a where clause. The thing is that I have do one thing if there is exactly one match and another thing if there is not match.

As I understand the sy-subrc returns 0 if the query was executed correct. Due to this I will get a sy-subrc=0 if there is no match. So how do I check weather there is one or no matches in the read table statement?

Regards

Torben

4 REPLIES 4
Read only

Former Member
0 Likes
993

Hi,

The sy-subrc will be greater than zero if there is no entry for the corresponding match..

Example

-


READ TABLE T_MARA WITH KEY MATNR = '12121'.

IF SY-SUBRC = 0.

WRITE: / 'Entry found for the corresponding material'.

ELSE.

WRITE: / 'Entry NOT found'.

ENDIF.

THanks,

Naren

Read only

Former Member
0 Likes
993

Hi,

You can code like this:

READ TABLE T_MARA WITH KEY MATNR = '12121'.

IF SY-SUBRC = 0.

WRITE: / 'One Entry Found'.

ELSEIF SY-SUBRC =1.

WRITE: / '2 ENTRIES FOUND'.

ELSEIF SY-SUBRC =2.

WRITE: / '3 ENTRIES FOUND'.

ELSEIF SY-SUBRC =3.

WRITE: / 'MORE THAN 3 ENTRIES FOUND'.

ELSE

WRITE: / 'NO ENTRIES FOUND'.

ENDIF.

iF HELPS PLS MARK POINTS.

THANKS.

radha.

Read only

Former Member
0 Likes
994

To Check if there is exactly one match or no mat at all:

read table itab with key field = <value>.

if sy-subrc = 0.

write:/ 'One match'.

else.

write:/ 'No match'.

endif.

Check for Not even one match/ atleast one match

loop at itab where field = <value>.

exit.

endloop.

if sy-subrc = 0.

write:/ 'Atleast one match'.

else.

write:/ 'No matches at all'.

endif.

Regards,

Ravi

Read only

Former Member
0 Likes
993

Try:


DATA: next    LIKE sy-tabix,
      counter TYPE i,
      old_key LIKE itab-f1.

SORT itab BY f1.

READ TABLE itab WITH KEY
  f1 = 'whatever'
  BINARY SEARCH.

IF sy-subrc <> 0.
  WRITE 'No matches'.
ELSE.
  next    = sy-tabix + 1.
  old_key = itab-f1.
  READ TABLE itab INDEX next.
  IF sy-subrc = 0.
    IF itab-f1 = old_key.
      WRITE 'More than one match'.
    ELSE.
      WRITE 'Exactly one match'.
    ENDIF.
  ELSE.
    WRITE 'Exactly one match'.
  ENDIF.
ENDIF.

Rob