Summary -

In this topic, we described about the below sections -

Internal tables used during the program execution for processing the data dynamically. So, the internal table should be created in the program where it is using.

DATA statement is used to declare an internal table. The declaration should complete between the BEGIN OF and END OF statements.

OCCURS clause is used to specify the number of lines/records contained by the table initially. If the OCCURS number is 0, then it specifies the table contains no records initially.

The table can expand to any number of records based on the insertion and there is no specified limit for the same.

How to create internal table?

The internal table can be created in many ways. Those are -

  • Using TYPE statement
  • Using existing Structure
  • Using existing table

Let us discuss one by one in detail.

Create Table using TYPE statement -

This process has two steps. The table structure created with the TYPE statement as shown below -

TYPE BEGIN OF <structure-name>
	.
	<fields-declaration>
	.
END OF <structure-name>.

structure-name structure is created in the above step. Next, the internal table creates by using the structure name.

DATA <table-name> TYPE <structure-name> OCCURS n [WITH HEADER LINE].

Table with the table-name created of type structure structure-name of n occurances. WITH HEADER LINE is optional. If the table declared with WITH HEADER LINE, then it is internal table with header line. If WITH HEADER LINE is ignored, then it is internal table without header line.

For example -

Create a student table with structure.

Code -

TYPE BEGIN OF sstudent.
	no			TYPE I,
	name(20) 	TYPE C,
	dob(10)		TYPE C,
END OF sstudent.

DATA tstudent TYPE sstudent OCCURS 10 WITH HEADER LINE.

Explaining Example -

sstudent is a structure declared with no, name and dob. Table tstudent declared of type sstudent with 10 lines. Table is declared with WITH HEADER LINE. So the table is internal table with header line.

Using existing Structure -

Creating table with some existing structure syntax shown below -

DATA <table-name> TYPE <structure-name> OCCURS n [WITH HEADER LINE].

Table with the table-name created of type structure structure-name of n occurances. WITH HEADER LINE is optional. If the table declared with WITH HEADER LINE, then it is internal table with header line. If WITH HEADER LINE is ignored, then it is internal table without header line.

For example -

Create a student table tstudent from existing structure sstudent.

DATA tstudent TYPE sstudent OCCURS 10.

Explaining Example -

Table tstudent declared of type sstudent with 10 lines.

Using existing table -

Creating table with existing table syntax shown below -

DATA <table-name> TYPE <table1> OCCURS n [WITH HEADER LINE].

Table with the table-name created of another table table1 type of n occurances. table-name defined with the structure of another table table1. WITH HEADER LINE is optional. If the table declared with WITH HEADER LINE, then it is internal table with header line. If WITH HEADER LINE is ignored, then it is internal table without header line.

For example -

Create a student table tstudent from existing table tstd.

DATA tstudent TYPE sstd OCCURS 10 WITH HEADER LINE.

Explaining Example -

Table tstudent declared of another table sstd type with 10 lines. The table sstd structure applied to tstudent.



Example -

Simple example to create product information internal table with below structure.

Pid -Character (10)
Pname - Character (40)
Pamount - Decimal (10)


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* Internal table Structure creation
TYPES: BEGIN OF t_product,
       pid(10)     TYPE C,
       pname(40)   TYPE C,
       pamount(10) TYPE P,
       END OF t_product.

* Data & internal table declaration
DATA: wa TYPE t_product,
      it TYPE TABLE OF t_product.

wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.

* Appending data to the internal table
APPEND wa TO it.

* Reading internal table of index 1
READ TABLE it INTO wa INDEX 1.

* If sy-subrc equal to ZERO, the internal table read is successful.
* Otherwise, 'No Record Found' gets displayed.
IF sy-subrc = 0.
  WRITE: wa-pid, wa-pname, wa-pamount.
ELSE.
  WRITE 'No Record Found'.
ENDIF.

Output -

Creating Internal Table Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

t_product - Structure declaration, wa - work area of type t_product, it - internal table declaration of type t_product, APPEND wa TO it - Appending work area(wa) to the table(it), READ TABLE it INTO wa INDEX 1 - Reading internal table with index 1.