Ads

What is Internal Table in SAP ABAP

Internal Table in ABAP program


In this post, we have to learn SAP ABAP internal tables and types of internal table and internal table program tutorials and what is work area and technical features of table’s Operations on internal tables that are creating, changing, appending, modifying and deleting internal tables in SAP program.

Internal Tables


Internal tables are used to take data from fixed structure and inserting it in working memory in ABAP.Line by line stored in the memory and each line has the same structure. We are using internal table whenever we want to make changes to data base tables. Internal tables acts as function of arrays, dynamic data objects and implements the dynamic memory management in programs.

What is Internal Table in SAP ABAP

Internal table data types


There are 3 types of data types Line type,Key and Table type.

Line Type


The line type of an internal table can be any data type and it is  a structure. Each component of the structure is a column in the internal table.

Key

The key identifies table rows. There are two kinds of key for internal tables - the standard key and a user-defined key. You can specify whether the key should be UNIQUE or NON-UNIQUE. Internal tables with a unique key cannot contain duplicate entries.
The user-defined key can contain any columns of the internal table that are no internal table themselves, and do not contain internal tables.

Table type


The table type determines how ABAP will access individual table entries. Internal tables can be divided into three types:

Standard Tables 
Sorted Tables 
Hashed Tables 

Standard tables

Standard tables have an internal linear index. The system can access records either by using the table index or the key.The response time for key access is proportional to the number of entries in the table. The key of a standard table is always non-unique. You cannot specify a unique key. This means that standard tables can always be filled very quickly

  Standard tables uses: This is the most appropriate type if you are going to address the individual table entries using the index. Index access is the quickest possible access. You should fill a standard table by appending lines (ABAP APPENDstatement), and read, modify and delete entries by specifying the index (INDEX option with the relevant ABAP command). The access time for a standard table increases in a linear relationship with the number of table entries.


Sorted tables

Sorted tables are always saved sorted by the key. They also have an internal index. The system can access records either by using the table index or the key. The response time for key access is logarithmically proportional to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique or non-unique. When you define the table, you must specify whether the key is to be UNIQUE or NON-UNIQUE. Standard tables and sorted tables are known generically as index tables.

Uses of Sorted tables This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.

Hashed tables

Hashed tables have no linear index. You can only access a hashed table using its key. The response time is independent of the number of table entries, and is constant, since the system access the table entries using a hash algorithm. The key of a hashed table must be unique. When you define the table, you must specify the key as UNIQUE.


Uses of Hashed tables Whenever we use key access operation. We cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries.Hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.

Generic Internal Tables We can use generic internal tables to define the types of field symbols and the interface parameters of procedures. We cannot use them to declare data objects.

Internal Tables as Dynamic Data Objects Internal tables are always completely specified regarding row type, key and access type. However, the number of lines is not fixed. Thus internal tables are dynamic data. The only restriction on the number of lines an internal table may contain are the limits of your system installation.

Table types

We define internal tables globally as data types in the ABAP Dictionary. Like all local data types in programs, you define internal tables using the TYPES statement. If you do not refer to an existing table type using the TYPE or LIKE addition, you can use the TYPES statement to construct a new local internal table in your program.


TYPES type TYPE|LIKE tabkind OF linetype [WITH key]  [INITIAL SIZE n].

 tabkind OF linetype [WITH key]


TYPES: BEGIN OF line, 
 FEILD1 TYPE i, 
FIELD2 TYPE i, 
FIELD3 TYPE i, 
END OF LINE.
 Here,Line is WORK AREA or Structure .

TYPES itab TYPE SORTED TABLE OF line WITH UNIQUE KEY column1.
TYPES vector TYPE HASHED TABLE OF i WITH UNIQUE KEY table_line. 
TYPES itab TYPE SORTED TABLE OF line WITH UNIQUE KEY column1.

Internal Tables 

Internal tables are dynamic variable  data objects. Like all variables, you declare them using the DATA statement. You can also declare static internal tables in procedures using the STATICSstatement, and static internal tables in classes using the CLASS-DATAstatement.


Referring to Known Table Types Like all other data objects, you can declare internal tables using the LIKE or TYPE addition of the DATA statement.


