0% found this document useful (0 votes)
19 views

Zdynamic Table

This document describes how to build a dynamic table in ABAP in 5 steps: 1) Define variables and data types, 2) Populate a structure with field definitions, 3) Create a structure type and table type from the field structure, 4) Create a data reference for the table type, 5) Assign the data reference to a field symbol to access the dynamic table.

Uploaded by

Raju Basava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Zdynamic Table

This document describes how to build a dynamic table in ABAP in 5 steps: 1) Define variables and data types, 2) Populate a structure with field definitions, 3) Create a structure type and table type from the field structure, 4) Create a data reference for the table type, 5) Assign the data reference to a field symbol to access the dynamic table.

Uploaded by

Raju Basava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

*&-------------------------------------------------------------------*& Report ZDYNAMIC_TABLE

*& By: Yuhalbert Graterol


*& Description: This Program Show how to build a Dynamic Table in Five
*& steps
*&-------------------------------------------------------------------*&
*&
*&-------------------------------------------------------------------REPORT zdynamic_table.
*&-------------------------------------------------------------------*&STEP: ONE
*&-------------------------------------------------------------------*& This struct will content all components (fields) of our dynamic
table
DATA: lt_comp
TYPE cl_abap_structdescr=>component_table.
*& 'str_type' will content new dynamic structure created
DATA: str_type TYPE REF TO cl_abap_structdescr.
*& 'tab_type' will content new dynamic table created
DATA: tab_type TYPE REF TO cl_abap_tabledescr.
*& 'lt_data' will be our point to reference data
DATA: lt_data
TYPE REF TO data.
*& We need a work area to handle attributes and name of each fields.
DATA: la_comp
LIKE LINE OF lt_comp.
*& Some variable to build name of each field
DATA: field_name
TYPE txt30.
DATA: field_number TYPE text10.
*& Field Symbol to handle Table
FIELD-SYMBOLS: <t_tab> TYPE STANDARD TABLE.
START-OF-SELECTION.
*&-------------------------------------------------------------------*& STEP: TWO
*&-------------------------------------------------------------------*& At this Point we will Populate Internal Table table lt_comp with
*& five fields called: FIELD01, FIELD02, FIELD03, FIELD04, FIELD05
*&-------------------------------------------------------------------DO 5 TIMES.
CLEAR: field_name.
WRITE sy-index TO field_number.
CONDENSE field_number.
CONCATENATE 'FIELD0' field_number INTO field_name.
*& Name for Each Field

la_comp-name = field_name.
*& For This Example we will build all field with type C and lenght
1024
la_comp-type = cl_abap_elemdescr=>get_c( p_length = 1024 ).
APPEND la_comp TO lt_comp.
CLEAR: la_comp.
ENDDO.
*&-------------------------------------------------------------------*& STEP. Three
*& Now we must create a Structure with all fields loaded before.
*&-------------------------------------------------------------------str_type = cl_abap_structdescr=>create( lt_comp ).
*& In this step we create a type table by using the structure
STR_TYPE,
*& and passing 2 Additional Parameters: p_table_kind (Standard,
Hashed, Sorted)
*& p_unique (this to indicate if have unique key or not)
tab_type = cl_abap_tabledescr=>create( p_line_type = str_type
p_table_kind =
cl_abap_tabledescr=>tablekind_std
p_unique
= abap_false ).
*&-------------------------------------------------------------------*& STEP: FOUR
*&-------------------------------------------------------------------*& Data to handle the new table type. We could catch an exception
here.
TRY.
CREATE DATA lt_data TYPE HANDLE tab_type.
CATCH cx_sy_create_data_error.
ENDTRY.
*&-------------------------------------------------------------------*& STEP: FIVE
*&-------------------------------------------------------------------*& New internal table referenced by field symbol
TRY.
ASSIGN lt_data->* TO <t_tab>.
CATCH cx_sy_assign_cast_illegal_cast.
CATCH cx_sy_assign_cast_unknown_type.
CATCH cx_sy_assign_out_of_range.
ENDTRY.
*&

at this point you will have a reference to your dynamic table type
BREAK-POINT.

You might also like