動態內表實例

****************** hardcoded "old style" local type*******************
* This is a normal hardcoded local type
TYPES : BEGIN OF TYP_HARDCODED,
      L_COUNT TYPE I,
      LT_SFLIGHT TYPE SFLIGHT.
TYPES : END OF TYP_HARDCODED.
* create a table based on hardcoded type
DATA : LT_HARDCODED TYPE TABLE OF TYP_HARDCODED.
****************** dynamic "new wave" local type *******************
TYPES: TYP_COUNT TYPE I.
FIELD-SYMBOLS : <LT_DYNAMIC> TYPE ANY TABLE.
DATA: DREF TYPE REF TO DATA,
  ITAB_TYPE TYPE REF TO CL_ABAP_TABLEDESCR,
  STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
  ELEM_TYPE TYPE REF TO CL_ABAP_ELEMDESCR,
  COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
  COMP_FLD TYPE CL_ABAP_STRUCTDESCR=>COMPONENT.
* We read information about each fields of SFLIGHT (see ABAP FAQ #2)
STRUCT_TYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'SFLIGHT' ).
* We also need the information about the type "typ_count", note that
* we should use class cl_abap_elemdescr instead of cl_abap_typedescr
ELEM_TYPE ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'TYP_COUNT' ).
* we read all fleids of SFLIGHT and create a component table
COMP_TAB = STRUCT_TYPE->GET_COMPONENTS( ).
* We add manually the counter
COMP_FLD-NAME = 'L_COUNT'.
COMP_FLD-TYPE = ELEM_TYPE.
INSERT COMP_FLD INTO COMP_TAB INDEX 1.
* we create the structure
STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( COMP_TAB ).
* ... and the internal table
ITAB_TYPE = CL_ABAP_TABLEDESCR=>CREATE( STRUCT_TYPE ).
* The new thing here is the "type handle" which create a pointer to a handle
CREATE DATA DREF TYPE HANDLE ITAB_TYPE.
* we finally assign a field symbol to the pointer because we cannot directly access a pointer.
ASSIGN DREF->* TO <LT_DYNAMIC>.
* At the end of this small program, internal table lt_hardcoded and lt_dynamic are the same

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章