In this blog, I will talk about the necessary methods and definitions for the use of dynamic tables in a program, dynamic select and how to perform operations on dynamic table. I hope it will be an example for you to create a dynamic table. Before start, dynamic internal table is an internal table with variable rows and columns which can be defined during run time.
Definitions
For dynamic table, we create Field-symbol. Remember that “Field symbols can be declared in any procedure and in the global declaration part of an ABAP program, but not in the declaration part of a class or an interface.”
FIELD-SYMBOLS : <table> TYPE STANDARD TABLE.
We can see the types, variables and methods that we will use in Class structure below.
CLASS lcl_class DEFINITION.
PUBLIC SECTION.
PRIVATE SECTION.
TYPES:BEGIN OF ts_add_column,
name TYPE string,
types TYPE string,
END OF ts_add_column,
tt_add_coll TYPE STANDARD TABLE OF ts_add_column WITH EMPTY KEY,
BEGIN OF ts_dyn_name,
name TYPE string,
END OF ts_dyn_name,
tt_dyn_name TYPE STANDARD TABLE OF ts_dyn_name WITH EMPTY KEY.
DATA: gt_dyn_names TYPE tt_dyn_name,
gv_string TYPE string.
METHODS:
get_data,
create_dynamic_table,
get_dynamic_fields RETURNING VALUE(rt_table) TYPE tt_add_coll,
get_all_fields IMPORTING VALUE(is_table) TYPE any
VALUE(it_type) TYPE tt_add_coll
RETURNING VALUE(rv_value) TYPE REF TO data,
set_datadescr IMPORTING VALUE(iv_types) TYPE string
RETURNING VALUE(rv_descr) TYPE REF TO cl_abap_datadescr.
ENDCLASS.
Methods
We call create_dynamic_table( ) method to create dynamic table. We add the fields that we dynamically retrieved from the table into a string. Thus, the fields we pull from the table in select also become dynamic.
METHOD get_data.
create_dynamic_table( ).
LOOP AT gt_dyn_names INTO DATA(ls_name).
gv_string = |{ gv_string } c~{ ls_name-name },|.
ENDLOOP.
gv_string = |{ gv_string } c~poper, c~waers, c~prtyp, data~bwkey, data~matnr, data~mtart, data~matkl, data~bklas, data~bwtar, data~prctr, data~kalnr,|.
gv_string = |{ gv_string } @p_bdatj AS bdatj, @p_bukrs AS bukrs, CASE WHEN c~prtyp = 'V' THEN 'Fiili' WHEN c~prtyp = 'S' THEN 'Plan' ELSE ' ' END AS stat|.
SELECT ( gv_string )
FROM ckmlprkeph AS c
INNER JOIN ckmlhd ON ckmlhd~kalnr = c~kalnr
INNER JOIN @lt_data AS data “itab join
ON data~kalnr = c~kalnr
WHERE c~bdatj EQ @p_bdatj
AND c~poper IN @s_poper
AND c~curtp EQ @p_curtp
AND c~kkzst EQ ''
INTO CORRESPONDING FIELDS OF TABLE @<table>.
We use field-symbol and assign component for the operations to be performed on dynamic table. We can also give a key to sort dynamically using the method in the example. For now we will use a fixed key. We can use the following examples.
LOOP AT <table> ASSIGNING FIELD-SYMBOL(<fs_table>).
ASSIGN COMPONENT 'PRTYP' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_prtyp>).
IF <fs_prtyp> EQ 'V'.
ASSIGN COMPONENT 'KALNR' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_kalnr>).
READ TABLE <table> ASSIGNING FIELD-SYMBOL(<fs_v>) WITH KEY ('PRTYP') ='V' ('KALNR') = <fs_kalnr>.
READ TABLE <table> ASSIGNING FIELD-SYMBOL(<fs_s>) WITH KEY ('PRTYP') = 'S' ('KALNR') = <fs_kalnr>.
APPEND INITIAL LINE TO <table> ASSIGNING FIELD-SYMBOL(<fs_line>).
MOVE-CORRESPONDING <fs_v> TO <fs_line>.
ASSIGN COMPONENT 'PRTYP' OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_l_prtyp>).
<fs_l_prtyp> = 'X'.
ASSIGN COMPONENT 'STAT' OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_durum>).
<fs_durum> = 'Fark'.
LOOP AT gt_dyn_names INTO DATA(ls_dyn_names).
ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_l_kst>).
ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_s> TO FIELD-SYMBOL(<fs_s_kst>).
<fs_l_kst> -= <fs_s_kst>.
ENDLOOP.
ENDIF.
ENDLOOP.
UNASSIGN <fs_table>.
LOOP AT <table> ASSIGNING <fs_table>.
LOOP AT gt_dyn_names INTO ls_dyn_names.
ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_kst>).
ASSIGN COMPONENT 'TOTAL' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_total>).
<fs_total> += <fs_kst>.
ENDLOOP.
ENDLOOP.
UNASSIGN <fs_table>.
SORT <table> ASCENDING BY ('KALNR').
ENDMETHOD.
After we give the fixed areas that we will use into the structure we have created, we give this structure to function. We call get_dynamic_fields( ) for dynamic fields as well. Then we give the data reference we obtained to table.
The purpose of using the ZSTATIC_ST static structure is to add the predefined fields to the table. We can use it with dynamic fields to be determined during the runtime. Required fields can be included in table fields by adding them to this structure. Or not use it at all.
METHOD create_dynamic_table.
DATA(dref) = get_all_fields( is_table = 'ZSTATIC_ST' it_type = get_dynamic_fields( ) ).
ASSIGN dref->* TO <table>.
ENDMETHOD.
We decide which fields we want to use with the data from the selection. We assign the field names to the table to be used in the operations and dynamic select. We assign the field names and types for dynamic type into table.
METHOD get_dynamic_fields.
SELECT concat( 'KST', tckh3~el_hv ) AS el_hv,
tckh1~txele
FROM tckh3
INNER JOIN tckh1 ON tckh3~elehk = tckh1~elehk
AND tckh3~elemt = tckh1~elemt
WHERE tckh3~elehk EQ 'Z1'
INTO TABLE @DATA(lt_tckh).
gt_dyn_names = VALUE tt_dyn_name( FOR ls_tckh IN lt_tckh ( name = ls_tckh-el_hv ) ).
IF sy-subrc = 0.
rt_table = VALUE #( FOR ls_tckh IN lt_tckh ( name = ls_tckh-el_hv types = 'MLCCS_D_KSTEL' ) ).
ENDIF.
ENDMETHOD.
We enclose fields from structure in lt_comp. We import dynamic fields from the table containing it into lt_comp. After creating table containing all fields, we return the table type with rv_value.
METHOD get_all_fields.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr,
lt_comp TYPE cl_abap_structdescr=>component_table.
ref_table_des ?= cl_abap_typedescr=>describe_by_name( is_table ).
lt_comp = ref_table_des->get_components( ).
APPEND LINES OF VALUE cl_abap_structdescr=>component_table( FOR is_type IN it_type ( name = is_type-name type = set_datadescr( is_type-types ) ) ) TO lt_comp.
DATA(lo_new_type) = cl_abap_structdescr=>create( lt_comp ).
DATA(lo_new_tab) = cl_abap_tabledescr=>create(
p_line_type = lo_new_type
p_table_kind = cl_abap_tabledescr=>tablekind_std
p_unique = abap_false ).
CREATE DATA rv_value TYPE HANDLE lo_new_tab.
ENDMETHOD.
METHOD set_datadescr.
rv_descr ?= cl_abap_elemdescr=>describe_by_name( iv_types ).
ENDMETHOD.
No comments:
Post a Comment