ABAP Internal table declaration - Various methods of creating internal data structures and tables
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
Example report showing full ABAP syntax and use of table declarations described above
*&-------------------------------------------------------------* *& 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.
ABAP Programming EVENTS in SAP
ABAP Function Module basics in SAP
DATA and @DATA Inline ABAP declarations available from release 7.40 to help make your code cleaner and more readable
ABAP Workbench Programming Techniques - BC402
ABAP rules to consider before creating a bespoke abap report or program
ABAP Subroutine basics in SAP
Get access to an SAP system for individual needs
SAP Base 64 encoding and decoding using ABAP code
Call web URl from ABAP report or SAP help documentation
Direct download, downloading sap objects
SAP icons and some ABAP code to display them
SAP icons list and their associated codes
Internal Program Environment diplays all internal/external objects and operations used by an SAP program
minisap installation on your local pc to allow ABAP development, your own local SE80
SAP minisap installation on your local pc to allow ABAP development for free
SAP module based information such FI, HR, MM, PM, BW etc
Need an SAP ABAP program created?
SAP repository objects - List of useful standard and bespoke SAP repository objects
Retrieve SAP objects using transport entry in SE10 to restore objects that have been deleted
SAP Help for all areas for SAP ABAP Development inc ABAP, ALV, Web dynpro, bsp, HR, BW
ABAP tutorial - Logging into an SAP system for the first time
Manage and delete SAP sessions using ABAP code
SAP module areas
Increase & Decrease size of SAP editor text
ABAP development information and code examples for producing be-spoke SAP functionality
ABAP Development Info - Example code and information on various areas of ABAP development
SAP command field entries - box in top left corner
Force new page when printing abap source code
ABAP FIELD SYMBOL - Techniques for manupulating data using the FIELD-SYMBOL statement
Hiding ABAP Source Code so that it can not be viewed by anyone
Parameter ID - ABAP code to demonstrate how to set and get a parameter ID
RANGE statement - Example ABAP code to demonstrate the RANGE command
Change SAP logo in top right hand corner of SAP client
ABAP SELECT statement within SAP - Example ABAP code to demonstrate the SELECT command
Create desktop Shortcut to SAP function
SAP Note assistant - Using transaction SNOTE to read and apply OSS note fix
VARYING command - Example ABAP code to demonstrate the VARYING command
Creating your first helloworld ABAP report in SAP