DATA itab TYPE type|LIKE obj [WITH HEADER LINE].

Here, the LIKE addition refers to an existing table object in the same program. The TYPE addition can refer to an internal type in the program declared using the TYPES statement, or a table type in the ABAP Dictionary.

Declaring New Internal Tables

 You can use the DATA statement to construct new internal tables as well as using the LIKE or TYPE addition to refer to existing types or objects.

DATA itab TYPE|LIKE tabkind OF linetype WITH key  [INITIAL SIZE n]  [WITH HEADER LINE].

tabkind OF linetype WITH key defines the table type tabkind,
the line type linetype, and the key key of the internal table itab.


DATA itab TYPE HASHED TABLE OF spfli  WITH UNIQUE KEY carrid connid. 

Operations on Entire Internal Tables 

When you access the entire internal table, you address the body of the table as a single data object. The following operations on the body of an internal table are relevant:

Assigning Operation
Initialize Operation
Compare Operation
Sort Operation

Internal Tables as Interface Parameters Determining the Attributes of Internal Tables

Assigning Internal Tables 

Like other data objects, you can use internal tables as operands in a MOVE statement

DATA: BEGIN OF line, 
col1(1) TYPE c,
col2(1) TYPE c, 
END OF line.

DATA: etab LIKE TABLE OF line WITH HEADER LINE,
ftab LIKE TABLE OF line.

MOVE itab1 TO itab2. 
itab2 = itab1.

 Both operands must either be compatible or convertible internal tables.

APPEND line TO etab.
 MOVE etab[] TO ftab.  
 INSERT sy-index INTO TABLE itab.
 iline-num = sy-index. 
APPEND iline-num TO itab.

Initializing Internal Tables

CLEAR itab[]. To ensure that the table itself has been initialized, you can use the statement REFRESH itab. This always applies to the body of the table. With REFRESH, FREE itab. You can use FREE to directly initialize an internal table and to release its entire memory space,
 Comparing Internal Tables

Like other data objects, you can use internal tables as operands in logical expressions. If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([ ]) after the table name. REPORT int_tables.
DATA: BEGIN OF line, 
col1 TYPE i, 
col2 TYPE i, 
END OF line.

DATA: itab LIKE TABLE OF line, 
jtab LIKE TABLE OF line.

DO 3 TIMES. 
line-col1 = sy-index. 
line-col2 = sy-index ** 2. 
APPEND line TO itab.
ENDDO.

MOVE itab TO jtab.
line-col1 = 10.
line-col2 = 20.

APPEND line TO itab.
IF itab GT jtab. 

WRITE / 'ITAB GT JTAB'. ENDIF. APPEND line TO jtab. IF itab EQ jtab. 
WRITE / 'ITAB EQ JTAB'. ENDIF. line-col1 = 30. line-col2 = 80.

APPEND line TO itab. IF jtab LE itab. 

WRITE / 'JTAB LE ITAB'. ENDIF. line-col1 = 50. line-col2 = 60.

APPEND line TO jtab. IF itab NE jtab. 

WRITE / 'ITAB NE JTAB'. ENDIF. IF itab LT jtab. 
WRITE / 'ITAB LT JTAB'. ENDIF. The list output is:

ITAB GT JTAB
ITAB EQ JTAB
JTAB LE ITAB
ITAB NE JTAB
ITAB LT JTAB   

Sorting Internal Tables 

You can sort a standard or hashed table in a program.
To sort a table by its key, use the statement


SORT itab [ASCENDING|DESCENDING] [AS text] [STABLE].

 The statement sorts the internal table itab in ascending order by its key. The statement always applies to the table itself, not to the header line. The sort order depends on the sequence of the standard key fields in the internal table. The default key is made up of the non-numeric fields of the table line in the order in which they occur. You can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending order.

  Sorting by Another Sort Key
 If you have an internal table with a structured line type that you want sort by a different key, you can specify the key in the SORT statement:

