How to use Table Expressions to modify internal table

              It is much similar to the READ TABLE technique. The table expressions can be used from ABAP 7.40 SP02 version. Particular data record is assigned to the field symbol. The pointers refers to the respective line of record in the internal table.

DATA lt_internal_table TYPE TABLE OF ztt_db_table2.

FIELD-SYMBOLS <ls_record> TYPE ztt_db_table2.

SELECT *FROM ztt_db_table2 INTO TABLE lt_internal_table.

ASSIGN lt_internal_table[2] TO <ls_record>.

<ls_record>-contact_address = ‘Street 44’.

The Data record at the second position of the internal table is modified.

‘Inline declaration’ can also be used during this modification.

ASSIGN lt_internal_table[2] TO FIELD-SYMBOL(<ls_rec>).

How to Delete records from the internal table in SAP ABAP

The Data records present in the internal table can be deleted in following ways.

  • Key or work area (structure)
  • WHERE conditions.
  • Index
  • From Index TO Index
  • Clean, Refresh, free

DATA lt_internal_table TYPE TABLE OF ztt_db_table2.

DATA ls_structure TYPE ztt_db_table2.

SELECT *FROM ztt_db_Table2 INTO TABLE lt_internal_table.

              ls_structure – contact_id = 102.

              ls_structure – contact_name = ‘Thiru’.

              ls_structure – contact_address = ‘Street 10’.

Key or Work Area:

DELETE TABLE lt_internal_Table FROM ls_structure.

Only for key or structure, ‘TABLE’ is written explicitly after DELETE in the syntax.

WHERE:

DELETE lt_internal_table WHERE contact_id = 102.

INDEX:

DELETE lt_internal_table INDEX 2.

FROM INDEX TO INDEX:

DELETE lt_internal_table FROM 2 TO 3.

If the deletion is successful, then the system field sy-subrc will hold the value 0. If it is not successful, it will hold the value 4.

How to use LOOP AT GROUP BY statement in SAP ABAP

The Group by is used to group a set of records from the internal table. It is similar to the old programming Technik AT ……. ENDAT. If you are new to SAP ABAP, please skip this part as it is an Advanced Technik.

Please remember the following points for the Group by definition.

  • There is always an enclosed loop to read the contents of the group.

As shown in above figure, the group by is defined in the outer loop. Group contents are read in inner loop.

  • It is grouped based on the key components. It is accessed from the assigned field-symbol.
  • Use LOOP AT Group by for the inner loop.

The following code can be used when each line of record in the group must be processed.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_con>) GROUP BY  ( key1 = <ls_con>-contact_id ).

WRITE:/ ‘CONTACT id:’, <ls_con>-contact_id.

         WRITE:/ ‘CONTACT Name:’, <ls_con>-contact_name.

LOOP AT GROUP <ls_con> ASSIGNING FIELD_SYMBOL(<ls_group_data>).

APPEND <ls_group_data> TO lt_group_contact.

         ENDLOOP.

ENDLOOP.

LOOP AT lt_group_contact ASSIGNING FIELD-SYMBOL(<ls_check_group>).

           WRITE:/ <ls_check_group>-contact_id.

ENDLOOP.

Group-Key Binding: –

  • When each line of record in the group need not be processed, then the Group-key binding is considered. It can be used to explain the results of the group. In combination with ‘WITHOUT MEMBERS’, each lines of record does not need to stored additionally.
  • It will have two assignments in the LOOP AT line.
  • The Group size and Group index can be used to extract additional details such as the size of the group, Index of the group.
  • The contents of the group are accessed through the key names defined in the Grouping at the LOOP AT line. It is accessed through the second field-symbol assignment and not through the first field-symbol assignment.
  • LOOP AT Group can only be used, when ‘WITHOUT MEMEBRS’ is not mentioned.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

DATA lt_group_Contact TYPE TABLE OF ztt_db_table2.

SELECT *FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_contact>) GROUP BY

                                                        ( cont_id_key1 = <ls_contact>-contact_id

                                                          cont_name_key2 = <ls_contact>-contact_name

                                                          size_of_group = GROUP SIZE

                                                          index_line_group = GROUP INDEX )

                            WITHOUT MEMBERS

                            ASSIGNING FIELD_SYMBOL(<ls_group>).

              WRITE:/ ‘Contact id:’, <ls_group>-cont_id_key1.

              WRITE:/ ‘Contact Name:’, <ls_group>-cont_name_key2.

              WRITE:/ ‘Size of the group:’, <ls_group>-size_of_group.

              WRITE:/ ‘Index of the group:’, <ls_group>-index_line_group.

END LOOP.

WRITE:/ ‘END’.

The following code is an example without ‘WITHOUT MEMBERS’

DATA lt_contact TYPE TABLE OF ztt_db_table2.

DATA lt_group_Contact TYPE TABLE OF ztt_db_table2.

SELECT *FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_contact>) GROUP BY

                                                        ( cont_id_key1 = <ls_contact>-contact_id

                                                          cont_name_key2 = <ls_contact>-contact_name

                                                          size_of_group = GROUP SIZE

                                                          index_line_group = GROUP INDEX )

                            ASSIGNING FIELD_SYMBOL(<ls_group>).

              WRITE:/ ‘Contact id:’, <ls_group>-cont_id_key1.

              WRITE:/ ‘Contact Name:’, <ls_group>-cont_name_key2.

              WRITE:/ ‘Size of the group:’, <ls_group>-size_of_group.

              WRITE:/ ‘Index of the group:’, <ls_group>-index_line_group.

END LOOP.

WRITE:/ ‘END’.