1
votes

I would like to ask how to do a basic search filtering in a selection screen that has multiple input fields which aren't required.

I tried to do it by using multiple IF statements followed by WHERE clauses which solves my current problem but its not fully correct, if i work with only a few inputs (2 at the moment, 'ID' and 'Number'), the code isn't too long, but if its over 10 or so it feel wrong to do it this way

What i tried so far was approximately like this :

IF lv_id IS INITIAL and lv_nr IS INITIAL.

     SELECT * from DBase INTO TABLE Local_Table.

ELSEIF lv_id IS NOT INITIAL AND lv_nr IS INITIAL.

     SELECT * from DBase INTO TABLE Local_Table WHERE ID = lv_nr.

ELSEIF lv_id IS INITIAL AND lv_nr IS NOT INITIAL.

     SELECT * from DBase INTO TABLE Local_Table WHERE Number = lv_nr.

ELSEIF lv_id IS NOT INITIAL AND lv_nr IS NOT INITIAL.

     SELECT * from DBase INTO TABLE Local_Table WHERE ID = lv_id AND Number = lv_nr.

The expected result is for the search to get executed correctly by having no input or multiple non-obligatory inputs, without having to write a very long code in case the number of inputs is high.

3

3 Answers

3
votes

you can use the IN Operator in your WHERE clause when you have multiple conditions.

First You need to define a Selection Table for every parameter and have to fill them or leave them empty.

types: begin of myselopt ,
       sign type char1 ,
       option type char2 ,
       low type ... (depends on the type you want select)
       high type ... ,
    end of myselopt .

types : t_selopt type table of myselopt .   

data: gt_selopt type t_selopt ,
      gt_selopt_2 type t_selopt_2 . #needs to be defined first 

if lv_id is not initial .
    insert value #( sign = 'I' option = 'EQ' low = lv_id ) into table gt_selopt .
endif . 

if lv_nr is not initial .
    insert value #( sign = 'I' option = 'EQ' low = lv_nr ) into table gt_selopt_2 .
endif .

You have to do this for every Parameter you want to query. And your query would look like this

select * from dbaste into table local_table where id in gt_selopt
                                        and number in gt_selopt_2 .
1
votes

You can define your input fields as SELECT-OPTIONS (with optional NO INTERVALS NO-EXTENSION to mimick the look of PARAMETERS). Then just use the IN operator in your WHERE clause:

REPORT.

DATA: your_ztable TYPE your_ztable.

SELECT-OPTIONS: s_id FOR your_ztable-id     NO INTERVALS NO-EXTENSION,
                s_nr FOR your_ztable-number NO INTERVALS NO-EXTENSION.

AT SELECTION-SCREEN.

  SELECT * FROM your_ztable
    WHERE id   IN @s_id
    AND number IN @s_nr
    INTO TABLE @DATA(local_table).
0
votes

What about this approach with concatenated where conditions?

DATA:
  lv_where TYPE string.


IF lv_id IS NOT INITIAL.
    CONCATENATE ' AND ID' space '=' space '"' lv_id '"' INTO lv_where.
ENDIF. 

IF lv_nr IS NOT INITIAL.
    CONCATENATE ' AND Number' space '=' space '"' lv_nr '"' INTO lv_where.
ENDIF.

IF lv_where IS NOT INITIAL.
    SHIFT lv_where By 5 PLACES. " to remove leading _AND_ (
ENDIF.

SELECT * from DBase INTO TABLE Local_Table WHERE (lv_where).