Generally , while validating select-options we write the code as below.
*It is only partially correct , refer the codes below
select matnr from mara into lv_mara
where matnr in so_matnr.
if sy-subrc is not initial.
"error message
endif.
but above code , is wrong . Now suppose the user enters two material number , 0000000002 and
0000000045 where the 2nd one does not exist in the system .But sy-subrc will become 0 , and the error message will not be executed .
So , we have to consider the following codes that will help !!!
REPORT ztestr_14.
TABLES : mara.
SELECT-OPTIONS : so_matnr FOR mara-matnr.
TYPES : BEGIN OF ty_matnr,
matnr TYPE matnr,
END OF ty_matnr.
DATA : li_matnr TYPE STANDARD TABLE OF ty_matnr.
FIELD-SYMBOLS : <fls_matnr> TYPE ty_matnr.
IF so_matnr IS NOT INITIAL.
SELECT matnr FROM mara
INTO TABLE li_matnr
WHERE matnr IN so_matnr.
IF sy-subrc IS INITIAL.
SORT li_matnr ASCENDING BY matnr.
LOOP AT so_matnr.
READ TABLE li_matnr ASSIGNING <fls_matnr>
WITH KEY matnr = so_matnr-low
BINARY SEARCH.
IF sy-subrc IS NOT INITIAL.
PERFORM error_handling USING text-e01 so_matnr-low.
ENDIF.
ENDLOOP.
IF so_matnr-high IS NOT INITIAL.
READ TABLE li_matnr ASSIGNING <fls_matnr>
WITH KEY matnr = so_matnr-high
BINARY SEARCH.
IF sy-subrc IS NOT INITIAL.
PERFORM error_handling USING text-e01 so_matnr-high.
ENDIF.
ENDIF.
ELSE.
PERFORM error_handling USING text-e01 so_matnr-low.
ENDIF.
ENDIF.
form ERROR_HANDLING using p_text_e01 TYPE string
value type any.
DATA : lv_message TYPE string.
CONSTANTS : lc_plach1 TYPE char2 VALUE '&1'.
CLEAR : lv_message.
lv_message = p_text_e01." where text-e01 = Material number &1 does not exist
REPLACE lc_plach1 WITH value
INTO lv_message.
MESSAGE e000(zh) WITH lv_message.
endform. " ERROR_HANDLING