Working with Internal Tables
This is only an overview of internal table operations. Use
the SAP on-line help to get further details.
- Append 2 tables with identical
structure
- Check if there are any entries in an
internal table
- Copy an internal table to another
internal table
- Delete lines
- Delete duplicate entries in internal
table after sort
- Modify line of internal table
- Summarize data into an internal
table
Append 2 tables with identical structure
All rows: append lines of itab1 to itab2
Subset of rows: append lines of itab1 from <rowno> to <rowno> to itab2
Check if there are any entries in an internal
table
If you don't need to know the number of entries in the table, but
only wants to know if there are any entries at all
use:
if itab[] is initial.........
instead of using describe table.
Copy an internal table to another internal
table
Note: The tabels must have exactly the structure
itab2[] = itab2[]
Delete lines
Deleting a single
line
read table itab
with key name = 'My name'
into wa_itab.
if sy-subrc = 0.
delete itab.
endif.
Deleting all
lines
refresh itab.
If you also want to free the mamory taken up by the table, use FREE
instead of REFRESH
Deleting a
subset
This can be done in a loop, but it is better to do it this
way:
delete itab where name = 'My name'.
Remember that you can also use wildcards. E.g. if you want to
delete all name statinmg with 'A':
delete itab where name = 'A*'.
Delete duplicate entries in internal table
after sort
To delete all duplicate entries from a sorted internal table (e.g.
just after SORT), you can use the
DELETE ADJACENT DUPLICATES FROM itab
statement.
You can use the COMPARING adition to limit the fields that are used
to test for duplicate entries e.g.
SORT i_tab by matnr werks logort.
DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr werks.
All duplicates withe same combination of matnr and werks will be
deleted.
Modify line of internal table
modify itab from wa_itab.
modify itab from wa_itab transporting <field1> <field2>
Summarize data into an internal
table
Syntax: COLLECT [wa INTO] itab.
Note: You can only use COLLECT if all of the tables non-key fields
are numeric ( Type I, P, F)
The collect command summarizes all numerical fields (Type I, P, F)
that are not part of the key into an internal table. The level of
summarization is determined by the table key which can be both
numerical and non numerical. After using the COLLECT command
you will have a table with unique keys
Example:
REPORT zcollect.
TYPES:
BEGIN OF t_mytype,
key_c(10) TYPE c,
key_n(10) TYPE n,
key_i TYPE i,
number TYPE i,
END OF t_mytype.
DATA:
gi_mytable TYPE SORTED TABLE OF t_mytype
WITH NON-UNIQUE KEY key_c key_n key_i,
wa_mytable TYPE t_mytype.
START-OF-SELECTION.
CLEAR wa_mytable.
wa_mytable-key_c = '10'.
wa_mytable-key_n = '25'.
wa_mytable-key_i = 5.
wa_mytable-number = 400.
COLLECT wa_mytable INTO gi_mytable.
CLEAR wa_mytable.
wa_mytable-key_c = '10'.
wa_mytable-key_n = '25'.
wa_mytable-key_i = 5.
wa_mytable-number = 500.
COLLECT wa_mytable INTO gi_mytable.
CLEAR wa_mytable.
wa_mytable-key_c = '11'.
wa_mytable-key_n = '30'.
wa_mytable-key_i = 6.
wa_mytable-number = 200.
COLLECT wa_mytable INTO gi_mytable.
CLEAR wa_mytable.
wa_mytable-key_c = '11'.
wa_mytable-key_n = '30'.
wa_mytable-key_i = 6.
wa_mytable-number = 900.
COLLECT wa_mytable INTO gi_mytable.
CLEAR wa_mytable.
wa_mytable-key_c = '11'.
wa_mytable-key_n = '30'.
wa_mytable-key_i = 7.
wa_mytable-number = 100.
COLLECT wa_mytable INTO gi_mytable.
END-OF-SELECTION.
LOOP AT gi_mytable INTO wa_mytable.
WRITE: / wa_mytable-key_c,
wa_mytable-key_n,
wa_mytable-key_i,
wa_mytable-number.
ENDLOOP.
Result:
10 0000000025 5 900
11 0000000030 6 1.100
11 0000000030 7 100
If you remove key_i from the table key the result will be:
10 0000000025 10 900
11 0000000030 19 1.200