欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ABAP Report根据屏幕字段,进行动态SQL查询

程序员文章站 2022-05-18 14:23:02
...

在写Report查询时,想根据用户输入的值动态进行数据查询,示例如下

创建选择屏幕:

SELECTION-SCREEN BEGIN OF BLOCK BLOCK1 WITH FRAME TITLE TEXT-T01.

SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_ECTYPE FOR ZTVPLM0031-ZVPLM_ER_EC_TYPE NO INTERVALS.
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_ECID FOR ZTVPLM0031-ECR_ID NO INTERVALS.
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_PRJID FOR ZTVPLM0031-ZPROJCODE NO INTERVALS.
SELECTION-SCREEN SKIP 1.

PARAMETERS P_STATUS TYPE ZVPLM_CMPT MODIF ID STU.
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_LOB FOR ZTVPLM0031-ZVPLM_ER_LOB NO INTERVALS.
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_ADATE FOR ztvplm0031-ZVPLM_REL_DATE.
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS S_CREATE FOR ZTVPLM0031-CREATED_BY NO INTERVALS.
SELECTION-SCREEN SKIP 1.

PARAMETERS P_LIMIT TYPE I DEFAULT TEXT-T07.

SELECTION-SCREEN END OF BLOCK BLOCK1.

SELECTION-SCREEN BEGIN OF BLOCK BLOCK2 WITH FRAME TITLE TEXT-T02.
SELECTION-SCREEN SKIP 1.
PARAMETERS P_CHECK AS CHECKBOX USER-COMMAND CHECK.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK BLOCK2.

数据查询:

FORM frm_get_alv_data.
  DATA: lv_string TYPE string,

  IF s_ectype IS NOT INITIAL.   "define dynamic Sql
    lv_string = |ZVPLM_ER_EC_TYPE IN @S_ECTYPE |.
  ENDIF.

  IF s_ecid IS NOT INITIAL.
    lv_string = |{ lv_string }AND ECR_ID IN @S_ECID |.
  ENDIF.

  IF s_prjid IS NOT INITIAL.
    lv_string = |{ lv_string }AND ZPROJCODE IN @S_PRJID |.
  ENDIF.

  IF p_status IS NOT INITIAL.
    lv_string = |{ lv_string }AND ( ZVPLM_CMPT = @P_STATUS OR ZVPLM_CMPT = '' ) |.
  ENDIF.

  IF s_lob IS NOT INITIAL.
    lv_string = |{ lv_string }AND ZVPLM_ER_LOB IN @S_LOB |.
  ENDIF.

  IF s_adate IS NOT INITIAL..
    lv_string = |{ lv_string }AND ZVPLM_REL_DATE IN @S_ADATE |.
  ENDIF.

  IF s_create IS NOT INITIAL.
    lv_string = |{ lv_string }AND CREATED_BY IN @S_CREATE |.
  ENDIF.

  IF p_check = 'X'.
    IF gv_moduser = TEXT-T11.
      lv_string = |{ lv_string }AND ZVPLM_UIPQC = '' |.
    ENDIF.

    IF gv_moduser = TEXT-T12.
      lv_string = |{ lv_string }AND ZVPLM_UIPQC <> '' AND ZVPLM_UPQE = '' |.
    ENDIF.
  ENDIF.

  IF lv_string IS INITIAL.
    SELECT * FROM ztvplm0031 INTO TABLE @gt_alvshow1[] UP TO @p_limit ROWS. "Undefined Query Conditions
    IF gt_alvshow1 IS INITIAL.
      MESSAGE |没有记录找到| TYPE 'S'.
    ELSE.
      gt_alvshow2 = gt_alvshow1.
    ENDIF.
  ELSE.
    IF lv_string+0(3) = 'AND'.
      lv_string = lv_string+4(*).   "Del Home "AND"
    ENDIF.
    SELECT * FROM ztvplm0031 WHERE (lv_string) INTO TABLE @gt_alvshow1[] UP TO @p_limit ROWS. "Dynamic Query
    IF gt_alvshow1 IS INITIAL.
      MESSAGE |没有记录找到| TYPE 'S'.
    ELSE.
      gt_alvshow2 = gt_alvshow1.
    ENDIF.
  ENDIF.
ENDFORM.