Scenario: This post shows how to create an internal table using RTTS concepts.
Step1. Create a program in SE38.
_________________________________________________________________________________
*&---------------------------------------------------------------------*
*& Report ZDEMO3_DYNAMIC_TABLE_CREATION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdemo3_dynamic_table_creation.
PARAMETERS : tab_name TYPE dd02l-tabname OBLIGATORY.
DATA : ls_tab TYPE dd02l,
lo_type TYPE REF TO cl_abap_typedescr,
lo_struc TYPE REF TO cl_abap_structdescr,
lo_struct TYPE REF TO cl_abap_structdescr,
lt_comp TYPE abap_component_tab,
lo_table TYPE REF TO cl_abap_tabledescr,
lo_grid TYPE REF TO cl_gui_alv_grid,
dr_tab TYPE REF TO data.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE.
START-OF-SELECTION.
SELECT SINGLE * FROM dd02l INTO ls_tab WHERE tabname = tab_name.
IF sy-subrc <> 0.
MESSAGE 'Table does not Exist.' TYPE 'S'.
LEAVE LIST-PROCESSING.
ENDIF.
CALL METHOD cl_abap_typedescr=>describe_by_name
EXPORTING
p_name = tab_name
RECEIVING
p_descr_ref = lo_type.
lo_struc ?= lo_type.
CALL METHOD lo_struc->get_components
RECEIVING
p_result = lt_comp.
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lo_struct.
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = lo_struct
RECEIVING
p_result = lo_table.
CREATE DATA dr_tab TYPE HANDLE lo_table.
ASSIGN dr_tab->* TO <fs_tab>.
SELECT * FROM (tab_name) INTO TABLE <fs_tab>.
END-OF-SELECTION.
CREATE OBJECT lo_grid
EXPORTING
i_parent = cl_gui_container=>default_screen.
CALL METHOD lo_grid->set_table_for_first_display
EXPORTING
i_structure_name = tab_name
CHANGING
it_outtab = <fs_tab>.
CALL SCREEN 9999.
_________________________________________________________________________________
Step2.
________________________________________________________________________________
Step1. Create a program in SE38.
_________________________________________________________________________________
*&---------------------------------------------------------------------*
*& Report ZDEMO3_DYNAMIC_TABLE_CREATION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdemo3_dynamic_table_creation.
PARAMETERS : tab_name TYPE dd02l-tabname OBLIGATORY.
DATA : ls_tab TYPE dd02l,
lo_type TYPE REF TO cl_abap_typedescr,
lo_struc TYPE REF TO cl_abap_structdescr,
lo_struct TYPE REF TO cl_abap_structdescr,
lt_comp TYPE abap_component_tab,
lo_table TYPE REF TO cl_abap_tabledescr,
lo_grid TYPE REF TO cl_gui_alv_grid,
dr_tab TYPE REF TO data.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE.
START-OF-SELECTION.
SELECT SINGLE * FROM dd02l INTO ls_tab WHERE tabname = tab_name.
IF sy-subrc <> 0.
MESSAGE 'Table does not Exist.' TYPE 'S'.
LEAVE LIST-PROCESSING.
ENDIF.
CALL METHOD cl_abap_typedescr=>describe_by_name
EXPORTING
p_name = tab_name
RECEIVING
p_descr_ref = lo_type.
lo_struc ?= lo_type.
CALL METHOD lo_struc->get_components
RECEIVING
p_result = lt_comp.
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lo_struct.
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = lo_struct
RECEIVING
p_result = lo_table.
CREATE DATA dr_tab TYPE HANDLE lo_table.
ASSIGN dr_tab->* TO <fs_tab>.
SELECT * FROM (tab_name) INTO TABLE <fs_tab>.
END-OF-SELECTION.
CREATE OBJECT lo_grid
EXPORTING
i_parent = cl_gui_container=>default_screen.
CALL METHOD lo_grid->set_table_for_first_display
EXPORTING
i_structure_name = tab_name
CHANGING
it_outtab = <fs_tab>.
CALL SCREEN 9999.
_________________________________________________________________________________
Step2.
________________________________________________________________________________
No comments:
Post a Comment