Types of Internal Tables in SAP ABAP

 


Types of Internal Tables in ABAP

Internal tables are a crucial part of ABAP programming, allowing developers to handle large amounts of data efficiently in-memory.

 1. Types of Internal Tables

In ABAP, internal tables are classified into two main types based on how they manage data and their performance characteristics:

 a. Indexed Internal Tables

- Standard Internal Tables

- Sorted Internal Tables

 b. Hashed Internal Tables

 Standard Internal Table

A standard internal table is the most basic type of internal table. It is called "standard" because it behaves like a simple list of entries.

Key Characteristics:

1. Allows duplicate records: You can store the same record multiple times.

2. Non-unique fields: All fields can be non-unique, meaning no field is required to be distinct.

3. Data insertion: Data is added to the table using the APPEND keyword, which adds records to the end of the table.

4. Linear search: Searching for records in a standard internal table is performed using a linear search, meaning the system checks records one by one, which can be slower for large datasets.

Syntax:

ABAP

DATA: it_standard TYPE STANDARD TABLE OF <structure>.

Sorted Internal Table

A sorted internal table is an internal table where records are automatically sorted based on a key when inserted.

Key Characteristics:

1. May or may not allow duplicates: You can choose whether to allow duplicate records based on the key.

2. Unique or non-unique fields: At least one field must be specified as a key, which can be either unique or non-unique.

3. Data insertion: Data is added using the INSERT keyword, which places records in the correct position to maintain sorting.

4. Binary search: Records are searched using a binary search, making searches faster, especially with large datasets.

Syntax:

ABAP

DATA: it_sorted TYPE SORTED TABLE OF <structure> WITH UNIQUE/NON-UNIQUE KEY <field>.

Hashed Internal Table

A hashed internal table is designed for fast access to records based on a key.

Key Characteristics:

1. Does not allow duplicates: Only unique records can be stored.

2. Unique fields: You must specify at least one unique field.

3. Data insertion: Data is added using the COLLECT keyword, which handles numeric fields in a special way. If a record already exists, numeric fields are added up, otherwise, the record is appended.

4. Hash algorithm search: Searches are performed using a hash algorithm, making access extremely fast.

Syntax:

ABAP

DATA: it_hashed TYPE HASHED TABLE OF <structure> WITH UNIQUE KEY <field>.

Internal Table with Header Line

In older versions of ABAP, it was common to declare an internal table with a header line. This means the internal table automatically comes with a work area of the same name. This is no longer recommended, but it’s still seen in legacy code.

Operations on Internal Tables

Once an internal table is defined, there are several common operations you can perform on it:

 a. Pushing Data (Inserting Data)

- Append: Adds a record to the end of a standard internal table.

    ABAP

    APPEND <wa> TO <it_standard>.

- Insert: Inserts a record in the correct position for a sorted internal table or simply adds it to the end in a standard internal table.

ABAP

    INSERT <wa> INTO TABLE <it_sorted>.

   

- Collect: Adds a record to a hashed internal table. If a record with the same key exists, numeric fields are summed.

ABAP

    COLLECT <wa> INTO <it_hashed>.

     b. Reading Data

You can read records from an internal table in two ways:

- Single record: Use READ TABLE to fetch one record.

- Multiple records: Use LOOP AT to iterate over the entire table.

 c. Modifying Data

You can change records in an internal table using the MODIFY keyword.

 d. Deleting Data

Records can be removed from an internal table using the DELETE keyword.

 e. Sorting Data

Internal tables can be sorted using the SORT keyword. This operation is useful for standard tables where you need to order records manually.

 7. Key Differences Between Internal Table Types

Feature 

Standard Table 

Sorted Table    

Hashed Table   

Duplicates

Yes

Optional

No             

Key Requirement      

No

Yes (unique or non-unique)

Yes (unique)

Insertion Method     

APPEND

INSERT

COLLECT

Search Method        

Linear Search  

Binary Search   

Hash Algorithm 

Performance 

Slow for large data

Moderate

Fast           


Internal tables are a powerful tool for handling data in-memory in ABAP. Understanding the differences between standard, sorted, and hashed internal tables can help you choose the right table type for your needs. Whether you need fast searching, unique records, or sorted data, internal tables offer flexible options to make your ABAP programs more efficient.

Post a Comment

Previous Post Next Post