1
Introduction To Internal Tables
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Internal Tables
Internal tables fulfill the function of arrays. Stores data extracted from database tables. Internal tables can be nested. It consists of Body and Header line. Body – Holds the rows of the internal table. Header line – Has same structure as row of the body holding a single row only. Work Area : To change or output the contents of an internal table, you need a work area. When processing an internal table, the system always fills the work area with the contents of the current table line. You can then process the work area. Header line is the default work area for internal tables with header line
3
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Internal Tables
4
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Declaring Internal Tables TYPE typ OCCURS n Defines an internal table without header line.
Example TYPES: BEGIN OF LINE_TYPE, NAME(20) TYPE C, AGE TYPE I, END OF LINE_TYPE. DATA:
PERSONS TYPE LINE_TYPE OCCURS 20, PERSONS_WA TYPE LINE_TYPE.
PERSONS-NAME = 'Michael'. PERSONS-AGE = 25. APPEND PERSONS_WA TO PERSONS.
5
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Declaring Internal Tables TYPE typ OCCURS n WITH HEADER LINE Defines an internal table with header line. Such a table consists of any number of table lines with the type typ and a header line. Example TYPES: BEGIN OF LINE_TYPE, NAME(20) TYPE C, AGE TYPE I, END OF LINE_TYPE. DATA:
PERSONS TYPE LINE_TYPE OCCURS 20 WITH HEADER LINE.
PERSONS-NAME = 'Michael'. PERSONS-AGE = 25. APPEND PERSONS.
6
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Declaring Internal Tables Referencing data dictionary object With header line DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10 WITH HEADER LINE.
Without header line DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10. DATA FLIGHT_TAB LIKE SFLIGHT.
Including structures You can Include another structure into Internal table. DATA : BEGIN OF T_TAB1 OCCURS 10, FIELDS1 LIKE BKPF-BELNR, FIELDS2 LIKE BSEG-BUZEI, END OF T_TAB1. DATA : BEGIN OF T_TAB2 OCCURS 10. INCLUDE STRUCTURE T_TAB1. DATA : END OF T_TAB2.
In this example, T_TAB2 will also contain the fields FIELD1 & FIELD2.
7
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Filling Internal Tables APPEND Statement APPEND [wa TO | INITIAL LINE TO] itab.
Appends a new line to the end of the internal table itab. If you specify wa TO, the new line is taken from the contents of the explicitly specified work area wa. If you use INITIAL LINE TO, a line filled with the correct value for the type is added. If the specification before itab is omitted, the new line is taken from the internal table itab. After the APPEND, the system field SY-TABIX contains the index of the newly added table entry.
8
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Filling Internal Tables INSERT Statement INSERT [wa INTO | INITIAL LINE INTO] itab [INDEX idx].
Inserts a new line into an internal table. If you specify wa INTO , the new line is taken from the contents of the explicitly specified work area wa. When using INITIAL LINE INTO , a line containing the appropriate initial value for its type is inserted into the table. If you omit the specification before itab , the new line is taken from the header line of the internal table itab. INDEX idx specifies the table index before which the line is inserted into the table itab . 9
www.aspireit.net
Call us 7058198728 / 8856033664
Assigning internal tables Internal tables without header line MOVE ITAB1 TO ITAB2. ITAB2 = ITAB1 Internal tables with header line MOVE ITAB1[ ] TO ITAB2[ ]. ITAB2[ ] = ITAB1[ ].
10
www.aspireit.net
Call us 7058198728 / 8856033664
Extracting data from database table
SELECT c1 c2 … cn|* FROM <dbtable> INTO TABLE <itab> WHERE <condition>.
dbtable – Name of the database table c1,c2,…cn – columns in the database table itab – Internal table that holds the data condition – WHERE clause condition
11
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Retrieving Internal Tables LOOP AT Statement LOOP AT itab. LOOP AT itab INTO wa. Processes an internal table (DATA) in a loop which begins with LOOP and ends with ENDLOOP. Each of the internal table entries is sent to the output area in turn. When LOOP AT itab. is used, the header line of the internal table itab is used as output area. In the case of LOOP AT itab INTO wa , there is an explicitly specified work area wa. If the internal table is empty, all the statements between LOOP and ENDLOOP are ignored. In each loop pass, SY-TABIX contains the index of the current table entry. After leaving a LOOP, SY-TABIX has the same value as it had before.
12
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Retrieving Internal Tables READ Statement READ TABLE itab INDEX idx [INTO WA]. READ TABLE itab WITH KEY <k1> = <f1> <k2> = <f2>… <kn> = <fn> [INTO wa] [BINARY SEARCH]. Reads an internal table entry. An entry can be chosen using a key or its index idx. With "READ TABLE itab.", the header line of the internal table itab is used as the output area; with "READ TABLE itab INTO wa." the explicitly specified work area wa is used for this purpose. For BINARY SEARCH, the internal table itab should be sorted by the keys k1,k2…kn.
13
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Modifying Internal Tables MODIFY Statement MODIFY itab [FROM wa] [INDEX idx]. Changes an entry in the internal table itab . If you specify FROM wa , the line is replaced by the explicitly specified work area wa . If the FROM specification is omitted, the line is replaced by the header line from itab . With INDEX idx, you can specify the table index of the line to be changed. The index specification can be omitted in a LOOP on an internal table. The INDEX specification can also appear before the FROM specification.
Note The counting of table entries begins with 1.
14
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Deleting Internal Tables DELETE Statement DELETE itab. The current entry of the internal table itab is deleted in a LOOP loop. Return code value is set to 0. DELETE itab INDEX idx. Deletes the idx entry from the internal table itab . The return code value is set as follows: SY-SUBRC = 0 The entry was deleted. SY_SUBRC = 4 The entry does not exist.
15
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Deleting Internal Tables DELETE Statement DELETE itab FROM idx1 TO idx2. Deletes the line area from index idx1 to idx2 from internal table itab. At least one of the two parameters FROM idx1 or TO idx2 should be specified. If parameter FROM is missing, the area from the start of the table to line idx2 is deleted. If parameter TO is missing, the area from line idx1 to the end of the table is deleted. Start index idx1 must be greater than 0. The return code value is set as follows: SY-SUBRC = 0 At least one entry was deleted. SY_SUBRC = 4 None of the entries were deleted.
16
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Sorting Internal Tables SORT Statement SORT itab DESCENDING. SORT itab ASCENDING. SORT itab BY f1 f2 ... fi. Sorts the entries of the internal table itab in ascending order. The default key is used as the sort key for internal tables. Sorts itab by the sub-fields f1, f2 , ..., fi which form the sort key. These fields can be any type (even number fields or tables). Unless you specify otherwise, the sort is in ascending order. You can also use additions 1 and 2 before BY if you want all sub-fields to apply. To change the sort sequence for each individual field, specify DESCENDING or ASCENDING after each of the sub-fields f1 , f2 , ..., fi .
17
www.aspireit.net
Call us 7058198728 / 8856033664
CLEAR/REFRESH
CLEAR ITAB. If ITAB is an internal table without a header line, the entire table is deleted together with all its entries. If, however, ITAB is an internal table with a header line, only the subfields in the table header entry are reset to their initial values. To delete the entire internal table together with all its entries, you can use CLEAR ITAB[ ] or REFRESH ITAB.
NOTE: CLEAR f.
Clears the field contents
18
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Retrieving Internal Table attributes DESCRIBE Statement DESCRIBE TABLE itab. ďƒ˜ Returns the attributes of the internal table itab. You must use at least one of the additions listed below. Additions : 1. ... LINES lin Places the number of filled lines of the table t in the field lin. 2. ... OCCURS n Transfers the size of the OCCURS parameter from the table definition to the variable n.
19
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Control Break With Internal Tables All these structures begin with AT and end with ENDAT. The sequence of statements which lies between them is then executed if a control break occurs. AT NEW f. / AT END OF f. f is a sub-field of an internal table processed with LOOP. The sequence of statements which follow it is executed if the subfield f or a sub-field in the current LOOP line defined (on the left) before f has a different value than in the preceding (AT NEW) or subsequent (AT END OF) table line. AT FIRST. / AT LAST. Executes the appropriate sequence of statements once during the first (AT FIRST) or last (AT LAST) loop pass.
20
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Control Break With Internal Tables AT FIRST Statements are executed before any records are processed while looping at Internal table.
Example : LOOP AT itab. AT FIRST. WRITE : SY-ULINE. ENDAT. ENDLOOP.
21
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Control Break With Internal Tables AT LAST Statements are executed after all records are processed while looping at Internal table.
Example : LOOP AT itab. AT LAST. WRITE : SY-ULINE. ENDAT. ENDLOOP.
22
www.aspireit.net
Call us 7058198728 / 8856033664
Report - Control Break With Internal Tables AT NEW <Field Name> Statements are executed at the beginning of a group of records containing the same value for <Field Name>..
Example : LOOP AT itab. AT NEW I_LIFNR. WRITE : SY-ULINE ENDAT. ENDLOOP.
23
24
Report - Control Break With Internal Tables AT END OF <Field Name> Statements are executed at the end of a group of records containing the same value for <Field Name>.
Example : LOOP AT itab. AT END OF I_LIFNR. WRITE : SY-ULINE. ENDAT. ENDLOOP.
Note : AT NEW and AT END OF make sense only for a sorted table.
25
Report - Summation SUM Statement SUM. SUM calculates the control totals of all fields of type I , F and P and places them in the LOOP output area (header line of the internal table or an explicitly specified work area). You can use the SUM statement both at the end and the beginning of a control group Example: LOOP AT itab. AT LAST. SUM. WRITE : itab-fld1. ENDAT. ENDLOOP.
Prints the sum of values of fld1 in all rows of itab. Fld1 should be a numeric type field
26
Report - Summation COLLECT Statement COLLECT [wa INTO] itab. COLLECT is used to summate entries in an internal table. COLLECT = APPEND, if no entries with the same key exists
= Adds the numeric values to their corresponding field
values, if an entry with same key exists
Used to create summarized tables.
27
Report - Summation COLLECT Statement Example Timesheets per employee per day INT_EMPTMSHT – EMPNO(char), EDATE(date), HOURS(integer) Total time per employee for the whole period under consideration INT_TMSHTSUM – EMPNO(char),HOURS(integer)
INT_EMPTMSHT
INT_TMSHTSUM
1000
20040102
5
1000
20040103
7
1001
20040103
8
1001
20040106
3
1000
12
1001
8
28
Report - Summation COLLECT Statement Example LOOP AT INT_EMPTMSHT. MOVE-CORRESPONDING INT_EMPTMSHT TO INT_TMSHTSUM. COLLECT INT_TMSHTSUM. ENDLOOP. INT_TMSHTSUM
INT_TMSHTSUM
INT_TMSHTSUM
INT_TMSHTSUM
1000
1000
1000
12
1000
12
1001
8
1001
11
5
12
INT_EMPTMSHT 1000
20040102
5
1000
20040103
7
1001
20040103
8
1001
20040106
3 29
Thank You For SAP ABAP Online / Classroom Training Please visit our website www.aspireit.net call us on 7058198728 / 8856033664
30