Internal table in abap


Internal Tables


internal table in abap

è It is a temporary table created in application server.

è The lifetime and scope of the internal table is within the program execution where it is created. i.e Once the Program execution is completed, the memory allocated for the internal table will be destroyed.

è Internal table can be created either with header or without header.

è A header of an internal table is like a work area (structure) which can be used to process the internal table.

è Work area can store only single record.

è The body of the internal table can store multiple records.

Syntax 1:  declaring internal tables

Data : begin of <internal table> occurs  <n>,
          Field1,
            Field2,
-      -  -  --,
           Field k,
End of <internal table>.
<n>-> ranges from 0,1,2,-  --  ---  ---
0 -> 8kb,
1 ->16kb,
2-> 24kb,----…multiples of 8

Note: In the above syntax, the occurs <n> will specify the initial amount of memory that needs to be allocated for the internal table. If the initial memory is utilized, the memory automatically gets increased to the amount of initial size.

Note: The above syntax is considered as internal table with header.

è  We generally use internal tables to store the data retrieved from database table’s,  process the data and then displaying the formatted(processed) data / update it back to database.

Append : 

This statement copies the data from work area to the end of the internal table.

Loop:
This statement is used for processing the internal table.

è Its copies each record from body to header.

è The number of times the loop execution depends upon the loop condition and internal table data.

SORT:

è  Before processing the internal table based on a condition, it is always’ recommended to “sort” the internal table based on the field on which the condition is specified.

è If the “sort field” is not specified, by default it will be “sorted” on first character field of the internal table. If no character fields are available in the internal table, then sorting will be done on first field of the internal table.

è If the “sorting order“ is not specified, by default  “sort” is in ascending order.

è The index of the internal table starts from 1.

è “Sy-tabix” is the system field which hold’s the index position of the internal table.

è Sy-index is the system field which stores the iteration value for the looping statements like “while end-while” and “do enddo”.

Describe :

 This statement counts the no of records of the internal table and stores the count value in the system field sy-tfill.
If we use the lines addition as part of describe statement, the count value is stored inside the program variable specified as part of lines addition.

Syntax:

Describe table <internal table> [ lines  <variable> ].

Note: Occurs statement in declaring the internal table allocates initial memory for the internal table, if the initial memory is utilized, the memory automatically gets extended to the amount of initial size.

Note: We can use loop statement to process all records / records based on range of indexes / records based on condition.

Second syntax for internal table:-


Data: <internal table> like <existing structure> occurs <n>
                                                                        [With header line]

In case of above syntax the internal table can be declared either with header or without header.

è The above syntax will copy only the structure but not the data.

è To copy data from one internal table to other internal table we can use either “append lines” statement or directly use the “=” operator / “move” statement with [ ] (in case of internal table with headers).

Append lines: It will copy all the records / range of records (based on indexes) from source to target internal table.

To delete the records from internal tables, we can use the following:


1.     Refresh: It will delete all the records; it is always used only on internal table.

2.     Clear: It will delete all records, use [ ] (for internal tables with header). Clear can be used on normal variables or work areas also.

3.     Free: It will delete all the records; it is always used only on internal table. The difference between refresh and free is refresh clears the data but doesn’t de-allocate the memory , whereas free statement clears the internal table data and gets the internal table to initial state.

4.     Delete: Deletes the records of the internal table based on a condition

5.     Delete adjacent duplicates: Deletes adjacent duplicate records from internal table based on the comparing field. The pre-requisite for using this is the data in the internal table should be sorted based on the comparing field (field specified in delete adjacent duplicates statement).

è In the first syntax and in second syntax we must use “occurs” keyword as part of internal table declaration.

è The “occurs” keyword will allocate some initial memory; this memory automatically gets extended depending on the usage.

è If the allocated memory is not utilized, then there will be memory wastage, to avoid this we can use following “standard syntax”.

Standard syntax of declaring the internal table:

Step 1:  Declare the required fields using the type’s declaration. This declaration provides a template of the internal table and work area.

Syntax: 

Types : begin of <type name>,
  Field1,
  Field 2,
-      - - ---,
Field n,
End of <type name>.

Note: “Types” keyword is used for creating a user defined data type. It doesn’t allocate any memory, it only provides template of the fields (field name, field type, field size)

Step 2:  Declare the internal table based on types declaration

Syntax:

Data : <internal table> type <standard table of>/<sorted table of>/ <hashed table of>   <type name> [with header line].

