chapter 07_data structures & internal tables

37
IBM Global Business Services © IBM Corporation 2013 Data Structure & Internal Tables | Dec-2008 Data structures and Internal tables

Upload: bakkalibilal

Post on 20-Nov-2015

20 views

Category:

Documents


4 download

DESCRIPTION

Chapter 07_Data Structures & Internal Tables

TRANSCRIPT

Data structures and Internal tablesData Structure & Internal Tables |
IBM Global Business Services
Create a Structure in an ABAP Program
Create an Internal Table in an ABAP program
Populate an Internal Table with data
Read Database information into an Internal Table
Data Structure & Internal Tables |
IBM Global Business Services
Structures
Structures in code are similar to structures in the Dictionary in that they are simply field strings. However, they are not dictionary objects, but temporary objects in program memory.
Structures are used in connection with sequential datasets and
subroutines as well as a “staging area” for internal tables.
Internal Tables
Internal tables are comprised of records which have the same format as an individual structure.
Internal tables are tables which only exist during the execution of a program.
Unlike the ABAP Dictionary transparent tables, internal table data is not stored on the database server. Internal table data resides in
a program specific work area.
Data can only be entered in an internal table by going through a “staging area”. A “staging area” can either be a header line or a work area, either of which has the same structure as the internal table itself.
IBM Global Business Services
1 REPORT YN1C0008.
4 FLAG TYPE C,
5 ID TYPE TABNA-ID,
6 NAME1 TYPE TABNA-NAME1,
7 CITY TYPE TABNA-CITY,
8 END OF ADDRESS.
12 MOVE ‘Philadelphia’ TO
Data Structure & Internal Tables |
Structures are defined with the DATA statement. The start and end of the structure are indicated by BEGIN OF <FSname> and END OF <FSname>.
Fields in a structure are defined as individual fields; you must specify the length, and data type. You can also define default values for components of structures. With the LIKE parameter, it is possible to adopt the attributes of internal fields which have already been declared or the attributes of fields defined in the ABAP Dictionary.
Reference fields in a structure as follows:
<Structure>- <field name>
e.g. YCUSTOMER-COUNTRY
It is possible to reference a complete Structure (for example, WRITE: <structure>.). Note that the complete structure is then considered as a field of type C. Type conversion does not take place. If the structure contained any packed fields, the WRITE ADDRESS statement would, therefore, not produce meaningful output. Thus We need to use WRITE: <structure-field1>, <structure-field2>,..)
IBM Global Business Services
Basic Syntax:
Data Structure & Internal Tables |
A structure can also be defined using a combination of the TYPES and DATA statements.
The TYPES statement defines a data type which in this example includes the fields: flag,
id, name1, and city. It does not allocate any memory for these fields.
The DATA statement defines a variable using the data type defined with the TYPES
statement. At this point, memory is allocated and the structure is present in the
program’s work area.
This method is preferable to the previous one, especially when it comes to building an internal table from a structure.
IBM Global Business Services
REPORT Y170DM37.
ADDRESS-ID = WA_EMPL-ID. ADDRESS-NAME = WA_EMPL-NAME. ADDRESS-CITY = WA_EMPL-CITY.
WRITE: / ADDRESS-FLAG,
ADDRESS-ID, ADDRESS-NAME,
Data Structure & Internal Tables |
The statement MOVE-CORRESPONDING <f1> to <f2> transports values field by field between the ABAP data structures f1 and f2 for all sub-fields with the same field names. The system searches in f1 for all single fields that also occur in f2 and generates a statement for all pairs of fields found: MOVE <f1>-<field name> TO <f2>-<field name>. The other fields remain unchanged. But this Statement is OBSOLETE . Hence We need to pass field by field value explicitly as shown above.
The CLEAR statement resets all fields to their initial value.
Performance Tips:
MOVE-CORRESPONDING is less efficient than a specific MOVE statement for each field in the
work area/data structure. If the data structures f1 and f2 are the same and the sequence and
attributes of all fields are identical, you can just use the statement MOVE <f1> to <f2> to transport
the values of all fields from f1 to f2.
TIPS
Demonstration
Declaring a structure and populating the structure with values inside a program.
Data Structure & Internal Tables |
IBM Global Business Services
Practice
Declaring a structure and populating the structure with values inside a program.
Data Structure & Internal Tables |
IBM Global Business Services
Data Structure & Internal Tables |
We will see in the next few slides how to expand a structure into an object called an internal table, that can store records of data temporarily during the processing of a program.
As of the 4.0 release, there are 3 different types of internal tables: Standard, Sorted, and Hashed.
Although we will not be discussing Sorted and Hashed tables in detail, the following are some basic differences between the table types:
The method of appending data.
The algorithm used in retrieving data.
There are various performance benefits for each of the internal table types.
When creating any of the internal table types, we have two further options:
Defining a key
Choosing between having the work area attached to, or independent of the table.
The single kind of internal table used in the past is now considered a Standard internal table. These are the ones we will use in the course.
IBM Global Business Services
REPORT Y170DM40.
OF EMP,
APPEND EMPTAB_WA TO EMPTAB.
APPEND <work area> to <EMPTAB>.
The TYPES statement defines the structure and data type for the internal table and its work area
Work Area
Data Structure & Internal Tables |
To create an internal table without a header line, the programmer must:
Create an internal table data type with the TYPES statement. This type will become the
structure of the internal table.
Once the TYPE is created, a DATA statement is coded to identify the actual internal table
object with a name and an INITIAL SIZE clause. The internal table inherits the fields and
attributes from the TYPES statement defined earlier.
In addition to the internal table definition with the INITIAL SIZE clause, the programmer
must also define an internal table work area (staging area). This work area is defined
using the same format as the table. It uses the same user-defined data type as the
internal table.
The work area is loaded programmatically then appended to the internal table.
To load an internal table without a header line, the following APPEND statement is used:
APPEND <work area> to <internal table>.
IBM Global Business Services
REPORT Y170DM40.
EMPTAB_WA TYPE EMP.
SELECT * FROM EMPLOYEE.
APPEND <work area> to <EMPTAB>.
The TYPES statement defines the structure and data type for the internal table and its work area
Work Area
The DATA statement with an INITIAL SIZE creates the actual internal table without a header line. The DATA statement without the INITIAL SIZE creates the work area for the internal table.
ID NAME1 COUNTRY
Data Structure & Internal Tables |
To create an internal table without a header line, the programmer must:
Create an internal table data type with the TYPES statement. This type will become the
structure of the internal table.
Once the TYPE is created, a DATA statement is coded to identify the actual internal table
object with a name and an INITIAL SIZE clause. The internal table inherits the fields and
attributes from the TYPES statement defined earlier.
In addition to the internal table definition with the INITIAL SIZE clause, the programmer
must also define an internal table work area (staging area). This work area is defined
using the same format as the table. It uses the same user-defined data type as the
internal table.
The work area is loaded programmatically (i.e., MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB_WA), then appended to the internal table. In Newer Versions, this Statement gives a warning in case of SLIN Check.
To load an internal table without a header line, the following APPEND statement is used:
APPEND <work area> to <internal table>.
IBM Global Business Services
Performance Issues
Data Structure & Internal Tables |
Why would we choose to use an internal table without a header line when it is easier to code one
with a header line?
Separate Internal Table Work Area: The work area (staging area) defined for the internal table
is not limited to use with just one internal table.
Suppose you want two internal tables for EMPLOYEE – one to contain all records and
one to contain only those records where country = ‘USA’. You could create both of
these internal tables without header lines and use only one work area to load data into
both of them. You would append all records from the work area into the first internal
table. You would conditionally append the ‘USA’ records from the same work area into
the second internal table.
Performance Issues: Using an internal table without a header line is more efficient than one
with a header line (see how to test this below).
Nested Internal Tables: If you want to include an internal table within a structure or another
internal table, you must use one without a header line.
Performance Tips:
To test efficiency issues, go to SYSTEM UTILITIES RUNTIME ANALYSIS Execute
and click on the ‘Tips & Tricks’ push-button. Here you can test various internal table
performance issues. The one dealing with header line versus no header line is entitled ‘Using
explicit work areas’. Within this area, you can measure the runtime to see that internal tables
without header lines are more efficient. In the context of classes in SAP ECC6.0 internal tables
without header line should be used .
TIPS
COUNTRY ID FORMA NAME1 SORTL . . .
Work Area
ID TYPE ID,
NAME1 TYPE NAME1,
COUNTRY TYPE COUNTRY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 ,
B EMPTAB_WA TYPE EMP.
APPEND EMPTAB_WA TO EMPTAB.
ID NAME1 COUNTRY
ID NAME1 COUNTRY
COUNTRY ID FORMA NAME1 SORT . . .
Work Area
This work area is not attached to the body of the internal table.
EMPLOYEE
1
2
3
1
2
3
10
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 ,
EMPTAB_WA TYPE EMP.
3 APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT.
REPORT Y170DM41.
WA_EMP TYPE EMPLOYEE.
MOVE WA_EMP TO EMPTAB.
APPEND WA_EMP TO EMPTAB.
ENDSELECT.
The internal table EMPTAB will have the exact same structure as the dictionary table EMPLOYEE.
Notice the MOVE statement instead of a MOVE-CORRESPONDING.
Data Structure & Internal Tables |
The example above demonstrates how to create an internal table that gets
its structure from an existing table/structure in the ABAP Dictionary (in this example, EMPLOYEE). This is a very valuable capability. Referencing other previously defined structures can save time (not having to code them) and problems.
The only difference between making an internal table from a TYPES statement or from an existing dictionary structure, is the use of the word TYPE in the DATA declaration.
It is also possible to refer to other objects defined in the program.
Notice the use of MOVE instead of MOVE-CORRESPONDING. The MOVE statement is used because the structure of EMPTAB is identical to the structure of EMPLOYEE.
The above code loads the EMPLOYEE data into the internal table EMPTAB. It does not process the internal table (this will be covered in later slides).
IBM Global Business Services
Individual field type conversion
Data Structure & Internal Tables |
When a MOVE-CORRESPONDING is performed, type conversion occurs automatically for the individual fields. The process is carried out on a field-by-field basis. MOVE specified field to specified field behaves the same way.
However, the following three scenarios are handled similarly to each other:
when you MOVE a structure to a structure of a different definition
MOVE a field to a structure
a structure to a field
In these cases, the data would be converted to one long character string (type C) first, then conversion would take place.
If a piece of data is moving to a longer space in the new structure, it will be padded with spaces or zeroes according to its data type. Moving into a shorter length would cause truncation.
Structures with an internal table included as a component do not follow the typical rules.
See Online Help for extended information about all the data conversion rules.
IBM Global Business Services
REPORT Y170DM69.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB
WHERE COUNTRY = ‘USA’.
SELECT * FROM <table> . . .
1. INTO TABLE <EMPTAB>.
2. APPENDING TABLE <EMPTAB>.
Notice no ENDSELECT is needed here because no loop processing occurs.
Data Structure & Internal Tables |
To read records from a database table directly into an internal table, you need a basic SELECT * FROM <database> statement with one of the following variations:
INTO TABLE <internal table>
APPENDING TABLE <internal table>
The INTO TABLE <internal table> addition fills the internal table with the selected database records. Any old entries in the internal table are overwritten.
The APPENDING TABLE <internal table> appends the selected database records to any existing entries in the internal table.
The database records are placed in the internal table EMPTAB in a single operation rather than one-by-one as in a basic SELECT processing loop.
Since no loop processing occurs, no ENDSELECT is needed.
The WHERE and ORDER BY additions are optional.
The internal table must be at least as wide as the database table. Records are placed in the internal table left-justified (i.e., starting from the first field listed in the definition of the internal table), so any additional field(s) you want in the internal table must be the last field(s) defined beyond the width of the database table.
Coding Recommendations:
The addition INTO CORRESPONDING FIELDS OF TABLE <itab> can be used.
TIPS
WA_EMP TYPE EMP
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
LOOP AT EMPTAB INTO WA_EMP WHERE COUNTRY BETWEEN ‘A’ AND ‘D’.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1,
ENDIF.
This LOOP AT <EMPTAB> statement allows for a logical expression in a WHERE clause to limit the processing of the internal table.
If no internal table entries qualify under the logical expression, the statement within the loop is not executed and SY-SUBRC is set to 4.
Data Structure & Internal Tables |
An internal table is processed using the LOOP AT … ENDLOOP statement. On each loop pass, the system ‘reads’ the next table entry and places it in the header line. Also, the system field SY-TABIX is set to the line number of the entry read.
When internal table fields are referenced within a program (i.e., EMPTAB-COUNTRY), the data comes from the header line, not the body of the internal table. You cannot directly access values from inside the body of the internal table – you must first ‘read’ an entry into the header line.
After the WHERE parameter, you can specify a logical expression. The entire internal table is read. If at least one entry satisfies the logical expression, SY-SUBRC is set to zero. If no table entry satisfies the logical expression, the statements within the loop are not executed and the system field SY-SUBRC is set to 4. For each entry that satisfies the logical expression, the statements within the loop are executed.
The programmer can use the FROM and TO parameters of the LOOP AT statement to restrict processing of an internal table to a specific block of lines.
When processing a table without a header line, the syntax of the LOOP statement is
LOOP AT <internal table> INTO <work area>.
IBM Global Business Services
PARAMETERS: START TYPE SY-TABIX DEFAULT 10,
END TYPE SY-TABIX DEFAULT 20.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
LOOP AT EMPTAB INTO WA_EMP FROM START TO END.
WRITE: / SY-TABIX, WA_EMP-COUNTRY, WA_EMP-NAME1.
Data Structure & Internal Tables |
When an internal table is read, the system field SY-TABIX is set to the index value of the internal table line which has been placed either in the header line or the work area (depending on the type of table you are reading).
Notice the use of the FROM and TO parameters in the LOOP AT statement to restrict the processing of the internal table to a specific set of records.
It is possible to have nested loops on internal tables. Within these loops SY-TABIX is set in a similar manner to SY-INDEX (see previous chapter)
Performance Tips:
Looping at internal tables for processing is more efficient than processing within
SELECT … ENDSELECT loops. The following is even more efficient than nested loops: SELECT FROM <database table>
FOR ALL ENTRIES IN <internal table>
WHERE <field> = <internal table field>.
See online help for further examples.
TIPS
REPORT Y170DM43.
WA_EMP TYPE EMP.
MOVE WA_EMP TO EMPTAB.
COLLECT WA_EMP INTO EMPTAB.
WRITE: / WA_EMP-COUNTRY, WA_EMP-SALES.
Data Structure & Internal Tables |
When the COLLECT statement is used, ABAP scans the internal table for entries which correspond to the header line in fields which are not of type P, I or F (i.e., all of the non-numeric fields become the ‘key’ of the header line).
If such an entry is found, the system adds all P, I and F fields in the header line to the corresponding fields in the matching entry.
If no corresponding table entry is found, the contents of the header line are added to the end of the table – the same effect as the APPEND statement.
The COLLECT statement is also used to load a table with unique key fields. Before adding a line to the table, the system checks to see if the table has an entry with the same key value. If an entry already exists, the record is not added, otherwise the COLLECT statement has the same effect as an APPEND statement. If COLLECT is used to fill an internal table, duplicate entries cannot occur.
When using an internal table without a header line, the syntax of the COLLECT statement changes to: COLLECT <work area> INTO <internal table>.
Performance Tips:
COLLECT can be very CPU intensive - when using an internal table with > 50 entries.
TIPS
WA_EMP TYPE EMP.
SORT EMPTAB BY SALES DESCENDING.
LOOP AT EMPTAB INTO WA_EMP.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1, WA_EMP-SALES.
Sorting options:
1) SORT <EMPTAB> - sorts the entries of the internal table <EMPTAB> in ascending order.
2) SORT <EMPTAB> BY <field> - sorts the table on one or more fields within the table.
screen output
Data Structure & Internal Tables |
An internal table is sorted using the SORT <internal table> statement. If sort criteria is not specified, the table is sorted by all fields (except those of data types P, I, and F) in ascending order in the sequence in which they were declared.
With the additional specifications BY <field name> and ASCENDING or DESCENDING, you can restrict the sort process to specific fields (or to fields of type P, I, F) and can determine the sorting sequence and hierarchy.
Where possible, the sort process should be limited by using the BY <field name> parameter. ABAP then requires less storage space in the roll area for the sorting process.
The sort process is the same whether or not the internal table has a header line.
You have the option of using a sorted table instead of a standard table. Then the entries will be sorted automatically on their way from the header line into the internal table.
Performance Tips:
It is advisable to always specify the fields to be sorted by rather than just SORT.
TIPS
COUNTRY TYPE COUNTRY,
END OF EMP.
OF EMP INITIAL SIZE 10 WITH
HEADER LINE.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
The TYPES statement defines the structure and data type for the internal table.
The DATA statement with an INITIAL SIZE creates the actual internal table capable of storing data. Because of the WITH HEADER LINE addition, this internal table is created with a header line.
Header Line
Data Structure & Internal Tables |
An internal table type is defined using the TYPES statement. No memory is allocated when defining a type.
An internal table object is created with the DATA statement by referring to an internal table type using the TYPE parameter. It is this DATA statement occupies memory. Internal table objects can also be created through a reference to an existing table or structure using the LIKE parameter.
The internal table object must have an INITIAL SIZE <n> parameter to indicate the initial number of table lines for which memory will be allocated.
The above code creates a standard internal table EMPTAB of structure EMP, and then loads the employee data from table EMPLOYEE into the internal table EMPTAB.
The APPEND <internal table> statement adds the contents of the header line to the end of the internal
table.
The APPEND <internal table> SORTED BY <field> statement adds a new record to an internal table and
sorts the table by a single <field> in descending order (the default and only option).
The SORTED BY clause limits the maximum number of table entries to the value in the INITIAL SIZE
parameter. If the table is full then, the last table entry is removed.
Performance Tips:
An INITIAL SIZE 0 cannot be used with the SORTED BY option, as no entries will be saved into the body of the internal table.
In previous versions, there was only one type of internal table, so the STANDARD TABLE OF addition was not necessary. See the appendix for extended examples of older internal table declaration styles. Also the INITIAL SIZE extension was called OCCURS. The old declarations are still recognized in 4.x.
In the Newer Versions of SAP, the Internal Table Declaration with Header Line is OBSOLETE. Hence we need to use Work Areas Which is Explained in the Earlier Slides.
IBM Global Business Services
Data Structure & Internal Tables |
By default, all the records that you append to your internal table have a key. This key is the combination of all non-numeric fields. This is the implicit key.
You can also define your own key for an internal table. You would add WITH KEY FIELD1 FIELD2 … etc. to your DATA statement.
More specifically, you can make your user-defined key:
UNIQUE: additional records with the same key would not be permitted
NON-UNIQUE: additional records with the same key would be permitted
Standard tables can have:
no user-defined key at all (more likely)
Indexing is used more than keys when it comes to reading standard internal tables.
User-defined keys are new in 4.0. You would only be able to use the implicit key in previous
releases.
Data Structure & Internal Tables |
INITIAL SIZE <n> parameter: With <n>, you specify an initial number of lines for the internal table. For the number of lines specified, memory will be reserved as soon as the first line is written into an internal table.
If more lines are added to an internal table than specified by <n>, the reserved memory will expand automatically. Therefore, it is possible to create an internal table with the parameter INITIAL SIZE 0.
If there is not enough storage in memory for the internal table, it will be written into a buffer or on a disk (paging area).
If the INITIAL SIZE parameter is left off, a value of INITIAL SIZE 0 is set.
The INITIAL SIZE parameter will affect the physical size of an internal table if the APPEND <Internal table> SORTED BY <field> statement is used to load data into the internal table (see next slide).
Performance Tips:
If you know the maximum size of an internal table and that size is less than 8K, you can increase the system’s performance by specifying the maximum size in the INITIAL SIZE <n> parameter. If you do not know the maximum size of an internal table or if it’s greater
than 8K, you should let the system allocate the appropriate memory by specifying INITIAL SIZE 0 in the internal table definition. Any table larger than 8K should be declared as INITIAL SIZE 0.
TIPS
12.unknown
Data Structure & Internal Tables |
When processing an internal table with the LOOP statement, it is possible to add additional code that will only be processed when a field changes.
This is referred to as Control Level Processing (and can be compared to the GET LATE syntax seen later for Logical Database Programs)
Depending on the coding requirements, it is usually required that the internal table is sorted before control level processing occurs.
For example, some of the customer details have been stored in an internal table, and these must be printed out grouped by country, with the country only written once and with the total number for each country at the end.
The syntaxes required to perform this type of operation are:
AT FIRST …………ENDAT.
Condition is true as soon as a single record is read.
AT NEW <field>………….ENDAT.
AT LAST………..ENDAT.
Condition is true as soon as very last record is read.
WARNING: The timing of your AT statements are crucial. For example, within a LOOP block, if you code an AT END to print a total before the generic write statement to print the original individual amounts, your last record will be left hanging after the total.
IBM Global Business Services
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
READ TABLE ….
Data Structure & Internal Tables |
The READ TABLE <internal table> statement reads a single table entry.
When using an internal table without a header line, the syntax of the READ TABLE statement changes to: READ TABLE <internal table> INTO <work area>.
If an entry was found with the READ statement:
SY-SUBRC is set to zero,
SY-TABIX is set to the line number of the entry read,
the table entry read is placed into the internal table header line or work area.
If an entry was not found with the READ statement:
SY-SUBRC is set to a non-zero number,
SY-TABIX is undefined,
the internal table header line or work area remains unchanged.
When you perform a LOOP AT <internal table> … ENDLOOP, the effect is the same as a READ TABLE <internal table> INDEX <i> where <i> starts at one and continues incrementing until the entire internal table is read. In other words, with each loop pass, SY-TABIX is set to the line number of the entry read and the header line or work area contains the data from the entry read.
IBM Global Business Services
READ TABLE <EMPTAB> options:
1) READ TABLE <EMPTAB>.
<kn> = <vn>.
<kn> = <vn>.
5) READ TABLE <EMPTAB> WITH KEY . . . BINARY SEARCH.
6) READ TABLE <EMPTAB> INDEX <i>.
7) READ TABLE <EMPTAB> COMPARING <f1> <f2> . . . .
8) READ TABLE <EMPTAB> COMPARING ALL FIELDS.
9) READ TABLE <EMPTAB> TRANSPORTING <f1> <f2> . . . .
10) READ TABLE <EMPTAB> TRANSPORTING NO FIELDS.
Data Structure & Internal Tables |
READ TABLE <internal table>.
The contents of the header line determine the table entry to be read. ABAP searches for the first entry in the internal table that matches the header line’s search term which consists of all non-numeric fields with contents not equal to space.
READ TABLE <internal table> WITH KEY ‘<key>‘.
Enter the search argument after the parameter KEY (in single quotes).
Beginning with the first character of the first field of the first entry in the table, ABAP compares each record character by character with the search argument (‘<key>‘).
READ TABLE <internal table> WITH KEY ‘<key>‘ BINARY SEARCH.
Like variant 2, but using a binary search (faster than linear search).
The internal table must be sorted in ascending order by the
search argument.
READ TABLE <internal table> INDEX <i>.
The i-th table entry is read.
For information on the other READ options available, see Online Help.
Performance Tips:
A BINARY SEARCH should always be used whenever possible, but the table MUST be sorted first.
TIPS
MOVE ‘ABC’ TO EMPTAB-NAME1.
MODIFY EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
ELSE.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.
INSERT <EMPTAB> INDEX <i>.
MODIFY <EMPTAB> INDEX <i>.
DELETE <EMPTAB> INDEX <i>.
Check SY-SUBRC after every attempt to change an internal table entry.
Data Structure & Internal Tables |
The INSERT <internal table> INDEX <i> statement generates a new table entry before line <i> with the contents of the header line. If the table <EMPTAB> has no entries, the contents of the header line are added to the table.
The MODIFY <internal table> INDEX <i> statement overwrites table line <i> with the contents of the header line. Line <i> must already exist.
The DELETE <internal table> INDEX <i> statement deletes table line <i>.
Within a LOOP AT <internal table> … ENDLOOP, you can make changes to an internal table. The line affected is always the current line (SY-TABIX).
INSERT <internal table>:
A new line with the contents of the header line is inserted before the current line.
MODIFY <internal table>:
The current line is overwritten by the contents of the header line.
DELETE <internal table>:
The current line is deleted.
Check SY-SUBRC after every attempt to change a table entry. If the change was successful, SY-SUBRC will be set to zero.
Performance Tips:
INSERT/MODIFY/DELETE <internal table> WHERE .... is more efficient than first LOOPing AT the table using the WHERE clause, then maintaining the tables header line.
TIPS
APPEND <work area> TO <internal table>.
COLLECT <work area> INTO <internal table>.
INSERT <work area> INTO <internal table>.
MODIFY <internal table> FROM <work area>.
READ TABLE <internal table> INTO <work area>.
LOOP AT <internal table> INTO <work area>.
Data Structure & Internal Tables |
A work area (staging area) is required when working with an internal table without a header line. This work area is defined as a Structure with the same structure as the internal table. The work area is loaded and the table is processed from the work area.
A summary of the statements used for internal tables without header lines is
the following:
APPEND <work area> TO <internal table>
Appends the contents of the work area to the end of the internal table
COLLECT <work area> INTO <internal table>
Accumulates the values on a field into the table
INSERT <work area> INTO <internal table>
Inserts a new line with the contents of the work area before the
current line
MODIFY <internal table> FROM <work area>
Overwrites a line in the table with the contents of the work area
READ TABLE <internal table> INTO <work area>
Reads a line from the table into the work area
LOOP AT <internal table> INTO <work area>
Processes an internal table. On each loop pass, a table entry is placed in the work area.
IBM Global Business Services
REFRESH <internal table>
Paging is released.
Deletes all table lines.
Storage space is released.
Header line remains unchanged
Data Structure & Internal Tables |
The CLEAR <internal table> statement initialises all single fields in the header line of an internal table according to type.
The REFRESH <internal table> statement deletes all table lines. The table storage space is not released. The header line remains unchanged.
The FREE <internal table> statement releases the storage space required for a table. The header line remains unchanged.
This statement is particularly useful for very large internal tables. You can improve a program’s performance by ‘freeing’ the memory space allocated for the internal table.
Internal tables that are local to a subroutine are automatically ‘freed’ upon leaving the subroutine.
WARNING: If you are working with an internal table with a separate work area, and you accidentally say CLEAR <internal table>rather than CLEAR <internal table work area >, you will delete all the table lines. Use REFRESH to achieve this instead.
IBM Global Business Services
REPORT Y170DM49.
LINE_COUNT TYPE I,
INITIAL_COUNT TYPE I.
DESCRIBE TABLE EMPTAB
Data Structure & Internal Tables |
The DESCRIBE TABLE <internal table> statement provides information about an internal table’s attributes. With this statement, the programmer must use at least one of the two parameters available – LINES and OCCURS.
The LINES parameter allows the programmer to find out the number of existing table entries.
The OCCURS parameter contains the value of the OCCURS clause specified in the internal table definition.
Notice in the example above the internal table can have more records in it than the number specified in the OCCURS clause.
Performance Tips:
Always use DESCRIBE to find out how many entries there are in an internal table - it is much more efficient than LOOPing AT the internal table and incrementing a counter.
TIPS
WA_EMP TYPE EMP.
EDITOR-CALL FOR EMPTAB.
LOOP AT EMPTAB INTO WA_EMP WHERE NAME1 EQ ‘Maurice Cheeks’.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1.
screen output
With the EDITOR-CALL FOR <internal table> statement, the program will
open the SAP Table Editor which allows the user to change any data in the
internal table.
Select the records (use select icon) and then edit them.
To close the Editor and return to remaining statements in the program, use one of these methods:
Type U (update) in the command line of the Editor,
Press the SAVE push-button,
Press the BACK, CANCEL, or EXIT pushbuttons, or
Press F3, F12, or F15.
The system field SY-SUBRC will contain one of the following return code values:
0 = Editor left with U, SAVE, or F11
4 = Editor left with BACK, CANCEL, EXIT, F3, F12, or F15.
The maximum line length for the Editor is 72 characters.
The internal table can only contain fields of type C.
IBM Global Business Services
Demonstration
Declaring an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.
Data Structure & Internal Tables |
IBM Global Business Services
Practice
Declaring an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.
Data Structure & Internal Tables |
IBM Global Business Services
Structures in code are temporary objects in program memory.
A structure can be defined using a combination of the TYPES and DATA statements.
The statement MOVE-CORRESPONDING transports values field by field between the ABAP data structures.
Internal table, that can store records of data temporarily during the processing of a program.
3 different types of internal tables: Standard, Sorted, and Hashed.
An internal table object is created with the DATA statement by referring to an internal table type using the TYPE parameter
APPEND statement adds the contents of the work area to the internal table.
The system field SY-TABIX is set to the line number of the entry read.
Data Structure & Internal Tables |
IBM Global Business Services
The CLEAR statement resets all fields to their initial value.
The REFRESH statement deletes all table lines.
The FREE statement releases the storage space required for a table.
Data Structure & Internal Tables |
IBM Global Business Services
What are the different types of internal tables are there?
Explain the following statements :