Declaring internal tables is an essential part of writing ABAP code as this is where most of the data retrieved
from database tables will be stored. During the select statement you retrieve data from a database table into
an internal table (multiple rows) or a work area or header line (single row).
When building an internal table you first need a row/structure type definition which defines all the field types of the table. For this you could use any table or structure already created within the data dictionary (SE11). For example if you wanted to create a table with a row structure like EKPO you would simply use the following ABAP code:
data: it_data TYPE STANDARD TABLE OF EKPO INITIAL SIZE 0.
This creates an internal table called it_data with the same structure as EKKO. The initial size 0 simple tells the program not to reserve any space for it upfront and allocate it as and when filled. Also if you wanted an internal 1 line work area with this structure you could also reference the dictionary object using the following statement:
"Creates a local work area called wa_ekpo data: wa_ekpo TYPE EKPO. "or this code creates a local work based on an existing itab wa_ekpo1 like line of it_data. "or the TABLES statement creates a local work area called ekpo TABLES: EKPO.
Define structure or table using declarations within your ABAP
What if you don't want your table to include all the fields from EKPO but just 2 such as EBELN and EBELP or if you wanted fields from different dictionary tables. Well you then have 2 choices you either create a new dictionary object with the fields you want with SE11 and then reference it as above or you build the table using ABAP declarations within your report.
Firstly you need to define the structure of the table which is basically just a list of fields and their type. To do this use the following ABAP code:
TYPES: BEGIN OF t_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, "more fields can be added here END OF t_ekpo.
The 'TYPES: BEGIN OF' statement defines the start of this declaration and the t_ekpo is the name of the structure and can be anything you want. The 'END OF t_ekpo.' statement defines the end of the structure definition. You now have a structure called t_ekpo that can be used in the same way as the dictionary object EKKO was used above:
data: it_ekpo TYPE STANDARD TABLE OF t_ekpo, wa_ekpo TYPE t_ekpo.
Build internal table and work area from existing structure and adding additional fields
A structure can be built based on an existing dictionary structure such as EKKO with a few extra fields being added to it using the following ABAP syntax.
TYPES: BEGIN OF t_repdata. INCLUDE STRUCTURE ekko. TYPES: ebelp TYPE ekpo-ebelp, werks TYPE ekpo-werks. TYPES: END OF t_repdata. TYPES: END OF t_repdata. DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab wa_repdata TYPE t_repdata. "work area (header line)
Old way of declaring internal tables and structures
Please note this information is just so you can understand code which was originally written when this was the way to do it. Do not use this method when creating new reports as it does not work in some of the newer technologies such as objects and web dynpro so if you are new to ABAP you might as well get used to the new method from the start, plus the new method is easier to understand anyway.
In the early days of ABAP development there was a slightly different concept to declaring a multi line table and its single lined header / work area. Basically when you create a table using this old method a header line is automatically created with the same name and depending on how you referenced it, and the statement used would determine the appropriate element to be accessed. For example CLEAR would initialise the header element, REFRESH would initialise the table element, MOVE-CORRESPONDING moved the header element, APPEND adds to the table element etc
When declaring using this old method it also declares the TYPES and DATA described above in one go just using DATA and with the addition of the OCCURS statement or not to determine if you want a table with a header line or just a header line/ work area.
DATA: BEGIN OF tab_ekpo, "Work area declaration / Header line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF tab_ekpo.
DATA: BEGIN OF tab_ekpo OCCURS 0, "Table declaration with header line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF tab_ekpo.
If you wanted to reference a dictionary object (SE11) such as EKPO then you would use the following syntax:
Data: it_tabekpo LIKE ekpo OCCURS 0, "Table with header line wa_tabekpo LIKE ekpo. "Work area
*&-------------------------------------------------------------* *& Report ZTYPES * *& * *&-------------------------------------------------------------* REPORT ZTYPES . *Table declaration (new method) "USE THIS WAY!!! TYPES: BEGIN OF t_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF t_ekpo. DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itab wa_ekpo TYPE t_ekpo, "work area (header line) wa_ekpo1 LIKE LINE OF it_ekpo. * Build internal table and work area from existing table with * additional fields TYPES: BEGIN OF t_repdata. INCLUDE STRUCTURE ekko. "could be an itab such as tab_ekpo TYPES: ebelp TYPE ekpo-ebelp, werks TYPE ekpo-werks. TYPES: END OF t_repdata. DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab wa_repdata TYPE t_repdata. "work area (header line) * Table declaration (old method) DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF tab_ekpo. Data: it_tabekpo LIKE ekpo OCCURS 0, "Table with header line wa_tabekpo LIKE ekpo. "Work area * Build internal table from existing dictionary structure (old method) DATA: it_datatab LIKE ekko OCCURS 0. ************************************************************************** *START-OF-SELECTION. START-OF-SELECTION. *Select data in itab SELECT ebeln ebelp from EKPO into table it_ekpo. *Process data within itab using LOOP statement Loop at it_ekpo into wa_ekpo. write: wa_ekpo-ebeln. * processing...... endloop. *Select data in itab declared the old way SELECT ebeln ebelp from EKPO into table tab_ekpo. Loop at tab_ekpo. write: tab_ekpo-ebeln. * processing...... endloop.