Application Development 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: 

Compare Select-Options against a Range

ezequieldiazpierri
Discoverer
0 Kudos

Hi all,

I have a Requirement that wants to exclude all Partner Numbers that aren't between 0010000000 and 0069999999. One simple way is to fill a range and compare all the values inside the Select-Options:

  APPEND INITIAL LINE TO lr_range ASSIGNING <fs_range>.
  <fs_range>-sign   = 'I'.
  <fs_range>-option = 'NB'.
  <fs_range>-low    = '0010000000'.
  <fs_range>-high   = '0069999999'.

  IF s_partnr IS NOT INITIAL.
    LOOP AT s_partnr[] ASSIGNING <fs_range>.


      IF <fs_range>-low  IS NOT INITIAL AND
         <fs_range>-low  IN lr_range[].
        pi_error = abap_true.
        EXIT.
      ENDIF.


      IF <fs_range>-high IS NOT INITIAL AND
         <fs_range>-high IN lr_range[].
        pi_error = abap_true.
        EXIT.
      ENDIF.


    ENDLOOP.
  ENDIF.

(The "<fs_range>-low IS NOT INITIAL" is for avoiding the comparison between empty and the range).

But this way I'm not taking advantage of all the logic the ranges have behind, meaning that if the user inputs "sign=I option=GT low=0010000008 high=empty" in the Select-Options, the requirement wouldn't get fulfulied since it would include values higher than 0069999999.

I tried the following:

    LOOP AT s_partnr[] ASSIGNING <fs_range>.

      IF <fs_range> IN lr_range[]. " With and without the brackets
        pi_error = abap_true.
        EXIT.
      ENDIF.

    ENDLOOP.

Didn't work. I thought this way the Select-Option values would be respected as a range, but the value that is being compared is the entire workarea (for example: IBT0000000009999999999).

Can you help me?

Thanks in advance!

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

Usually this requirement is achieved at the moment you want to use S_PARTNR (either in SELECT or in a conditional statement), and you add the other range:

SELECT COUNT(*) FROM ... 
WHERE partner IN s_partnr
AND partner IN lr_range ...
IF sy-subrc = 0. MESSAGE 'error your selection is wrong' TYPE 'E'. ENDIF.

Note that your example doesn't need a range, and can be written with the operator BETWEEN:

SELECT COUNT(*) FROM ... 
           WHERE partner IN s_partnr
             AND partner BETWEEN '0010000000' AND '0069999999' ...
4 REPLIES 4

former_member230215
Participant
0 Kudos

check this if it suits your requirement

data gv_partner TYPE bu_partner.
SELECT-OPTIONS : lr_range for gv_partner.
data lt_range LIKE TABLE OF lr_range.
START-OF-SELECTION.
APPEND INITIAL LINE TO lt_range ASSIGNING FIELD-SYMBOL(<fs_range>).
  <fs_range>-sign   = 'I'.
  <fs_range>-option = 'NB'.
  <fs_range>-low    = '0010000000'.
  <fs_range>-high   = '0069999999'.

  SELECT partner FROM but000 INto TABLE @data(lt_partner)
    where partner in @lr_range and partner in  @lt_range.

former_member230215
Participant
0 Kudos

or this

data gv_partner TYPE bu_partner.
data lt_range LIKE TABLE OF lr_range.
data lt_partner2 TYPE TABLE OF bu_partner.
SELECT-OPTIONS : lr_range for gv_partner.
START-OF-SELECTION.
APPEND INITIAL LINE TO lt_range ASSIGNING FIELD-SYMBOL(<fs_range>).
  <fs_range>-sign   = 'I'.
  <fs_range>-option = 'NB'.
  <fs_range>-low    = '0010000000'.
  <fs_range>-high   = '0069999999'.

  SELECT partner FROM but000 INto TABLE @data(lt_partner)
    where partner in @lr_range .

LOOP AT lt_partner INTO DATA(ls_partner) where partner in lt_range.
APPEND ls_partner-partner to lt_partner2.
ENDLOOP.

Sandra_Rossi
Active Contributor

Usually this requirement is achieved at the moment you want to use S_PARTNR (either in SELECT or in a conditional statement), and you add the other range:

SELECT COUNT(*) FROM ... 
WHERE partner IN s_partnr
AND partner IN lr_range ...
IF sy-subrc = 0. MESSAGE 'error your selection is wrong' TYPE 'E'. ENDIF.

Note that your example doesn't need a range, and can be written with the operator BETWEEN:

SELECT COUNT(*) FROM ... 
           WHERE partner IN s_partnr
             AND partner BETWEEN '0010000000' AND '0069999999' ...

0 Kudos

Why my proposal couldn't "display an error message at the moment they hit Execute"?

No, it's not the only solution, but it's the simplest one, from far.