Saturday, 22 February 2014

Creating Dynamic Internal Table with Data Reference and Field Symbol

Scenario: This post shows how to create a table structure at run time based on the user input table name and displaying the table contents either on the list or on the ALV grid.


View1. Output view in list.

Step1: Create a program in SE38 and activate it.
_________________________________________________________________________________
*&---------------------------------------------------------------------*
*& Report  ZDEMO1_DYNAMIC_TABLE_CREATION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdemo1_dynamic_table_creation.

PARAMETERS : tab_name TYPE dd02l-tabname OBLIGATORY.
DATA : ls_tab TYPE dd02l.
DATA: dr_tab TYPE REF TO data.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE,
                                 <fs_wa>,
                                 <fs_fld>.

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.

  CREATE DATA dr_tab TYPE TABLE OF (tab_name).
  ASSIGN dr_tab->* TO <fs_tab>.

  SELECT * FROM (tab_name) INTO TABLE <fs_tab>.

END-OF-SELECTION.

  LOOP AT <fs_tab> ASSIGNING <fs_wa> .
   new-line.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_fld>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      WRITE  <fs_fld>.
    ENDDO.
  ENDLOOP.

_________________________________________________________________________________

Step2. Run and See the Output.

















________________________________________________________________________________
Run-2.






































_________________________________________________________________________________

View2. Output view in ALV grid.

Step1.

_________________________________________________________________________________
*&---------------------------------------------------------------------*
*& Report  ZDEMO1_DYNAMIC_TABLE_CREATION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdemo1_dynamic_table_creation.

PARAMETERS : tab_name TYPE dd02l-tabname OBLIGATORY.
DATA : ls_tab TYPE dd02l.
DATA: dr_tab TYPE REF TO data.
DATA : o_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE,
                <fs_wa>,
                <fs_fld>.

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.
  CREATE DATA dr_tab TYPE TABLE OF (tab_name).
  ASSIGN dr_tab->* TO <fs_tab>.

  SELECT * FROM (tab_name) INTO TABLE <fs_tab>.

END-OF-SELECTION.

  CREATE OBJECT o_grid
    EXPORTING
      i_parent          =  cl_gui_container=>default_screen.
*     i_parent          =  cl_gui_container=>screen0.     

  CALL METHOD o_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = tab_name
    CHANGING
      it_outtab        = <fs_tab>.

  CALL SCREEN 9999. " Provide a screen number and double click on it, provide a text and activate.
________________________________________________________________________________


________________________________________________________________________________

Step2. Run the program, provide the table name and observe the out put on alv grid.








































________________________________________________________________________________
Run-2



































_________________________________________________________________________________

No comments:

Comments system

Disqus Shortname