6. Filling an internal table – Example 1 A table customers has the following fields : cnum customer number cname customer name caddress customer address cphone customer phone no. report intabex. tables: customers. data: begin of itabcust occurs 0, cnum like customers-cnum, cname like customers-cname, end of itabcust. select * from customers. move-corresponding customers to itabcust. append itabcust. endselect.
7. Filling an internal table – Example 2 A table customers has the following fields : cnum customer number cname customer name caddress customer address cphone customer phone no. report intabex. tables: customers. data: begin of itabcust occurs 0, cnum like customers-cnum, cname like customers-cname, end of itabcust. select cnum cname from customers into table itabcust.
8.
9.
10.
11.
12.
13.
14.
15.
Editor's Notes
Multitudes of records can be processed using internal tables – a very important part of ABAP programming ! Because internal tables are held in the current program’s memory space, the access time to read and process the data stored inside the internal tables is significantly less than a read of the database table. The trade-off is CPU time. The header record space holds a copy of whichever record has been read or modified, as well as any new record being added to the table. Individual data records in an internal table are known as table rows or table entries. Individual components in a row are referred to as columns of the internal table.
To define an internal table body, use occurs n on the definitions of any field string. To define an internal table with a header line, you must include either begin of or with header line in the definition. The only time you would create an internal table without a header line is in the case of a nested internal table. The body of the internal table and the header line have the same name. Where you use that name determines what you are referring to. Example : data: begin of table1 occurs 0, fld1(1), fld2(2), end of table1. The fieldnames in the above example will be prefixed by the table name. Example : table1-fld1. If you cannot estimate the size of a table, set it to zero. The system will increase it as required. A table set to occurs 0 takes up 8-16K,
You can read specific fields directly into an internal table using: select no name from vendor into table vendtab. Normally the previous content of the internal table is overwritten by the new data. If appending is used, the new lines are added at the end of the table. The append statement adds the content of the header line to the end of the internal table. The contents of an internal table can be appended to another table using the addition lines of . e.g. append lines of table1 from 10 to 20 to table2.
In example 1, the order is implicitly determined by the sequence of fields in the line structure of the record; i.e. the left-most column represents the highest sort criterion. Note that only non-numeric fields are considered as sort criteria in this form of the statement. In example 2, studname takes precedence, within that the table is sorted by studid.
Example 1. These statements display the student id and name for all entries of the internal table stud_tab. During the loop, the header line is filled with the current line of the table at each step. Example 2. The where clause can support all conditional operators that are supported for the if statement. During the loop the current line number is held in sy-tabix. Loops in internal tables can be nested. In addition you can specify the range of lines you want to work with. e.g. loop at stud_tab from 10 to 20.
Reading single lines. Example 3. This reads the fifth line in the table. If the table does not have at least 5 entries, the return code sy-subrc returns a non-zero result. Example 4. If this read is successful, the index of the line is returned in sy-tabix. Example 5. Use of multiple keys. Example 6. The binary search algorithm requires that the table entries are sorted according to the specified key. This is a much faster way of locating entries in large tables.
Insert : e.g. insert stud_tab index sy-tabix. Inserts before the current line. Delete : e.g. delete stud_tab index sy-tabix.