SORT itab [ASCENDING|DESCENDING] [AS text] [STABLE]   BY f1 [ASCENDING|DESCENDING] [AS text]  ...  fn [ASCENDING|DESCENDING] [AS text].

  Sorting alphabetically


 As well as the ASCENDING or DESCENDING addition, you can also specify that the entire sort or each sort field should be alphabetical. SORT itab... AS text.... This addition affects the sort method for strings. Without the addition, strings are sorted according to the sequence specified by the hardware platform. With the option AS text, the system sorts character fields alphabetically according to the current text environment. By default, the text environment is set in the user’s master record. However, you can also set it specifically using the statement SET LOCALE LANGUAGE . The AS text addition saves you having to convert strings into a sortable format. Such a conversion is only necessary if you want to

Stable sort The option SORT itab... STABLE.


Internal Tables as Interface Parameters

 Like other data objects, you can pass internal tables by value or reference to the parameter interface  of procedures. If an internal table has a header line, you must indicate that you want to address the body of the table by placing two square brackets ([]) after the table name.

Operations on Individual Lines 

. Access Methods for Individual Table Entries

Append Statement
Reading  Statement
Loops  Statement
Changing  Statement
Deleting  Statement
Searching Through Internal Tables Line by Line


  Assigning Values with MOVE 


MOVE source TO destination.
destination = source. f4 = f3 = f2 = f1.
 MOVE f1 TO f2.
MOVE f2 TO f3.
MOVE f3 TO f4.

Assigning Values Between Components of Structures

 MOVE-CORRESPONDING sourcestruct TO destinationstruct.
 MOVE sourcestruct-comp1 TO destinationstruct-comp1.
MOVE sourcestruct-comp2 TO destinationstruct-comp2.  

Inserting Lines into Tables 

Inserting a Single Line To add a line to an internal table, use the statement: INSERT line INTO TABLE itab.

Inserting Lines Using the Index The INSERT statement allows you not only to insert lines in any type of internal table, but also to change them using a line index. You can insert either a single line or a group of lines into index tables using the index.

INSERT line INTO itab [INDEX idx].


Inserting Several Lines To add several lines to an internal table, use the statement:

INSERT LINES OF itab1 INTO itab2 [INDEX idx].

 The system inserts the lines of the entire table itab1 one by one into itab2 using the same rules as for single lines. itab1 can be any type of table. The line type of itab1 must be convertible into the line type of itab2. When you append an index table to another index table, you can specify the lines to be appended as follows:



INSERT LINES OF itab1 [FROM n1] [TO n2] INTO itab2  [INDEX idx]. 

Appending Table Lines 

Appending a Single Line To add a line to an index table, use the statement:


APPEND line TO itab.


Appending Several Lines You can also append internal tables to index tables using the following statement:


APPEND LINES OF itab1 TO itab2.

This statement appends the whole of itab1 to itab2. itab1can be any type of table. The line type of itab1 must be convertible into the line type of itab2. When you append an index table to another index table, you can specify the lines to be appended as follows:


APPEND LINES OF itab1 [FROM n1] [TO n2] TO itab2.

Ranked Lists You can use the APPEND statement to create ranked lists in standard tables. To do this, create an empty table, and then use the statement:

APPEND wa TO itab SORTED BY f.

Appending Summarized Lines The following special statement allows you to summate entries in an internal table:

COLLECT wa INTO itab.

itab must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (f, i, p). You specify the line wa that you want to add as a work area that is compatible with the line type of itab.

Reading Lines of Tables 

To read a single line of any table, use the statement: READ TABLE itab key result.

Using the Table Key To use the table key of itab as a search key, enter key as follows:

READ TABLE itab FROM wa 

READ TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn result.

READ TABLE itab WITH TABLE KEY table_line = f result.

 The system searches for the relevant table types as follows:
 Standard tables Linear search, where the runtime is in linear relation to the number of table entries.  Sorted tables Binary search, where the runtime is in logarithmic relation to the number of table entries.

 Hashed tables The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.


Using a Different Search Key


READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.

  Using a Work Area You can write the table entry read from the table into a work area by specifying result as follows:



READ TABLE itab key INTO wa [COMPARING f1 f2 ... |ALL FIELDS]  [TRANSPORTING f1 f2 ... |ALL FIELDS    |NO FIELDS].

Using a Field Symbol You can assign the table entry read from the table to a field symbol by specifying result as follows:

READ TABLE itab key ASSIGNING <fs>.

