You are on page 1of 10

Internal Tables

Internal tables are data objects that allow you to retain several data records with the same structure in the working memory. In principle, the number of data records is unlimited. It is only restricted by the capacity limits of specific system installations. The ABAP runtime system dynamically manages the length of internal tables. This removes the necessity for you to carry out any work concerning working memory management. Internal tables are therefore a simple way of processing large data sets in a structured manner. Typical uses include: Temporarily storing data from database tables for future processing Structuring and formatting data for output Formatting data for the use of other services

The data type of an internal table is completely specified by the following attributes:

Line type
This is the source of the attributes of the individual columns. You normally specify a structure type but any data types are possible.

Key definition
The key columns and their sequence define the criteria by which the table rows are identified. Depending on the access type, the key can be defined as unique or non-unique. In the case of unique keys, there are no multiple entries in internal tables with identical keys.

Access type
With key access - as with database tables - you access using the field contents. Example: A read access with the search term carrid = 'UA' and connid = '0007' to an internal table using the unique key CARRID and CONNID and the data shown above returns exactly one data record. Index access: Unlike database tables, the system may number the rows for an internal table. It is therefore possible to access table rows using this index. Example: A read access to a data record with index 5 returns the fifth data record of the internal table.

Another internal table attribute is the table kind. Internal tables can be divided into three table kinds according to the access types that are possible in each case:

With STANDARD TABLES the row numbering is maintained internally. Both index and key accesses are possible. With SORTED TABLES the data records are always sorted according to key and saved. Here too, the index is maintained internally. Both index and key accesses are possible. With HASHED TABLES the data records are managed optimized to runtime. A unique key is required. With hashed tables only key accesses are possible. Which table type you should use in each case depends on how that table's entries are generally going to be accessed: For index accesses you should normally use standard tables. Sorted tables are best for unique keys and fixed sorting. With hashed tables the runtime optimization is only noticeable if the accesses are mainly of the read type with a unique key.

Syntax

DATA itab_name TYPE table_kind TABLE OF struc_type WITH key_def.


Steps to Declare Internal Table

1. Declare the structure of internal table I.e. User defined data type with the required fields, according to the fields required from the corresponding Database table(s). Syntax:TYPES: BEGIN OF <TY>, F1 TYPE <REF. DATA TYPE>, F2 TYPE <REF. DATA TYPE>, .. ..

END OF <TY>. 2. Declare internal table I.e. Array of above structure. DATA <ITAB> TYPE TABLE OF <TY>.
Syntax to select data from Database table into internal table SELECT <F1> <F2> <F3> .. INTO TABLE <ITAB> FROM <DBTABLE> WHERE <CONDITION IF ANY>. NOTE: - Make sure that the structure (Order) of fields in the SELECT and Internal table should be same, because content of 1st field in the SELECT is transferred to the 1st field of <ITAB> and similarly 2nd, 3rdetc.

OPERATIONS ON INTERNAL TABLES

SINGLE RECORD ACCESSES The following single record operations are available for internal tables: In each case, wa represents a structure that must have the same type as the line type of the internal table itab. APPEND Appends the contents of a structure to an internal table. This operation can only be used with standard tables. INSERT Inserts the contents of a structure into an internal table. In a standard table these contents are appended, in a sorted table they are inserted in the right place, and in a hashed table they are inserted according to the hash algorithm. READ Copies the contents of an internal table row into a structure. MODIFY Overwrites an internal table row with the contents of a structure. DELETE Deletes a row of an internal table.

COLLECT Accumulates the contents of a structure in an internal table. This statement can only be used for tables whose non-key fields are all numeric. If the keys are identical, the numeric values are added.

PROCESSING SETS OF RECORDS The following set operations are available for internal tables: In each case, wa represents a structure that must have the same type as the line type of the internal table itab. LOOP ... ENDLOOP The LOOP places the rows of an internal table one by one into the structure specified in the INTO clause. All single record operations can be executed within the LOOP. In this case, for the single recor operations, the system identifies the row to be processed. DELETE Deletes the rows of the internal table that satisfy the condition <condition>. INSERT Copies the contents of several rows of an internal table to another internal table. APPEND Appends the contents of several rows of an internal table to another standard table.

SYNTAX

COPYING (ADDING MULTIPLE RECORDS): 1. COPY 2.


AT THE END OF INTERNAL TABLE

APPEND LINES OF INSERT LINES OF FROM

<ITAB1> <ITAB1> <N2>

FROM FROM

<N1> <N1>

TO TO

<N2> <N2>

TO TO

<ITAB2> <ITAB2>
INDEX

COPY FROM THE GIVEN LOCATION

<N>

NOTE : INSERT WITH NO INDEX ACTS AS APPEND ONLY .

<N1>

TO

IS OPTIONAL , IF WE IGNORE IT

,ALL

THE RECORDS ARE TRANSFERRED.

ADDING SINGLE RECORD

1. 2.

ADD AT THE END OF INTERNAL TABLE APPEND INSERT

<WA> <WA>

TO

<ITAB> <ITAB>
INDEX

ADD AT THE GIVEN LOCATION INTO

<N>. (N>0)

FINDING NUMBER OF RECORDS

1.

DESCRIBE TABLE SORTING

