Google Search - Blog...........

SAP ABAP - Internal Table - Introduction, Syntax & Examples.

Consider the following cases:

• You want to reorganize the contents of table.
• You want to modify few details of table and then display the contents of table to user.
• You want to perform table calculations on subset of database table.

In above cases you need to recognize or change the database table contents.

In ABAP/4 you work mainly with tables. Long life data is stored in database tables. You cannot afford to lose data from database table. In such cases where you can not work directly with database table (where you are modifying contents of table or reorganizing the contents of table or any other case where you are altering contents of table and then displaying output to the user) hence need of intermediate table where you put in all the data from database table and work with this data thus avoiding accidental loss of data.

These intermediate tables are called INTERNAL TABLES in ABAP/4 and are created only during runtime i.e., no memory is reserved for internal tables.

When you use Internal Table in a program, three steps are associated with it.

1. Declaration of Internal Table
2. Populating Internal Table
3. Processing Internal Table

Declaration of Internal Table

Depending on how you create, internal tables are of two types.

• Internal tables with header line.
When you create internal tables with header line, and then default workarea or header is created. And you can work with this header.

• Internal Table without header line.
In this case you need to create output explicit workarea to work with table. Only advantage of internal tables without header line is that you can nest them i.e., you can have table within table, which is not possible on internal tables with header line.


Internal table can be declared in the following way:

• Data : Itab like sflight occurs 0 with header line
Here you are declaring internal table, which is similar to sflight i.e., itab like sflight i.e., you are referring to existing table (like).
(By default internal table created with this declaration is without header line).

• Data : Begin of itab occurs 0.
Include structure sflight
Data : End of itab
(Internal Table created with this type of declaration is similar to declaration done in ‘a’ type the only difference is by default internal table created by this type is with header line)

• Data : Begin of itab occurs 0
carrid like sflight-carrid,
connid like sflight-connid,
fldate like sflight-f1date
End of itab.
By default internal table created by this type of declaration is with header line. In this type of declaration, you are using only those fields from database table, which you require for processing.

• Data : Begin of itab occurs 0
carrid like sflight-carrid,
connid like sflight-connid,
bookid like sbook-bookid
id like scustom-id,
End of itab.
Here you are combining fields from three different tables in one internal table.

• Data : Begin of itab occurs 0
Carrid1 like sflight-carrid,
End of itab.
Here you are specifying different field names.


Populating Internal Table

• Itab-name = ‘ABCD’.
Append Itab.
(In this case itab is filled with one name i.e., ‘ABCD’.)

• Do 5 times.
Itab-number = sy-index.
Append itab.
Enddo.
(In this case itab is filled with sy-index for 5 times)

• Select * from sflight into itab.
Append itab.
Endselect.

• Select * from sflight into table itab.
Note: Addition of Table in INTO clause, you omit append itab and Endselect

• Select * from sflight
Move-corresponding sflight to itab.
Append itab.
Endselect.

Note: While using Move-corresponding, field names of DB table & Int’ table should be same.

• Select * from sflight.
Move sflight to itab.
Append itab.
Endselect.
Note: In this case structure of sflight and itab should be similar

• Select carrid1 connid1 from sflight into (itab-carr1, itab-connid1)
Append itab.
Clear itab.
Endselect.


Processing Internal Table

Processing of internal table includes:

• Writing: write : / itab carrid. (will write only one field)
Loop at itab. (Will write whole internal table)
Write itab
Endloop.

• Reading: You can read internal table by:
Read table itab with key carrid = ‘LH’ (Here you are reading table with key)
Or Read table itab with index 3. (Here you read table with index 3)
(Note: Reading of internal table can be done inside the loop or outside the loop)

Modifying: You can modify contents of internal table by specifying key or index.
Itab-carrid = ‘LM’
Modify table itab index 3.

• Delete: delete table itab index 3.
(Will delete record with index 3)

Commands associated with clearing of internal tables are as follows:

• Clear itab : Will clear header of internal table
• Clear itab [ ] : Will clear body of table.
• Refresh itab : Will remove contents of internal table.
• Fee itab : Will de-allocate memory associated with internal table.
Sorting of Internal Tables

To sort contents of internal table you can use

SORT ITAB

This command will sort internal table on all non-numeric primary keys in ascending order.

To specify internal table for given key the syntax is,

SORT ITAB BY CARRID ASCENDING.

In this case the table itab is sorted with carrid key in ascending order.
You can sort table in either ASCENDING or DESCENDING order. The default order is ASCENDING.

You can sort table with multiple keys also. The number of SORT key fields is restricted to 250. If you specify more than one field then the system sorts the record first by f1 then by f2 and so on (Here f1, f2 are fields).

No comments:

Post a Comment