isap 发表于 2011-9-26 15:49:57

ABAP动态查询表

*&---------------------------------------------------------------------*
*& Report  ZMYTEST_DYNA_SQL
*&
*&---------------------------------------------------------------------*
*
*ABAP动态查询的实现:可以完全实现动态查询,每个字段都可以动态。
*(1)         利用宏
*(2)         利用abap指针
*(3)         利用FIELD-SYMBOL
*&---------------------------------------------------------------------*
REPORT  ZMYTEST_DYNA_SQL.
TABLES: SPFLI.
DATA: L_FIELD(100) TYPE C,
      L_TABLE(10) TYPE C,
      L_COND(100) TYPE C.
DATA: I_COND LIKE TABLE OF L_COND.
DATA: i_spfli LIKE TABLE OF SPFLI,
      wa_spfli LIKE SPFLI.
FIELD-SYMBOLS <fs> TYPE ANY.
FIELD-SYMBOLS <i_fs> TYPE TABLE.

*具体实现:
*1.结构的动态查询
DEFINE SELECT_DATA_TO_WA.
SELECT &1
    FROM &2
    INTO CORRESPONDING FIELDS OF &3
   WHERE (&4).
    EXIT.
ENDSELECT.
END-OF-DEFINITION.
*2.变量的动态查询
DEFINE SELECT_DATA_TO_VARIANT.
SELECT &1
    FROM &2
    INTO &3
   WHERE (&4).
    EXIT.
ENDSELECT.
END-OF-DEFINITION.
*3.内表的动态查询
DEFINE SELECT_DATA_TO_ITAB.
SELECT &1
    FROM &2
    INTO CORRESPONDING FIELDS OF TABLE &3
   WHERE (&4).
END-OF-DEFINITION.
START-OF-SELECTION.
  CONCATENATE 'CARRID' 'CONNID' 'CITYFROM' INTO L_FIELD SEPARATED BY SPACE.
  L_COND = 'CONNID <> ' & '''' & '0123' & ''''.
  APPEND L_COND TO I_COND.
  L_TABLE = 'SPFLI'.
*  IF <fs> IS ASSIGNED.
*    UNASSIGN  <fs>.
*    ASSIGN wa_spfli TO <fs>.
*  ELSE.
*    ASSIGN wa_spfli TO <fs>.
*  ENDIF.
*  SELECT_DATA_TO_WA (L_FIELD) (L_TABLE) <fs> I_COND.
  IF <i_fs> IS ASSIGNED.
    UNASSIGN  <i_fs>.
    ASSIGN i_spfli TO <i_fs>.
  ELSE.
    ASSIGN i_spfli TO <i_fs>.
  ENDIF.
*  SELECT_DATA_TO_ITAB (L_FIELD) (L_TABLE) i_spfli I_COND.
  SELECT_DATA_TO_ITAB (L_FIELD) (L_TABLE) <i_fs> I_COND.
  LOOP AT <i_fs> INTO wa_spfli.
  ENDLOOP.
  IF sy-subrc <> 0.
    MESSAGE i001(00) WITH '失败!'.
  ELSE.
    MESSAGE i001(00) WITH '成功!'.
  ENDIF
页: [1] 2
查看完整版本: ABAP动态查询表