Reading Lines Using the Index You can use the READ statement to read lines in tables using their index. To read a single line of an index table, use the statement:

READ TABLE itab INDEX idx result.

Processing Table Entries in Loops 

You can use the LOOP statement to process special loops for any internal table.


 LOOP AT itab result condition.  statement block ENDLOOP.

Using a Work Area To place the current loop line into a work area, specify result as follows:


 LOOP AT itab INTO wa bedingung.

Using a Field Symbol

 To assign the contents of the current loop line to a field symbol, specify result as follows:


 LOOP AT itab ASSIGNING <fs> condition.

Suppressing the Assignment of Lines

 If you do not need to transfer the contents of the current table line to a work area or assign them to a field symbol, you can use the following statement: LOOP AT itab TRANSPORTING NO FIELDS condition. Enter conditions To avoid reading all of the lines in an internal table, you can specify a condition for the line selection as follows:

 LOOP AT itab result WHERE cond.

Control level processing

Control level processing is allowed within a LOOP over an internal table. This means that you can divide sequences of entries into groups based on the contents of certain fields. The AT statement introduces a statement block that you end with the ENDAT statement. AT level.  statement block ENDAT.
LOOP AT itab.

 AT FIRST.... ENDAT.

 AT NEW f1....... ENDAT.

 AT NEW f2....... ENDAT.

 .......

 single record processing

 .......

 AT END OF f2.... ENDAT.

 AT END OF f1.... ENDAT.

 AT LAST..... ENDAT.

ENDLOOP.
FIRST
First line of the internal table
LAST
Last line of the internal table
NEW f
Beginning of a group of lines with the same contents in the field f and in the fields left of f
END Of f

End of a group of lines with the same contents in the field f and in the fields left of f


Specifying the Index in Loops When you process an internal table in a loop, you can specify the index of an index table to restrict the number of entries that are read: LOOP AT itab result [FROM n1] [TO n2] condition.  statement block ENDLOOP.

Changing Lines 

To change a single line of any internal table, use the MODIFYstatement.

Changing a Line Using the Table Key To change a single line, use the following statement: 

MODIFY TABLE itab FROM wa [TRANSPORTING f1 f2 ...].


Changing Several Lines Using a Condition
To change one or more lines using a condition, use the following statement:



MODIFY itab FROM wa TRANSPORTING f1 f2 ... WHERE cond.

Changing Table Lines Using the Index You can use the MODIFY statement to change lines in tables using their index. There is also an obsolete variant of the WRITE TO statement that you can use to modify lines in standard tables. Changing Single Lines using MODIFY To change a line using its index, use the following statement:


MODIFY itab FROM wa [INDEX idx] [TRANSPORTING f1 f2... ].

Deleting Lines 

To delete a single line of any internal table, use the DELETEstatement.

Deleting a Line Using the Table Key To use the table key of table itab as a search key, use one of the following statements:


DELETE TABLE itab FROM wa.
or


DELETE TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn.



Deleting Several Lines Using a Condition To delete more than one line using a condition, use the following statement:


DELETE itab WHERE cond.


Deleting Adjacent Duplicate Entries To delete adjacent duplicate entries use the following statement:



DELETE ADJACENT DUPLICATE ENTRIES FROM itab  [COMPARING f1 f2 ... |ALL FIELDS].



If you use the addition COMPARING f1 f2 ... the contents of the specified fields f1 f2 ... must be identical in both lines. You can also specify the fields f1 f2… dynamically using (n1) (n2) … as the contents of a field n1 n2. If n1 n2 is empty when the statement is executed, it is ignored. You can also restrict all fields f1 f2 … to subfields by specifying offset and length.

  If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.

Searching Through Internal Tables Line By Line 

Searching through Table Rows Using the statement:

FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern   IN TABLE itab ... [IN {BYTE|CHARACTER} MODE] ...


Searching Through and Replacing Table Rows Using the statement:

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern   IN TABLE itab ... WITH new [IN {BYTE|CHARACTER} MODE] ... 

From this post,ABAP consultant can learn all definitions and operation of internal tables and its importance in SAP ABAP .If we know the complete technical aspect on internal table ,then we can become well ABAP programmer.

To know more about SAP ABAP programming Basics ,read this post.

Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced