【原创】ABAP动态编程之功能实现

根据名字获取结构
DATA: STRUCTTYPE TYPE REF TO CL_ABAP_STRUCTDESCR.
STRUCTTYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'SPFLI' ).
根据变量获取结构
DATA: DATATYPE TYPE REF TO CL_ABAP_ELEMDESCR,
      W_CHAR TYPE CHAR5.
DATATYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( W_CHAR ).
自定义数值结构
DATA: ELEMTYPE TYPE REF TO CL_ABAP_ELEMDESCR.
ELEMTYPE ?= CL_ABAP_ELEMDESCR=>GET_I( ).
ELEMTYPE ?= CL_ABAP_ELEMDESCR=>GET_C( 20 ).
定义动态长度的字符串
DATA: W_C10 TYPE CHAR10 VALUE 'HB',
      W_C20 TYPE CHAR20,
      W_CNT TYPE I.
W_CNT = STRLEN( W_C10 ).

DATA: DREF  TYPE REF TO DATA,
      CTYPE TYPE REF TO CL_ABAP_ELEMDESCR.
FIELD-SYMBOLS:
      <FS>.

CTYPE = CL_ABAP_ELEMDESCR=>GET_C( W_CNT ).

CREATE DATA DREF TYPE HANDLE CTYPE.
ASSIGN DREF->* TO <FS>.
*ASSIGN DREF->* TO <FS> CASTING TYPE HANDLE CTYPE.

<FS> = W_C10(W_CNT).
CONCATENATE '***' <FS> '***' INTO W_C20 RESPECTING BLANKS.
WRITE W_C20.

动态读取系统表数据
PARAMETERS P_NAME TYPE TABNAME.

DATA: LR_STRUC  TYPE REF TO CL_ABAP_STRUCTDESCR,
      LR_DATA   TYPE REF TO CL_ABAP_DATADESCR,
      LR_TABLE  TYPE REF TO CL_ABAP_TABLEDESCR,
      DYN_TABLE TYPE REF TO DATA,
      DYN_WA    TYPE REF TO DATA.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>    TYPE ANY.

LR_STRUC ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( P_NAME ).

LR_TABLE ?= CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LR_STRUC ).

*ASSIGN DYN_WA->*    TO <DYN_WA>    CASTING TYPE HANDLE LR_STRUC.
*ASSIGN DYN_TABLE->* TO <DYN_TABLE> CASTING TYPE HANDLE LR_TABLE.

CREATE DATA DYN_WA    TYPE HANDLE LR_STRUC.
CREATE DATA DYN_TABLE TYPE HANDLE LR_TABLE.
ASSIGN DYN_WA->*    TO <DYN_WA>.
ASSIGN DYN_TABLE->* TO <DYN_TABLE>.

SELECT * INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE>
       UP TO 100 ROWS
     FROM (P_NAME).

*CALL METHOD LR_TABLE->GET_TABLE_LINE_TYPE
*  RECEIVING
*    P_DESCR_REF = LR_DATA
*    .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    I_STRUCTURE_NAME = P_NAME
  TABLES
    T_OUTTAB         = <DYN_TABLE>
  EXCEPTIONS
    PROGRAM_ERROR    = 1
    OTHERS           = 2.
IF SY-SUBRC <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

动态定义表结构
TYPES:
  BEGIN OF TYP_PERSON,
    NAME TYPE CHAR10,
    SEX  TYPE CHAR1,
    AGE  TYPE I,
  END OF TYP_PERSON.

DATA: ST_PERSON TYPE TYP_PERSON,
      TR_TABLE  TYPE REF TO CL_ABAP_TABLEDESCR,
      TR_STRUCT TYPE REF TO CL_ABAP_STRUCTDESCR,
      TR_TYPE   TYPE REF TO CL_ABAP_TYPEDESCR,
      FS_TABLE  TYPE REF TO DATA,
      FS_STRUCT TYPE REF TO DATA,
      TD_COMP   TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
      TH_COMP   LIKE LINE OF TD_COMP.

FIELD-SYMBOLS:
      <FS_TABLE>  TYPE STANDARD TABLE,
      <FS_STRUCT> TYPE ANY.

TR_STRUCT ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( ST_PERSON ).
TD_COMP = TR_STRUCT->GET_COMPONENTS( ).

CALL METHOD CL_ABAP_STRUCTDESCR=>CREATE
  EXPORTING
    P_COMPONENTS = TD_COMP
*    P_STRICT     = TRUE
  RECEIVING
    P_RESULT     = TR_STRUCT
    .

CALL METHOD CL_ABAP_TABLEDESCR=>CREATE
  EXPORTING
    P_LINE_TYPE  = TR_STRUCT
  RECEIVING
    P_RESULT     = TR_TABLE
    .

CREATE DATA FS_STRUCT TYPE HANDLE TR_STRUCT.
CREATE DATA FS_TABLE  TYPE HANDLE TR_TABLE.
ASSIGN FS_STRUCT->* TO <FS_STRUCT>.
ASSIGN FS_TABLE->*  TO <FS_TABLE>.

动态读取表结构
TYPES:
  BEGIN OF TYP_PERSON,
    NAME TYPE CHAR10,
    SEX  TYPE CHAR1,
    AGE  TYPE I,
  END OF TYP_PERSON.

DATA: TH_PERSON TYPE TYP_PERSON,
      TD_PERSON TYPE TABLE OF TYP_PERSON.

PERFORM FRM_SWAP_TABLE  TABLES TD_PERSON.
*&---------------------------------------------------------------------*
*&      Form  FRM_SWAP_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PTD_PERSON  text
*----------------------------------------------------------------------*
FORM FRM_SWAP_TABLE  TABLES   PTD_PERSON.
DATA: FS_TABLE  TYPE REF TO DATA,
      FS_STRUCT TYPE REF TO DATA,
      TR_TABLE  TYPE REF TO CL_ABAP_TABLEDESCR,
      TR_STRUCT  TYPE REF TO CL_ABAP_STRUCTDESCR,
      TR_TYPE   TYPE REF TO CL_ABAP_TYPEDESCR.

FIELD-SYMBOLS:
      <FS_TABLE>  TYPE STANDARD TABLE,
      <FS_STRUCT> TYPE ANY.

TR_STRUCT ?=
          CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( P_DATA = PTD_PERSON ).

CREATE DATA FS_STRUCT TYPE HANDLE TR_STRUCT.
ASSIGN FS_STRUCT->* TO <FS_STRUCT>.
CREATE DATA FS_TABLE  LIKE TABLE OF <FS_STRUCT>.
ASSIGN FS_TABLE->*  TO <FS_TABLE>.

ENDFORM.                    " FRM_SWAP_TABLE

转载于:https://www.cnblogs.com/loveplxf/archive/2012/08/15/2640767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值