Thursday 10 May 2012

SAP ABAP Internal Tables


Internal Tables
By definition, internal tables are user defined structured data types. These internal tables can be created as a replica of data base table, using some or all fields of one table or more tables. These internal tables are created only during run time no memory is reserved.

Why we need internal table
Long life data is stored in database tables when we are directly modifying data there is every possibility that we may loose data by accident, which we can not afford to do so, As such we need some intermediate etc tables to do some operations upon satisfactory results, we can modify database tables.

Defining Internal Tables:

Internal table with header line: When we create internal table with header line, a default work area is created.
Internal table with out header line: In this case, we need to create explicit work area to work with it. When we need to nest the tables with in tables, we need to use this type of internal table.

Types of internal tables

1. 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 tables. The key of standard table is always non-unique. Standard tables can always be filled very quickly, since the system does not have to check whether there are already existing entries.

Key access to a standard table uses a linear search. This means that the time required for a search is in linear relation to the number of table entries. You should use index operations to access standard tables.

2. Sorted Tables: - Defines the tables as one that is always saved correctly sorted by key. 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.

If the key is not unique, the system takes the entry with the lowest index. The runtime required for key access is logarithmically related to the number of table entries.

3. Hashed tables: - Hashed tables have no linear index. Hashed table is accessed using its key. The response time is independent of the number of table entries and is constant, since the system accesses the table entries using a hash algorithm. The key of a hashed table must be unique.

Defines the tables as one that is managed with an internal hash procedure. You can only access a hashed table using the generic key operations or other generic operations (sort, loop and so on) Explicit or implicit index operations (such as loop... from and insert itab with in and loop) are not allowed.

4. Index Table: - A table that can be accessed using an index. Index table is only used to specify the type of generic parameters in a from or function. That means that you cannot create a table of type Index. Standard and sorted tables are index tables.
Initializing Internal Tables

Clear: Clears the header (Work Area) line of the internal table.
Syntax: clear Itab.

Clear []: Clears the body of the internal table without clearing the work area or header.
Syntax: Clear itab [].

Refresh : Delete all table entries, memory space remains occupied i.e. only removes only contents of internal table.

Free : Delete all table entries, memory space is released i.e. de-allocates memory associated with internal table.

Operations of Internal Tables

1. Append: It will take data from work area or header into internal table body.
Syntax: Append itab (append itab to itab)
Append wa_itab to itab.

2. Collect: It will work similar to append operation but it will adds the same integer fields otherwise it will append it.
Syntax: Collect itab.

3. Insert: Used to insert data at any place. For this we have to specify the index no otherwise its goes to dump screen.
Syntax: insert itab index (Index number).

4. Modify: This operation will modify the existing records.
Syntax: Modify itab index (Index number).

5. Read: Used to read a single record from itab with key or index and it will read from application server to the screen.
Syntax: read itab with key (field name)

6. Sort: Used to sort the internal tables and by default it is ascending.
Syntax: Sort itab by (Field name).

7. Delete: Used to delete the data from internal tables but you need to give index number or range and field names.
Syntax: Delete itab by index (Index number)

8. Describe: Used to describe the internal tables like how many records and what kind of internal table etc.
Syntax: Describe table itab lines (variable name)
9.occurs
occurs 0 is key a word in abap used to create memory in body area , when ever we declare occurs 0 the system will allocates 8bytes of memory and it keep incrementing the memory to 8bytes every time.

occucs 1 is a key word in abap used to create memory in body area of an internal table. it allocates memory for one records only, and keep incrementing every time by one record memory.

Mostely prefered one is occurs 1 , in order to reduce the wastage of memory.

Example for internal table operations
*&---------------------------------------------------------------------*
*& Report ZBASU_INTERNAL_TABLE_OPERATION *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT ZBASU_INTERNAL_TABLE_OPERATION.

DATA: BEGIN OF ITAB OCCURS 0,
F1,
F2 TYPE I,
F3(3),
END OF ITAB.

DATA: LIN.

******Append Operation
ITAB-F1 = 'A'.
ITAB-F2 = 10.
ITAB-F3 = 'BC'.
APPEND ITAB.
CLEAR ITAB.

ITAB-F1 = 'B'.
ITAB-F2 = 15.
ITAB-F3 = 'BD'.
APPEND ITAB.
CLEAR ITAB.

******* Collect Operation
ITAB-F1 = 'A'.
ITAB-F2 = 20.
ITAB-F3 = 'BC'.
COLLECT ITAB.
CLEAR ITAB.

ITAB-F1 = 'C'.
ITAB-F2 = 25.
ITAB-F3 = 'BF'.
APPEND ITAB.
CLEAR ITAB.

*******Insert Operation
ITAB-F1 = 'D'.
ITAB-F2 = 30.
ITAB-F3 = 'BG'.
INSERT ITAB INDEX 2.
CLEAR ITAB.

*******Modify Operation
ITAB-F1 = 'F'.
ITAB-F2 = 40.
ITAB-F3 = 'BH'.
MODIFY ITAB INDEX 2.
CLEAR ITAB.

ITAB-F1 = 'F'.
MODIFY ITAB TRANSPORTING F1 WHERE F1 = 'D'.
CLEAR ITAB.

*******Sort Operation
SORT ITAB.
SORT ITAB BY F2.

******Read Operation
READ TABLE ITAB INDEX SY-TABIX INTO ITAB.
READ TABLE ITAB INDEX 3 INTO ITAB.
READ TABLE ITAB INTO ITAB WITH KEY F1 = 'D'.

WRITE:/ ITAB-F1,
ITAB-F2,
ITAB-F3.

******Describe Operation
DESCRIBE TABLE ITAB LINES LIN.

******Delete Operation
DELETE ITAB INDEX 2.
DELETE ITAB WHERE F1 = 'A'.

WRITE:/ 'NO OF LINES IN MY INTERNAL TABLE', LIN.
LOOP AT ITAB.
WRITE:/ ITAB-F1,
ITAB-F2,
ITAB-F3.
ENDLOOP.
 Courtesy:Prem

No comments:

Post a Comment