Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Append Rows to a dynamic defined table

Former Member
0 Kudos

Hi,

i have to build some data for different tables in the same way. So i defined a table dynamic with:

TYPE ANY.

Now i have to buil this table and insert some rows in it by APPEND, can someone tell me how it works?

Many thanks,

Kambiz

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hello.

Take a look on these:

[Dynamic Internal Table|https://wiki.sdn.sap.com/wiki/x/UU8] in form SET_DATA

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b735e96f...

Regards.

3 REPLIES 3

Former Member
0 Kudos

Hello.

Take a look on these:

[Dynamic Internal Table|https://wiki.sdn.sap.com/wiki/x/UU8] in form SET_DATA

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b735e96f...

Regards.

david_carballido
Active Participant
0 Kudos

Hi ... I post a little example about how insert lines in a table

Sry for my bad english xD!

*&---------------------------------------------------------------------*
*& Report  ZPRUEBA_2
*&
*&---------------------------------------------------------------------*
*&  Autor       : David Carballido
*&  Descripción : Crear una tabla interna dinámica
*&  Programa    : ZPRUEBA_2
*&  Proyecto    : Proyecto Relax xD!
*&---------------------------------------------------------------------*

REPORT  ZPRUEBA_2.

TYPE-POOLS: abap, col.
* Tablas dinámicas
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <fs>.

* Variables Globales
DATA: gt_dyn          TYPE REF TO data,
      gw_dyn          TYPE REF TO DATA,
      r_table         TYPE REF TO cl_salv_table,
      r_header        TYPE REF TO cl_salv_form_element,
      r_footer        TYPE REF TO cl_salv_form_element,
      r_columns_table TYPE REF TO cl_salv_columns_table,
      r_column_table  TYPE REF TO cl_salv_column_table,
      r_datadescr     TYPE REF TO cl_abap_datadescr,
      r_structdescr   TYPE REF TO cl_abap_structdescr,
      gw_component    TYPE abap_componentdescr,
      gt_component    TYPE abap_component_tab.

PARAMETERS: p_col(2) TYPE c OBLIGATORY.

*  S T A R T  -  O F  -  S E L E C T I O N
START-OF-SELECTION.
  CHECK sy-ucomm = 'ONLI'.
  PERFORM build_dynamic_table.
  PERFORM get_data.

*  E N D  -  O F  -  S E L E C T I O N
END-OF-SELECTION.
  PERFORM display_report.

*&---------------------------------------------------------------------*
*&      Form build_dynamic_table
*&---------------------------------------------------------------------*
FORM build_dynamic_table.

  DATA: l_idx(2) TYPE c,
        l_desc   TYPE char50,
        l_hslxx  TYPE p LENGTH 5 DECIMALS 2,
        lt_color TYPE lvc_t_scol.

* Columna 1
  r_datadescr ?= cl_abap_datadescr=>describe_by_data( l_desc ).
  gw_component-name = 'COLUMN'.
  gw_component-type = r_datadescr.
  APPEND gw_component TO gt_component.
* Column 2 - Types of color for each line
  r_datadescr ?= cl_abap_datadescr=>describe_by_data( lt_color ).
  gw_component-name = 'COLOR'.
  gw_component-type = r_datadescr.
  APPEND gw_component TO gt_component.

  DO p_col TIMES.
    l_idx = sy-index.
    CONDENSE l_idx NO-GAPS.
    r_datadescr ?= cl_abap_datadescr=>describe_by_data( l_hslxx ).
    CONCATENATE 'COL' l_idx INTO gw_component-name.
    gw_component-type = r_datadescr.
    APPEND gw_component TO gt_component.
  ENDDO.

  TRY.
    r_structdescr = cl_abap_structdescr=>create( p_components = gt_component ).
    CATCH cx_sy_struct_creation .
    WRITE: / 'CX_SY_STRUCT_CREATION'.
  ENDTRY.
* Fill the table with data from GT_DATA
  CREATE DATA gw_dyn TYPE HANDLE r_structdescr.
  ASSIGN gw_dyn->* TO <dyn_wa>.
  CREATE DATA gt_dyn LIKE STANDARD TABLE OF <dyn_wa>.
  ASSIGN gt_dyn->* TO <dyn_table>.

ENDFORM.                 " build_dynamic_table

*&---------------------------------------------------------------------*
*&      Form get_data
*&---------------------------------------------------------------------*
FORM get_data.
* select statement
ENDFORM.                    "get_data

*&---------------------------------------------------------------------*
*&      Form display_report
*&---------------------------------------------------------------------*
FORM display_report .

  PERFORM display_header.   " Display ALV Header
  PERFORM display_footer.   " DIsplay ALV Footer
  PERFORM fill_data.        " Fill data
  PERFORM set_color.        " Set color
  PERFORM display_list.     " Display the ALV

ENDFORM.                  " display_report

*&---------------------------------------------------------------------*
*&      Form display_header
*&---------------------------------------------------------------------*
FORM display_header .

  DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid,
        l_text  TYPE string.

  SELECT SINGLE name_textc
    INTO l_text
      FROM user_addr
        WHERE bname = sy-uname.

  CONCATENATE 'Usuario :'
              l_text INTO l_text SEPARATED BY space.

  CREATE OBJECT lr_grid.
  lr_grid->create_text( row    = 1
                        column = 1
                        text   = l_text ).
  lr_grid->create_text( row    = 2
                        column = 1
                        text   = sy-host ).
  r_header = lr_grid.

ENDFORM.                    " display_header

*&---------------------------------------------------------------------*
*&      Form display_footer
*&---------------------------------------------------------------------*
FORM display_footer .

  DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid.

  CREATE OBJECT lr_grid.
  lr_grid->create_text( row    = 1
                        column = 1
                        text   = sy-datum ).
  lr_grid->create_text( row    = 2
                        column = 1
                        text   = sy-uzeit ).
  r_footer = lr_grid.

ENDFORM.                    " display_footer

*&---------------------------------------------------------------------*
*&      Form set_color
*&---------------------------------------------------------------------*
FORM set_color.

  DATA: lt_color TYPE lvc_t_scol,
        ls_color LIKE LINE OF lt_color,
        l_idx(2) TYPE c.

  FIELD-SYMBOLS: <fs_color>.

  LOOP AT <dyn_table> INTO <dyn_wa>.
    l_idx = sy-tabix.
    CONDENSE l_idx NO-GAPS.
    ASSIGN COMPONENT 'COLOR' OF STRUCTURE <dyn_wa> TO <fs_color>.
    ls_color-color-col = '6'.
    APPEND ls_color TO lt_color.
    <fs_color> = lt_color.
    MODIFY <dyn_table> FROM <dyn_wa> INDEX l_idx.
    UNASSIGN: <fs_color>.
  ENDLOOP.

ENDFORM.                    " set_color

*&---------------------------------------------------------------------*
*&      Form display_list
*&---------------------------------------------------------------------*
FORM display_list.

  DATA: r_display TYPE REF TO cl_salv_display_settings.
* Prepare the internal table for display
  cl_salv_table=>factory( EXPORTING list_display = 'X'
                          IMPORTING r_salv_table = r_table
                          CHANGING  t_table      = <dyn_table> ).
* Set report page title
  r_table->set_top_of_list( r_header ).
* Set report footer
  r_table->set_end_of_list( r_footer ).
  r_display = r_table->get_display_settings( ).
* Assign all the column names
  PERFORM set_column_attr.
* Display the report
  r_table->display( ).

ENDFORM.                    " display_list

*&---------------------------------------------------------------------*
*&      Form set_column_attr
*&---------------------------------------------------------------------*
FORM set_column_attr.

  DATA: l_idx(2)  TYPE c,
        l_text(4) TYPE c,
        colname   TYPE lvc_fname,
        outps     TYPE scrtext_s,
        outpm     TYPE scrtext_m,
        outpl     TYPE scrtext_l.

  r_columns_table = r_table->get_columns( ).
* Set color column
  r_columns_table->set_color_column( 'COLOR' ).
  DO p_col TIMES.
    l_idx = sy-index.
    CONDENSE l_idx NO-GAPS.
    CONCATENATE 'COL' l_idx INTO colname.
    outps = colname.
    outpm = colname.
    outpl = colname.
    r_column_table ?= r_columns_table->get_column( colname ).
    r_column_table->set_optimized( value  = abap_true ).
    r_column_table->set_alignment( value  = 1 ).
    r_column_table->set_zero( value  = space ).
    r_column_table->set_short_text( outps ).
    r_column_table->set_medium_text( outpm ).
    r_column_table->set_long_text( outpl ).
  ENDDO.

ENDFORM.                    " set_column_attr

*&---------------------------------------------------------------------*
*&      Form fill_data
*&---------------------------------------------------------------------*
FORM fill_data.

  DATA: l_idx(2),
        l_col  TYPE string.

  DO p_col TIMES.
    l_idx = sy-index.
    CONDENSE l_idx NO-GAPS.
    CONCATENATE 'COL' l_idx INTO l_col.
    ASSIGN COMPONENT l_col OF STRUCTURE <dyn_wa> TO <fs>.
    <fs> = 0.
    UNASSIGN <fs>.
  ENDDO.

  DO p_col TIMES.
    l_idx = sy-index.
    CONDENSE l_idx NO-GAPS.
    ASSIGN COMPONENT 'COLUMN' OF STRUCTURE <dyn_wa> TO <fs>.
    CONCATENATE 'Row' l_idx INTO <fs> SEPARATED BY space.
    UNASSIGN <fs>.
    APPEND <dyn_wa> TO <dyn_table>.
  ENDDO.

ENDFORM.                   " fill_data

Former Member
0 Kudos

Hi kambiz,

If you are using ALV, you can check with the following code below:

*&-------Field Tab

DATA : t_fieldtab TYPE TABLE OF lvc_s_fcat. "Internal Table

*&-----Field Symbols Declarations

FIELD-SYMBOLS : <f_fs> TYPE table. "FS to hold dynamic table

FORM generate_output_table .

*&-----Local Data Declarations

DATA : l_ref TYPE REF TO data. "Reference

*&-----Generate Internal Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fieldtab

IMPORTING

ep_table = l_ref.

ASSIGN l_ref->* TO <f_fs>.

ENDFORM. " generate_output_table

If possible please elaborate on your requirement.

Regards,

Santosh Verma,

Deloitte, India.