An Internal table is a temporary table gets created in the memory of application server during program execution and gets destroyed once the program ends. It is used to hold data temporarily or manipulate the data. It contains one or more rows with same structure.
An internal table can be defined using the keyword TABLE OF in the DATA statement. Internal table can be defined by the following ways.
TYPES: BEGIN OF ty_student, id(5) TYPE n, name(10) TYPE c, END OF ty_student. DATA: gwa_student TYPE ty_student. "Referring to local data type DATA: it1 TYPE TABLE OF ty_student. "Referring to local data object DATA: it2 LIKE TABLE OF gwa_student. "Referring to data type in ABAP dictionary DATA: it3 TYPE TABLE OF mara.
Use the APPEND statement to add data to internal table. First define the work area i.e. define a field string with a structure similar to row of the internal table. Then place the data in the work area and use the APPEND statement to add the data from work area to internal table.
*--------------------------------------------------------------* *Data Types *--------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(5) TYPE n, name(10) TYPE c, END OF ty_student. DATA: gwa_student TYPE ty_student. *--------------------------------------------------------------* *Data Declaration *--------------------------------------------------------------* "Referring to local data type DATA: it TYPE TABLE OF ty_student. gwa_student-id = 1. gwa_student-name = 'JOHN'. APPEND gwa_student TO it. gwa_student-id = 2. gwa_student-name = 'JIM'. APPEND gwa_student TO it. gwa_student-id = 3. gwa_student-name = 'JACK'. APPEND gwa_student TO it.
After the last APPEND statement in the above program, internal table ‘IT’ has the following 3 entries. But the internal table values are not persistent i.e. the internal table and its values are discarded once the program ends.
ID | Name |
---|---|
1 | JOHN |
2 | JIM |
3 | JACK |
Usually internal tables are used to hold data from database tables temporarily for displaying on the screen or further processing. To fill the internal table with database values, use SELECT statement to read the records from the database one by one, place it in the work area and then APPEND the values in the work area to internal table.
DATA: gwa_employee TYPE zemployee, gt_employee TYPE TABLE OF zemployee. SELECT * FROM zemployee INTO gwa_employee. APPEND gwa_employee TO gt_employee. ENDSELECT.
After ENDSELECT the internal table GT_EMPLOYEE contains all the records that are present in table ZEMPLOYEE.
Using INTO TABLE addition to SELECT statement we can also read multiple records directly into the internal table directly. No work area used in this case. This select statement will not work in loop, so no ENDSELECT is required.
SELECT * FROM zemployee INTO TABLE gt_employee.