Internal Tables in SAP

The next step to Field Strings is Internal Tables. The first primary difference between the field string and Internal tables is that a field string holds only 1 record while an internal table holds multiple records.

An Internal Table contains a Work Area and a Body.

Work Area

Body

..

..

..

..

..

..

..

..

..

..

For example, the data extracted from the employee table is stored in the application server in an internal table for further processing. The syntax to define an internal table is

BEGIN of IT_TAB   OCCURS n
..
END OF IT_TAB

Where IT_TAB represents the name of the internal structure and ‘n’ represents the number of rows. Since, the number of rows is not known normally leave that option out altogether or make it as OCCURS 0 and the internal table expands according to the data that is put in it.

You should remember that the internal table work area holds only 1 record at any given time. It’s the body of the internal table that contains the bulk of the data.

If we take our employee example, we can define an internal table as follows.

DATA: BEGIN OF IT_TAB OCCURS 0
      MANDT   like     MANDT,
      EMPNO   like     EMPNO,
      END  OF IT_TAB.

Now, we can select the data from the table ZEMPLOYEE into the internal table.

SELECT * from ZEMPOYEE into IT_TAB,
APPEND IT_TAB,
ENDSELECT.

The append keywords appends data from the internal table work area to the internal table body.  As usual we can print out the contents of the internal table just like we did for field strings.

LOOP AT IT_TAB.
WRITE:/ IT_TAB.
ENDLOOP.

To refer to the work area of the internal table, use IT_TAB. To refer to the internal table body, use IT_TAB[].  The work area only holds the last retrieved record. You cannot process the data in the internal table without first bringing it to the work area.

Let’s do a quick demo of the internal table.

Step 1: Goto transaction code [SE38] or ABAP Editor, enter the name of the program and click on create button.

Step 2 : Enter the title of the program ( which is like a small description that shows up on the GUI when the program is being executed ), enter the type of the program as Executable Program, and the status as SAP Production Program or Test Program. You can pick the application as HR since we are dealing with employees. Click on Save button. Enter the package that you want to save the program in or enter it as a local package.


For formal SAP ABAP Training or other SAP Training, please visit our home page.


Step No. 3 – Enter the Program Code

*&---------------------------------------------------------------------*
*& Report  ZEMPLOY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZEMPLOY.
TABLES: ZEMPLOYEE.  " This is a Pre-Defined Programming Structure of type ZEMPLOYEE Table
DATA: BEGIN OF  IT_TAB OCCURS 0,
      MANDT  LIKE ZEMPLOYEE-MANDT,
      EMPNO  LIKE ZEMPLOYEE-ZEMPNO,
      END OF IT_TAB.
SELECT * FROM  ZEMPLOYEE INTO IT_TAB.
APPEND IT_TAB.
ENDSELECT.
LOOP AT IT_TAB.
WRITE:/ IT_TAB.
ENDLOOP.

Step No. 4 : Test the Program and you should see results like these

Advertisements

One thought on “Internal Tables in SAP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s