<ITAB>

LINES

<V_LINES> . (V_LINES

TYPE

I)

TO ARRANGE RECORDS INTO ASCENDING SORT

<ITAB>

ASCENDING /DESCENDING

/DESCENDING GROUPS BY < F1> < F2>.

NOTE: a. SORTING IS ASCENDING BY b. THE DEFAULT KEY IS MADE

DEFAULT . UP OF THE NON - NUMERIC FIELDS OF THE TABLE LINE IN THE

ORDER IN WHICH THEY OCCUR . ACCESSING/ READING SINGLE RECORD READ TABLE OR READ TABLE

<ITAB>

INTO

<WA>

INDEX

<N>.

<ITAB>

INTO

<WA>

WITH KEY

<CONDITION>

BINARY SEARCH.

NOTE:

MAKE SURE THAT THE INTERNAL TABLE IS SORTED TO APPLY BINARY SEARCH.

ACCESSING MULTIPLE RECORDS LOOP AT

<ITAB>

INTO

<WA>

FROM

<N1>

TO

<N2>.

**** PROCESSING
ENDLOOP. OR LOOP AT

<ITAB>

INTO

<WA>

WHERE

<CONDITION>.

**PROCESSING

ENDLOOP.

DELETING RECORDS SINGLE RECORD

DELETE

MULTIPLE RECORDS

DELETE

<ITAB> INDEX <N>. <ITAB> WHERE <CONDITION>. DELETE <ITAB> FROM <N 1> TO <N 2>.

DELETE ADJACENT DUPLICATES NOTE

MAKE SURE THAT THE DUPLICATES SHOULD BE ADJACENT .

W HICH

CAN BE DONE THROUGH

SORTING. SO THAT SORTING THE INTERNAL TABLE IS MANDATORY DELETE ADJACENT DUPLICATES FROM MODIFYING MODIFY

<ITAB>

COMPARING

<F1> <F2>.

<ITAB>

FROM

<WA>

TRANSPORTING

<F1> <F2> .

WHERE

<CONDITION>

Types of Internal Tables

ANY TABLE INDEX TABLE HASHED TABLE

STANDARD TABLE

SORTED TABLE

STANDARD TABLES: I. II. III. IV. V. VI. Are the default internal tables. The key of a standard table is always non-unique , so duplicates are allowed. DATA <ITAB> TYPE STANDARD TABLE OF <TY>. RECORDS CAN BE ACCESSED THROUGH BOTH INDEX AND CONDITION <ITAB> CAN BE SORTED ACCESSING/SEARCHING TIME FOR RECORDS DEPENDS ON THE NO OF RECORDS BECAUSE SEARCHING IS EITHER LINER OR BINARY. SORTED TABLES I. II. III. RECORDS ARE ALWAYS IN SORTED ORDER. SORTED INTERNAL TABLES CANNOT BE SORTED AGAIN. THE RESPONSE TIME FOR KEY ACCESS IS LOGARITHIMCALLY PROPOTIONAL TO THE NUMBER OF TABLE ENTRIES , SINCE THE SYSTEM ALWAYS USES A BINARY SEARCH RECORDS CAN BE ACCESSED THROUGH BOTH INDEX AND KEY. DATA <ITAB> TYPE SORTED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY <F1> <F2> .. NOTE : SORTED INTERNAL TABLES ARE ALWAYS,EITHER UNIQUE/NONUNIQUE .

IV. V.

HASHED TABLES I. II. III. HASHED TABLES ALWAYS HAVE A UNIQUE KEY YOU CANNOT ACCESS A HASHED TABLE USING ITS INDEX DATA <ITAB> TYPE HASHED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY <F1> <F2>..

INITIALIZING INTERNAL TABLES CLEAR <VARIABLE>. NOTE : THE VARIABLE CAN BE A NORMAL VARIABLE , WORK AREA ,INTERNAL TABLE ETC.

CLEAR <WA> - CLEARS THE WORK AREA CLEAR <ITAB>-CLEARS INTERNAL TABLE

** IF <ITAB> WITH HEADER LINE CLEAR <ITAB>[] CLEARS THE INTERNAL TABLE CLEAR <ITAB> - CLEARS THE WORK AREA

REFRESH ALWAYS WORKS FOR INTERNAL TABLE ONLY.CLEARS THE ITAB FREE CLEARS THE ITAB AND RELEASE ITS MEMORY SPACE

ADDING SUMMARIZED LINES

COLLECT <WA> INTO <ITAB>.

CONTROL BREAK STATEMENTS


NOTE : FIRST SORT THE TABLE BEFORE USING CONTROL BREAK STATEMENTS. AT <LEVEL>.
<STATEMENT BLOCK>.

ENDAT.

DIFFERENT STATEMENTS AT FIRST FIRST LINE OF INTERNAL TABLE AT LAST LAST LINE OF THE INTERNAL TABLE

AT NEW <F> - BEGINNING OF A GROUP OF LINES WITH THE SAME CONTENT IN THE FIELD <F> AND IN THE FIELD LEFT OF <F>. AT END OF <F> - END OF A GROUP OF LINES WITH THE SAME CONTENTS IN THE FIELD <F> AND IN THE FIELDS LEFT OF <F>.

You might also like