如何使用多个非强制性输入字段进行搜索?

时间:2019-05-20 10:17:46

标签: sap abap

我想问一下如何在具有多个不需要输入字段的选择屏幕中进行基本搜索过滤。

我尝试通过使用多个IF语句后跟WHERE子句来实现此目的,这解决了我当前的问题,但如果我仅使用少量输入(目前为2个,“ ID”和“ Number”),则它不能完全正确,代码不会太长,但是,如果代码超过10个左右,以这种方式进行操作会感到错误

到目前为止,我尝试的操作大致是这样的:

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.

期望的结果是通过没有输入或多个非强制性输入来正确执行搜索,而在输入数量很多的情况下不必编写很长的代码。

3 个答案:

答案 0 :(得分:2)

当您有多个条件时,可以在WHERE子句中使用IN运算符。

首先,您需要为每个参数定义一个选择表,并必须将其填写或保留为空。

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 .

您必须对要查询的每个参数执行此操作。您的查询将如下所示:

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

答案 1 :(得分:1)

您可以将输入字段定义为SELECT-OPTIONS(使用可选的NO INTERVALS NO-EXTENSION来模仿PARAMETERS的外观)。然后只需在IN子句中使用WHERE运算符:

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).

答案 2 :(得分:-1)

这种在何处连接条件的方法呢?

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).