Note: The default internal table type is ‘standard’.
In the above syntax if the internal table is declared without header then perform step 3.

Step 3: Declare the work area based on types declaration.

è This work area (structure) can be a normal work area or a field symbol work area.

Syntax for normal work area:-

Data <work area> type <type name>.

Syntax for field-symbol work area:-

Field-symbols <<field symbol variable>> like line of <internal table>.

Note: If we use normal work area as part of loop, we need to use ‘into’ and if we use field-symbol work area as part of loop, we need to use ‘assigning’ keyword.

è It is always recommended  to declare the internal table without header

è To process this internal table we need to declare explicit work area.

Standard Internal tables

1.     Key specification is optional

2.     Supports only non-unique key specification

3.     Sort without the sorting field will sort the data on key specification field (if declared) otherwise it sorts on first character field of the internal table (if available) otherwise first field of the internal table

4.     Append and Insert statements are supported, Append statement always adds data to the end of the internal table and insert can be used to add the data either at the end or in a specific index position of a internal table

5.     Supports indexed search, linear search (default), binary search

Read statement:

è It is used for reading specific record from the internal table.

è It always read’s single record.

è If the read is successful the system field “Sy-subrc” is set to 0. Other wise 4.

è Sy-subrc is a system field used for storing the execution status of ABAP statements.

è Read statement can be used either based on the index or based on a condition.

Note: If the read is successful, Sy-subrc is set to zero and the content of the record read is copied to work area specified in read statement

è Before/while using Read statement, it is recommended to perform the following to improve the performance:-

è A) Sort the internal table based on search field

è B) Use the transporting addition as part of read statement to transport only specific fields from body to work area; otherwise all fields are transported from body to work area.

è C) Use the addition binary search in the read statement to minimize the search, before that, sort the internal table based on search field; otherwise using binary search doesn’t have any effect.

è Use the addition ‘transporting no fields’ as part of read statement when we need to check only the existence of the record but not interested in the content of the record. In this, it is better not to specify any work area as part of read statement as we are not transporting any data from body to work area, otherwise it leads to warning.

è It is always recommended to “clear” the variable values before using them, otherwise if the variable is used as it is, it refers to previous value (last value stored in the variable) which leads to data inconsistency. Here a variable can be an individual variable or work area.

Modify statement:

This statement is used for updating the internal table as well as database table. In case of modifying the internal tables we need to modify referring to work area.

è As part of using “modify” statement it is always recommended to use “transporting” option so that only the fields specified in the transporting addition/option will be compared between work area and corresponding record in the internal table body. If the transporting option is not specified, all the fields will be compared between work area and record in body which decreases performance.

è Using the modify statement inside a loop will decrease the performance.

è To overcome this, it is recommended to use the following:

  A) As Part of loop, use Field symbol work area instead of normal work area so that any changes made to the data of the field-symbol work area will automatically update the corresponding row in the internal table (field-symbol work area will act as pointer). i.e explicitly we need not use the modify statement.

   B) Use modify statement outside loop using where clause referring to work area.

Sorted internal tables

1.     Key Specification is Mandatory

2.     Supports both non-unique and unique key specifications

3.     Append and Insert statements are supported, but in case of Append, we need to ensure the sorting sequence based on key specification field and also provide unique values for the unique key field, otherwise it leads to runtime error. Because of this reason, it is recommended to use ‘insert’ statement on sorted internal tables.

4.     If the sorted internal table is declared using unique key specification and if we try to insert any duplicate entries for the unique key field using insert statement, it ignores the duplicate entry and considers only the first entry which is inserted.

5.     Sort statement is not supported

6.     Supports indexed, linear and binary search for searching a record using read statement

Hashed internal table

è Key specification is mandatory

è Supports only unique key specification

è Doesn’t support explicit or implicit index operations            
                                                       
è i.e. Implicit à Append statement not supported
è       Implicit à Read with Binary search not supported
è       Explicit à insert with index not supported
è       Explicit à Read with index not supported
     Small tables à Standard internal tables
     Huge tables àHashed internal table 
     For defaulting sorting à sorted internal table

i.e. In case of standard internal tables, time consumed for searching a record depends on the no. of records/size of internal table.

In case of hashed internal tables, irrespective of no. of records/size of internal table, time consumed for searching a record is constant.

**Note: If anybody wants some example related with this topic then kindly mail or comment below with your mail id. I will send some important example related with this topic.



Post a Comment

0 Comments