Thursday, March 23, 2023

ABAP CODE 1: Dynamic Table and STructure

 Inserting:

INSERT LINES OF <itab1> INTO <itab2> INDEX <idx>.

INSERT LINES OF <itab1>  FROM <n1>  TO <n 2> INTO <itab2> INDEX <idx>.

INSERT LINES OF ITAB INTO JTAB INDEX 1.

Dynamic:

DATA: lo_dynamic_table_1 TYPE REF TO DATA,

            lo_dynamic_workarea_1 TYPE REF TO DATA,

            lv_table_name TYPE STRING.

FIELD-SYMBOLS: <lt_table_structure> TYPE TABLE,

                                  <ls_table_structure> TYPE ANY.

lv_table_name = 'PA0002'.

"This will create a table of same fields as of SFLIGHT

CREATE DATA lo_dynamic_table_1 TYPE TABLE OF (lv_table_name).

ASSIGN lo_dynamic_table_1->* TO <lt_table_structure>. 


"This will workarea of same fields as of SFLIGHT

CREATE DATA lo_dynamic_workarea_1 LIKE LINE OF (lv_table_name).

ASSIGN lo_dynamic_workarea_1->* TO <ls_table_structure>.



Assign Components

CASE 1: 

LOOP AT ITAB ASSIGNING <fs-structure>.

"determination of field name logic goes here let's say you have 

"field name is in variable lv_field

    ASSIGN COMPONENT (lv_field) of STRUCTURE <fs-structure> to <fs-field>.

    IF <fs-field> IS ASSIGNED.

      <fs-field> = 'the value you want to assign'.

    ENDIF.

ENDLOOP.

CASE 2:
LOOP AT ITAB ASSIGNING <fs-structure>.

*-- determination of field name logic goes here let's say you have 
*-- field name is in variable lv_field1 and other is lv_field2

    ASSIGN COMPONENT (lv_field1) of STRUCTURE <fs-structure> to <fs-field1>.

    ASSIGN COMPONENT (lv_field2) of STRUCTURE <fs-structure> to <fs-field2>.

    IF <fs-field1> IS ASSIGNED ANDV<fs-field2> IS ASSIGNED .
      <fs-field1> = <fs-field1> + <fs-field1>.
    ENDIF.

ENDLOOP.
CASE 3:
TYPES: BEGIN OF ts_basic_line,
     matnr TYPE mara-matnr,
     lifnr TYPE lfa1-lifnr,
   END OF ts_basic_line,
   ty_basic_line TYPE TABLE OF ts_basic_line.

DATA: lt_basic_data TYPE ty_basic_line,
      lo_structure  TYPE REF TO cl_abap_structdescr,
      lt_components TYPE abap_component_tab.

lt_basic_data = VALUE #( ( matnr = '111' lifnr = '333' ) ). " data for test
READ TABLE lt_basic_data ASSIGNING FIELD-SYMBOL(<fs_line>) INDEX 1.
IF sy-subrc EQ 0.
  lo_structure ?= cl_abap_typedescr=>describe_by_data( <fs_line> ).
  lt_components = lo_structure->get_components( ).

  LOOP AT lt_components ASSIGNING FIELD-SYMBOL(<fs_strucutre_fields>).
    LOOP AT lt_basic_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      ASSIGN COMPONENT <fs_strucutre_fields>-name OF STRUCTURE <fs_data> 
                                               TO FIELD-SYMBOL(<fs_value>).
      IF sy-subrc EQ 0.
        " here your code, e.x. data conversion
        <fs_value> = |{ <fs_value> ALPHA = IN }|.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
ENDIF.

CASE 4: Multiple Dynamic Tables in a single table
DATA: lt_dynamic_tables TYPE TABLE OF REF TO DATA.
            ls_dynamic_table  TYPE REF TO DATA.
FIELD-SYMBOLS: <fs_dynamic_table>  TYPE ANY TABLE.

" Store the lists of Dynamic Table
DO 10 TIMES.  "(as required)
  CREATE DATA ls_dynamic_table TYPE TABLE OF PA0002. 
  APPEND ls_dynamic_table TO lt_dynamic_table. 
ENDDO.

" Read the list of dynamic tables
LOOP AT lt_dynamic_table INTO ls_dynamic_table.
  ASSIGN ls_dynamic_table->* TO <fs_dynamic_table>.
" This will aloo to reach each table data
ENDLOOP.