Determining the Attributes of Internal Tables
To find the attributes of internal table at runtime that are not available statically, use the statement
DESCRIBE DESCRIBE TABLE <itab> [LINES<L>] [OCCURS <n>] [KIND <k>].
If you use the LINES parameter, the number of filled lines is returned to the variable. l
If you use the OCCURS parameter, the value of the initial size of the table is returned to the variable <n>
If you use the KIND parameter, the table type is returned to the variable <k>; ‘T’ for standard table, ‘S’ for sorted table, ‘H’ for Hashed table.
DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1 INITIAL SIZE 10. DATA: LIN TYPE I, INI TYPE I, KND TYPE C. DESCRIBE TABLE ITAB LINES LIN OCCURS INI KIND KND. WRITE:/ LIN, INI, KND. DO 1000 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX**2. INSERT LINE INTO TABLE ITAB. ENDDO. DESCRIBE ITAB LINES LIN OCCURS INI KIND KND. WRITE:/ LIN, INI, KND.
The output is
0 10 H 1000 10 H
Operations on Internal Tables
1. Filling a table line by line
2. Reading a table line by line
3. Modifying Individual lines
4. Deleting individual lines
Influence of the Table type
Index tables (Standard and Sorted tables)
These tables have an internal index making linear access possible Hashed tables have no linear index. Only key access is possible for hashed tables. The operations that are permitted for all table types do not use indexes. They can also be used in procedures or with field symbols where the internal table type is not fully typed. These operations are known as generic operations.
Use modify to change lines of an index table, use MODIFY TABLE to change lines of any kind of table.
ACCESS METHODS There are 2 ways to access a single table entry: Access Using a Work Area when you access individual table entries using a work area, you are not working directly with the data in the table. Instead, you work with another data object as a work area. Use work area compatible with the line type of the internal table When you read data from a a table record, the data you are reading overwrites the current contents of the work area. You can then use this data in the program.
When you write data to the internal table, this must first be placed in the work area. The system then transfers it to the appropriate table entry. Data transfer follows the rules of assigning data using MOVE. If the internal table has a header line, you can use it as Work area.
The ABAP statements that you use to access individual table entries can use the header line implicitly as a work area. If you access internal table using field symbol, you do not need to copy the data into a work area.
Operations for all table types
For performance reasons, Use APPEND…TO statement to fill index tables (standard and sorted tables)
Use INSERT….INTO table to fill hashed or generic tables.
Inserting lines into tables either insert a single line or in groups
Inserting a single line
Use the statement below to insert a single line into table
INSERT <line> INTO TABLE <itab>.
<line> is either a work area that is compatible with the line type, or the expression INITIAL LINE.
This inserts a blank line containing the correct initial value for each field of the structure. If the table has a unique key and you attempt to insert lines whose key already exists in the table, the system does not add the line to the table and sets SY-SUBRC to 4. When the system successfully adds a line to the table, SY-SUBRC is set to 0.
Lines are added to the internal tables as follows –
Standard tables – The line is appended at the end of the table. This is same as APPEND statement.
Sorted Tables The line is inserted into the table according to its key. If the key is non unique, duplicates are inserted above the table entry with same key.
The runtime of the operation increases logarithmically with the no. of existing table entries.
Hashed tables inserted into internal hash administration according to table key
Inserting several lines at a time
INSERT LINES OF <itab1> [FROM <n1>] [TO<n2>] INTO TABLE <itab2>.
<itab1> and <itab2> are tables of compatible line type. This method is faster than single insertion in a LOOP by almost 20 times (depends on the table size and where they are inserted)
This example fills a sorted internal table with 6 entries.
DATA: BEGIN OF LINE, LAND(3) TYPE C, NAME(10) TYPE C, AGE TYPE I, WEIGHT TYPE P DECIMALS 2, END OF LINE. DATA ITAB LIKE SORTED TABLE OF LINE WITH NON-UNIQUE KEY LAND NAME AGE WEIGHT. LINE-LAND = 'G'. LINE-NAME='Hans'. LINE-AGE = 20. LINE-WEIGHT = '80.00'. INSERT LINE INTO TABLE ITAB. LINE-LAND = 'USA'. LINE-NAME = 'Nancy'. LINE-AGE = 35. LINE-WEIGHT = '45.00'. INSERT LINE INTO TABLE ITAB. LINE-LAND = 'USA'. LINE-NAME = 'Howard'. LINE-AGE = 40. LINE-WEIGHT = '95.00'. INSERT LINE INTO TABLE ITAB. LINE-LAND = 'GB'. LINE-NAME = 'Jenny'. LINE-AGE = 18. LINE-WEIGHT = '50.00'. INSERT LINE INO TABLE ITAB. LINE-LAND = 'F'. LINE-NAME = 'Michele'. LINE-AGE = 30. LINE-WEIGHT = '60.00'. INSERT LINE INTO TABLE ITAB. LINE-LAND = 'G'. LINE-NAME = 'Karl'. LINE-AGE = 60. LINE-WEIGHT = '75.00'. INSERT LINE INTO TABLE ITAB. LOOP AT ITAB INTO LINE. WRITE:/ LINE-LAND, LINE-NAME, LINE-AGE, LINE-WEIGHT. ENDLOOP.
The output is sorted 3.
This example creates 2 tables with same line types but different table types.
When unsorted data in inserted in sorted table, the entries get sorted
DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA: ITAB LIKE STANDARD TABLE OF LINE, JTAB LIKE SORTED TABLE OF LINE WITH NON-UNIQUE KEY COL1 COL2. DO 3 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX **2. APPEND LINE TO ITAB. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX**3. APPEND LINE TO JTAB. ENDDO. INSERT LINES OF ITAB INTO JTAB. LOOP AT JTAB INTO LINE. WRITE: / LINE-COL1, LINE-COL2. ENDLOOP.
Reading lines of tables to read the single line of any table READ TABLE <itab> <key> <result>.
For the above statement to be valid for any kind of table, you must specify the entry using the key and not the index. SY-TABIX is set to the index of the line retrieved and SY-SUBRC is set to 0. If the table has non unique keys and there are duplicate entries, the first entry is read.