Se also Loop at with where clause - Do and dont's
Internal table with header line
Loop at itab.
itab-qty = 0.
modify itab.
endloop.
Internal table without header line
Note: You must create a workarea with same line type as the table
loop at itab into wa_itab.
wa_itab-qty = 0.
modify itab from wa_itab.
endloop.
2. 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
You can use the COMPARING 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 with same combination of matnr and werks will be deleted.
3. Appending an internal table to another
Note that the two tables must have the same structure
The area the source table that will be appended can be restricted by using FROM index1 TO index2
APPEND LINES OF itab FROM index1 TO index2 TO table2.
4. Copy an internal table to another internal table
Note: The tables must have exactly the structure
itab2[] = itab2[]
5. 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.
6. Summarize dats 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 of collect
7. Modify, Insert, and Delete entries in an internal table
Modifying a single line
read table itab
with key name = 'My name'
into wa_itab.
wa_itab-age = 25.
modify itab from wa_itab transporting age.
Note: If you want to modify the whole table line, you can commit TRANSPORTING.
Modifying a set of lines
loop at itab into wa_itab where ........
wa_itab-name = 'My name'.
wa_itab-age = 40.
modify itab from wa_itab.
endloop.
If a subset of entries in the table should have the same values, it is better to use:
wa_itab-name = 'My name'.
wa_itab-age = 40.
modify itab from wa_itab transporting name age where .............
Notes: If you commit the where clause, a runtime error occurs.
If the table has the type SORTED TABLE or HASHED TABLE, the TRANSPORTING list may not contain key fields.
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 memory 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 names starting with 'A':
Delete itab where name = 'A*'.
Reading database tables into internal tables
Performance tips:
• Only read the fields that you need. If you don't need all the fields avoid using SELECT *
• Avoid nested select for large numbers of entries
• Use a WHERE clause instead of CHECK
• Use as meny keyfields as possible in the WHERE clause
• If you only want to evaluate the selected data once, you should read it into a work area. Reading it into an internal table would incur additional costs for the handling of internal tables and also use more memory space.
• If you want to read the data into an internal table, it is better to do this in a single operation than to read it line-by-line in a SELECT loop and then use APPEND to append it to an internal table.
• You should only use the variant ... INTO CORRESPONDING FIELDS ... with large volumes of data because otherwise the time required to compare the field names in the name table is too high.
Efficient Method:
select carrid connid fldate
from sflight
into itab
where carrid = 'LH'.
If you use INTO
select carrid connid fldate
from sflight
INTO CORRESPONDING FIELDS OF TABLE itab
where carrid = 'LH'.
If you want to append entries to a table which allready has entries use
select carrid connid fldate
from sflight
APPENDING CORRESPONDING FIELDS OF TABLE itab
where carrid = 'LH'.
or
select carrid connid fldate
from sflight
APPENDING itab
where carrid = 'LH'.
No comments:
Post a Comment