0% found this document useful (0 votes)
120 views66 pages

Baics and Classical Reports

The document provides an overview of internal tables in ABAP. It discusses that internal tables exist only at runtime and memory is managed dynamically. It describes the three parts of internal tables - rows, columns, and work area. It explains the differences between implicit and explicit work areas when declaring with and without header lines. It also discusses the three types of internal tables - standard, sorted, and hashed - and how they can be accessed and declared.

Uploaded by

Ram Praneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views66 pages

Baics and Classical Reports

The document provides an overview of internal tables in ABAP. It discusses that internal tables exist only at runtime and memory is managed dynamically. It describes the three parts of internal tables - rows, columns, and work area. It explains the differences between implicit and explicit work areas when declaring with and without header lines. It also discusses the three types of internal tables - standard, sorted, and hashed - and how they can be accessed and declared.

Uploaded by

Ram Praneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Internal Table Basics

Internal table is a data object in ABAP that exists only at run time of the program. It means when the
program execution is complete then the internal table will be lost. We use internal table to store
database table after fetching it by a select query. The ABAP runtime system dynamically manages the
internal table’s memory. It means we developer do not need to work on memory management of
internal table.

Internal table has three parts – rows, columns and work area.

1. Rows are the line type of internal table. It is a structure which contains several fields. Those
fields are of data elements. We need to declare the structure locally or globally to declare the
internal tables.
2. Columns are the fields of internal table. Those fields are of different data element declared by
locally or globally.
3. The most important part of the internal table is its work area. Work area is basically the line type
of internal table. It means it has the same structure of the rows of the internal table. Work area
contains the same field of same type of the rows. It is of two types Implicit and Expicit work
area.
A. When we declare an internal table with header line then a work area will automatically be
created with the same name of the table. This work area is called the implicit work area which is
actually the header line. There is no need to declare the work area separately. This work area /
header line contains the same table as of the internal table.
Example:

TYPES: BEGIN OF ty_mat,


matnr TYPE mara-matnr,
werks TYPE marc-werks,
lgort TYPE mard-lgort,
END OF ty_mat.

DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY


matnr WITH HEADER LINE.

Here we have declared a local structure ty_mat. This is the line type / row of the internal table. It means
the table will contain rows which has three fields (matnr, werks & lgort). We declare the internal table
it_mat with this local structure. We also can declare with global structure.

DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH HEADER LINE


WITH NON-UNIQUE KEY type.

Here slis_qinfo_alv is a structure which has been declared globally in data dictionary. We can declare the
internal table directly with the table type also.

DATA: it_qinfo TYPE slis_t_add_fieldcat WITH HEADER LINE.


Here slis_t_add_fieldcat is a table type declared in data dictionary.

Header line concept:

MATNR WERKS LGORT

The name of this work area / header line is IT_MAT.

When we create the internal table then it is like following:

MATNR WERKS LGORT

It also contains the same name IT_MAT but it is mentioned IT_MAT[ ] in the program.

B. If we declare an internal table without header line then we need to declare its work area seperately.
Since we are declaring the work area explicitly it is called explicit work area. This work area contains the
different name from the internal table.

Example –
TYPES: BEGIN OF ty_mat,
matnr TYPE mara-matnr,
werks TYPE marc-werks,
lgort TYPE mard-lgort,
END OF ty_mat.

DATA: it_mat TYPE STANDARD TABLE OF ty_mat,


wa_mat TYPE ty_mat.

Similarly we can declare internal table with globally declared structure or table type also.
DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH NON-UNIQUE KEY type.
DATA: it_qinfo TYPE slis_t_qinfo_alv.

Work area concept:

MATNR WERKS LGORT

The name of this work area is WA_MAT.

When we create the internal table then it is like following:

MATNR WERKS LGORT

The table contains the name IT_MAT.


In today’s programming header line is not used in internal table. It is now obsolete. There are two main
reasons for that.
1. The automatically generated header line / implicit work area has the same name as of internal table.
That’s why it loses the readability of program.
2. In object oriented programming header line is not allowed.

To declare an internal table there is three basic specifications. They are 1. Row type, 2. Key & 3. Types
of internal table. Internal table is of three types – standard table, sorted table & hashed table.
Standard & sorted tables are called index table because we can access its records by its index. Index is
nothing but a row number of the internal table.

1. Standard table is an index table which has non-unique key. It can be accessed by index or key also. If
we want to access by key then the key must be defined otherwise default key would be considered. The
declaration is as follows:

DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.
OR
DATA: it_mat TYPE TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

If we don’t mention “Standard table of” clause then by default the system takes it as a standard internal
table. We can enter data into a standard internal table by using the APPEND statement. Append always
enters data at the last row of the table.

APPEND wa_mat TO it_mat.

2. Sorted table is another kind of index table which has unique / non unique key. It also can be accessed
via index or key. For sorted table the key must be specified. The declaration is as follows:

DATA: it_mat TYPE SORTED TABLE OF ty_mat WITH UNIQUE KEY matnr,
it_mat TYPE SORTED TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

Unique key means the MATNR (material no) will must be unique. If same material number is inserted
then a run time error will happen. However we can declare the sorted table with non unique key also. In
this case same material number can be entered but it will be sorted after entering the number. Here the
sorted table behaves similar to sorted standard table. We use INSERT statement to enter any records to
the sorted table.

INSERT wa_mat INTO it_mat.

3. Hashed table is not an index table. It follows the hash algorithm. Here the declaration of key is must and
also the key must be unique. Hence no duplicate entry will be in the hashed table. We can access
records only by the key.

DATA: it_mat TYPE HASHED TABLE OF ty_mat WITH UNIQUE KEY matnr.

Similar to sorted tables data can be inserted here by INSERT statement. Hashed tables are used when
the internal table contains huge volume of data.

INSERT wa_mat INTO TABLE it_mat.


Index Tables
Table kind Hashed Tables
Standard Table Sorted Table
Index Access Yes Yes No
Key Access Yes Yes Yes
Key Uniqueness Non unique Unique/Non unique Unique
Usage Index access Key access Only key access

Standard Internal Table

Standard table is an index table which has non-unique key. It can be accessed by index or key also. If
we want to access by key then the key must be defined otherwise default key would be considered. The
declaration is as follows:

DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.
OR
DATA: it_mat TYPE TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.
OR
DATA: it_mat TYPE TABLE OF ty_mat.

If we don’t mention “STANDARD TABLE OF” clause then by default the system takes it as a standard
internal table. We can enter data into a standard internal table by using the APPEND statement. APPEND
always enters data at the last row of the table.
APPEND wa_mat TO it_mat.

We can sort data records in the standard table.


SORT itab BY item.
Here item is the field of the table. We can sort table by any of its fields or multiple fields. Here we have an
example of Standard table.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab WITH NON-UNIQUE KEY item,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

WRITE: /3 'Item',
17 'Quantity',
32 'Price'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.
Sorted Internal Table

Sorted table is another kind of index table which has unique / non unique key. It also can be accessed
via index or key. For sorted table the key must be specified. The declaration is as follows:

DATA: it_mat TYPE SORTED TABLE OF ty_mat WITH UNIQUE KEY matnr,
it_mat TYPE SORTED TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

Unique key means the MATNR (material no) will must be unique. If same material number is inserted
then a run time error will happen. However we can declare the sorted table with non unique key also. In
this case same material number can be entered but it will be sorted after entering the number. Here the
sorted table behaves similar to sorted standard table. We use INSERT statement to enter any records to
the sorted table.

INSERT wa_mat INTO TABLE it_mat.

Here is an example of sorted table by using unique key concept.


REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Sorted internal table with non unique key


DATA:
itab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY item,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


INSERT wtab INTO TABLE itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


INSERT wtab INTO TABLE itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


INSERT wtab INTO TABLE itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


INSERT wtab INTO TABLE itab.
WRITE: /3 'Item',
13 'Quantity (KG)',
28 'Price (Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Since the key is unique the similar entries are ignored by the system.

Here is an example of sorted table by using non unique key concept.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


INSERT wtab INTO TABLE itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


INSERT wtab INTO TABLE itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


INSERT wtab INTO TABLE itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


INSERT wtab INTO TABLE itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Hashed Internal Table


Hashed table is not an index table. It follows the hash algorithm. Here the declaration of key is must and
also the key must be unique. Hence no duplicate entry will be in the hashed table. We can access
records only by the key. Similar to sorted tables data can be inserted here by INSERT statement. Hashed
tables are used when the internal table contains huge volume of data.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Hashed internal table with non unique key


DATA:
itab TYPE HASHED TABLE OF ty_tab WITH UNIQUE KEY item,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


INSERT wtab INTO TABLE itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


INSERT wtab INTO TABLE itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


INSERT wtab INTO TABLE itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.
Continue Statement

The CONTINUE statement is used inside a loop only. When the program controller finds the CONTINUE
statement it quits the current loop pass. Hence all other statements after the CONTINUE will not be
executed inside that loop. The program controller then goes to the next loop iteration.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.


IF wtab-item = 'Rice'.

* Continue always moves to the next iteration of the loop


CONTINUE.
ENDIF.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Whenever the program controller finds Rice item it just ignores it and goes to next loop iteration. That is
why Rice item is not coming to the output.

Check Statement

The CHECK statement checks a condition first inside a loop. If the condition is true then rest of the
statements inside that loop will be executed. If the condition is false then the program controller
terminates the current loop pass and it will go to the next loop iteration. Hence the condition is the
prerequisite of CHECK statement. No condition will lead the syntax error of the program. Outside of the
loop CHECK will leave the current processing block of the program.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

* Sorting the itab by item.


SORT itab BY item.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

* If the condition is true then it will execute further statements


* If CHECK condition is false then it will move to the next iteration of loop
CHECK wtab-item = 'Rice'.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Whenever the check condition finds Rice item it goes to the rest of the statements of current loop. If the
condition finds false (other than Rice) it leaves the current loop iteration and goes to the next loop pass.

Exit Statement

The EXIT statement quits the processing of the current loop. If we use EXIT outside of the loop then it
leaves the current processing block. Hence no other statement of that processing block will be executed.
It is recommended that EXIT needs to be used inside a loop. We can use it outside of loop as per
requirement as well.
If there are nested loops and we use EXIT statement in the inner loop then the system will leave the inner
loop after executing the EXIT statement. The program control will continue with the outer loop execution.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

* Sorting the itab by item.


SORT itab BY item.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.


WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.

WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.


IF wtab-item = 'Rice'.

* Exit always quits the control from the loop iteration


* After exit the loop execution will be finished
EXIT.
ENDIF.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Here the first set of output has come without the exit statement. Second set has come by exit statement.
Whenever the program controller finds the item Rice then it executes exit. The system leaves the loop
totally. That is why only one output has come yet.

Read Table and Describe Table

There are different kinds of fetching the records of internal table. One of these is READ TABLE
statement. By using READ TABLE we can fetch only the first record from the table. Hence it can fetch a
single record always. Read table statement can be used to any type of table to fetch the record. Now if
the table is sorted then we can use BINARY SEARCH to fetch record. Now fetching records may take
time but if we use BINARY SEARCH then the system performance will improve. Obviously if the table is
sorted then the fetching operation will be faster.

We can describe an internal table as well. If the table stores 300 records then the system will inform us by
the system field SY-TFILL. Here note that SY-TFILL will not work alone. We need to describe the table
first then we can get data from SY-TFILL field.

DESCRIBE TABLE itab.


WRITE: / 'Number of table records: ', sy-tfill.
Here we are describing the internal table itab. After that we are getting records from SY-TFILL.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.
SORT itab BY item.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

READ TABLE itab INTO wtab WITH KEY item = 'Rice'


BINARY SEARCH.
IF sy-subrc = 0.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDIF.

SKIP.
WRITE '=========================================='.

DESCRIBE TABLE itab.


WRITE: / 'Number of table records: ', sy-tfill.

Though the output is showing only one record the table still contains six
records.

Modify Table Statement

Internal table can be edited as per requirement. We can modify one particular record when it is required.
Two kinds of statements can be used to modify one single line.

MODIFY TABLE internal_tab FROM work_area.


MODIFY internal_tab FROM work_area INDEX sy-tabix TRANSPORTING field1 field2.

Note that by these statements one single line can be modified. We can add more fields as per
requirement. We can modify any type of internal table by these statements. After modification the old
record is lost completely from the table. If we store it to any outer structure before modification then the
old record can be reused.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.


WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.
SKIP.

LOOP AT itab INTO wtab.


IF wtab-item = 'Horlicks'.

wtab-item = 'Complan'.
wtab-price = 220.
MODIFY TABLE itab FROM wtab.
ENDIF.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
Here the item Horlicks has been modified to Complan.

Delete Table Statement

Internal table can be edited as per requirement. We can delete a particular line of it based on the
condition. The statement is “DELETE TABLEitab FROM wtab.” Here itab is internal table and wtab is
its work area. This statement can be used in any type of internal table whenever there is a requirement.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next appending one single row data into the table
APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


APPEND wtab TO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


APPEND wtab TO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


APPEND wtab TO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


APPEND wtab TO itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


APPEND wtab TO itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
IF wtab-item = 'Rice'.
DELETE TABLE itab FROM wtab.
ELSEIF wtab-item = 'Suger'.
DELETE TABLE itab FROM wtab.
ENDIF.
ENDLOOP.

LOOP AT itab INTO wtab.


WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.
SKIP.
Whenever the program controller finds Rice & Suger it deletes from the internal table.

Delete Adjacent Duplicate

DELETE ADJACENT DUPLICATE statement works logically on sorted standard table and sorted
table with non-unique key. It compares the adjacent rows. If similar records are found based on the
comparing fields then it deletes from the second records onward. The system keeps only the first record.
Let us suppose we are having the following table with records.

Item Quantity (KG) Price (Rs)


Rice 2 100
Sugar 1 50
Rice 3 150
Tea 1 100
Sugar 2 120

We know that in standard table APPEND statement is used to enter data records. Now APPEND
appends data to the last position of the standard table. It is not possible to enter in any other position.
Now if we run the Delete adjacent duplicate command on the previous table then the data records will
remain same. Nothing will be changed. Because the command will check every single adjacent line and
found nothing to similar. Hence in this case we need to sort the table (standard table).

Item Quantity (KG) Price (Rs)


Rice 2 100
Rice 3 150
Sugar 1 50
Sugar 2 120
Tea 1 100
Now we have found the sorted structure of the data records. In this position if the command runs then it
will find two similar records based on the comparison of the item field. After evaluating this command we
shall have the following.

Item Quantity (KG) Price (Rs)


Rice 2 100
Sugar 1 50
Tea 1 100

The command will remove always the second record onward. It will keep only the first record. Here if we
use Sorted table with non-unique key then after inserting the records we can find the sorted structure of
the data records. That is why DELETE ADJACENT DUPLICATE command always run sorted standard
table or sorted table with non-unique key properly.

The statement is as follows.


DELETE ADJACENT DUPLICATES FROM itab.

If we want to mention specific fields then it will be like this.


DELETE ADJACENT DUPLICATES FROM it_ekpo COMPARING ebeln.

In this statement we are comparing EBELN (PO No.). If we don't mention this then all fields will come into
the comparison. Then any change in any field will be compared and it will fall into the unique category. So
comparing field or fields is very important for this to avoid logical error.

REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Sorted internal table with non unique key


DATA:
itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


INSERT wtab INTO TABLE itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


INSERT wtab INTO TABLE itab.

wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.


INSERT wtab INTO TABLE itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


INSERT wtab INTO TABLE itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

DELETE ADJACENT DUPLICATES FROM itab.

LOOP AT itab INTO wtab.


WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.

Collect Statement

COLLECT statement can be used in the internal table whose non key fields are all numeric. It means the
statement finds the key (non numeric fields) and if the records are same then the statement will add all
other numeric field values. The prerequisite of this statement is that the internal table must have numeric
data type fields which are non key fields. COLLECT statement can be applied on any type of internal
tables.

Item Quantity (Kg) Price (Rs)


Rice 2 100
Wheat 1 40
Rice 1 60
Tea 1 120
Sugar 1 90
Wheat 3 90

If we run the collect statement upon this table then the output will be as following

Item Quantity (Kg) Price (Rs)


Rice 3 160
Wheat 4 130
Tea 1 120
Sugar 1 90
REPORT zabap_gui.

* Declaring the local structure of internal table


TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.

* Declaring the Standard internal table with non unique key


DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.

* Entering records to each field


wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data


* Next collecting one single row data into the table
COLLECT wtab INTO itab.

wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.


COLLECT wtab INTO itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.


COLLECT wtab INTO itab.

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.


COLLECT wtab INTO itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.


COLLECT wtab INTO itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.


COLLECT wtab INTO itab.

WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line

LOOP AT itab INTO wtab.

WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.
Control Break - AT NEW statement
AT NEW is one of a control break statements. It works inside the LOOP – ENDLOOP. Let’s take an
example. We have prepared an internal table where we are storing 50 rows of material, plant & storage
location data. Now we run the operation of AT NEW for plant record. Whenever the system finds a new
plant with respect to previous then it will trigger the AT NEW statement. Here the internal table must be
sorted. Otherwise similar plant may come afterwards and system will trigger AT NEW wrongly.

The program is as follows.

TYPES: BEGIN OF ty_tab,


werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.

DATA: wtab TYPE ty_tab,


itab TYPE TABLE OF ty_tab.
START-OF-SELECTION.

SELECT matnr werks lgort


UP TO 30 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.

IF sy-subrc = 0.
SORT itab.
WRITE: / 'Material', 20 'Plant', 27 'Storage Location'.
ULINE.

LOOP AT itab INTO wtab.


WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.

AT NEW werks.
WRITE: '***** AT NEW plant triggers at ', sy-tabix.
ENDAT.
ENDLOOP.
ENDIF.

Now we shall see at debugging mode how AT NEW works. At the first loop iteration AT NEW will
definitely trigger because the plant is read as new. Here SY-TABIX = 1.

After entering into AT NEW, we can see the other fields which are right side of the mentioned field contain
***. Only the mentioned field contains the proper data.

After that whenever system finds a new plant data it will trigger AT NEW similarly. Here at SY-TABIX = 3.

Control Break - AT END OF Statement

AT END OF statement always triggers when there is any change of fields’ data. This statement triggers at
the last occurrence of that value. Let’s take an example. We have a table MARD which contains material,
plant & storage location. Now we want to trigger AT END OF at the last occurrence of plant data as
follows.
As we can see that the statement triggers at the last occurrence of plant data.

REPORT zctrlbrk_at_end_of NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab,


werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.

DATA: wtab TYPE ty_tab,


itab TYPE TABLE OF ty_tab.

START-OF-SELECTION.

SELECT matnr werks lgort


UP TO 30 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.

SORT itab BY werks.


WRITE: / 'Material Number', 20 'Plant', 27 'Storage Location'.
ULINE.

LOOP AT itab INTO wtab.


WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.

AT END OF werks.
WRITE: '=== At End Of plant - triggers at line ', sy-tabix.
ENDAT.
ENDLOOP.

Here we have shifted the plant (WERKS) at field 1 in structure level. Otherwise system will consider
material & plant both as a key. Because the AT END OF will trigger if there is any change from left most
field to the particular field.

Now we want to see at the debugging level of AT END OF statement. On second line it has triggered
because that is the last occurrence of plant 1200. SY-TABIX = 2. For the rest of the rows system will skip
the AT END OF statement.

Similarly it has triggered at last occurrence of plant 3000. SY-TABIX = 15.

There is only one entry for plant 7500. So it triggers at the next loop iteration. SY-TABIX = 16.
At the last of the table record AT END OF triggers. Here the plant occurrence & table data both are of last
entry.

Similarly at SY-TABIX = 16 it will be triggered.


In this way AT NEW is triggered inside the loop.

Control Break - AT FIRST and AT LAST

AT FIRST triggers at the first loop iteration whereas AT LAST triggers at the last loop iteration. We can’t
mention any particular field here as we can do in AT NEW or AT END OF. The main purpose of this
control break is to write some header or footer information. We can write some header info by using AT
FIRST statement and similarly some footer info by using AT LAST statement.

REPORT zctrlbrk_at_first_last NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab,


werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.

DATA: wtab TYPE ty_tab,


itab TYPE TABLE OF ty_tab.

START-OF-SELECTION.

SELECT matnr werks lgort


UP TO 25 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.

IF sy-subrc = 0.
SORT itab BY werks.
WRITE: / 'Material', 20 'Plant', 27 'Storage Location'.
ULINE.

LOOP AT itab INTO wtab.


WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.

AT FIRST.
WRITE: '===AT FIRST will trigger here'.
ENDAT.
AT LAST.
WRITE: '===AT LAST will trigger here'.
ENDAT.
ENDLOOP.
ENDIF.

Now at the debugger level we see the following results. AT FIRST triggers at 1st loop iteration when SY-
TABIX = 1. Here all fields of work area are ***.

After that the system triggers AT LAST statement at the last loop iteration when SY-TABIX = 25.
Previously the values of work area are also ***.

Control Break - ON CHANGE OF

ON CHANGE OF triggers when there is any change of the first occurrence of mentioned fields’ value. It
means it actually works like AT NEW statement. If there is any change of the value of the mentioned field
then ON CHANGE OF will trigger. At the time of triggering all of other fields contain their respective data.
Hence it doesn’t go for *** value at the time of triggering. ON CHANGE OF can be used outside the loop
also.
REPORT zctrlbrk_on_change NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab,


werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.

DATA: wtab TYPE ty_tab,


itab TYPE TABLE OF ty_tab.

START-OF-SELECTION.

SELECT matnr werks lgort


UP TO 25 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.

IF sy-subrc = 0.
SORT itab BY werks.
WRITE: / 'Material', 20 'Plant', 27 'Storage Location'.
ULINE.

LOOP AT itab INTO wtab.


WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.

ON CHANGE OF wtab-werks.
WRITE: '=== On Change Of triggers at plant - ', wtab-werks.
ENDON.
ENDLOOP.
ENDIF.

Now at debugging level we can see as follows. At first loop iteration system will definitely triggers the ON
CHANGE OF statement because the plant data is new. All other fields contain their respective data at the
time of triggering.
Control Break with SUM statement

SUM statement can only be used in LOOP – ENDLOOP statement. It is considered in AT – END AT
control break structure. Inside the control break SUM calculates the sum of all the fields which are like I or
P or F type. SUM holds the summation at it is reflected to the respective work areas. This summation
must be the total of current control break. SUM doesn’t work when the row type of the internal table
contains table type components.

Here we have prepared an example in which purchase order item table EKPO has been selected. Now
we use SUM statement which calculates the summation of quantity & net price inside the AT END OF
control statement. Hence at the last occurrence of PO the system calculates the total quantity & price for
different item level.

REPORT z_sum NO STANDARD PAGE HEADING.

*-------Declaring tables for select option-----------------------------*


TABLES: ekpo.

*------Declaring local structure for work area & table-----------------*


TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
END OF ty_ekpo.

*-----Declaration of work area & internal table------------------------*


DATA: wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE TABLE OF ty_ekpo,
v_flag TYPE c.

*-----Event Initialization---------------------------------------------*
INITIALIZATION.
SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.

*-----Event Start of Selection-----------------------------------------*


START-OF-SELECTION.

SELECT ebeln ebelp menge meins netpr


FROM ekpo INTO TABLE it_ekpo
WHERE ebeln IN s_ebeln.

IF sy-subrc = 0.

"Table needs to be sorted.


"Otherwise AT NEW & AT END OF will operate data wrongly
SORT it_ekpo.
LOOP AT it_ekpo INTO wa_ekpo.

"Triggers at the first loop iteration only


AT FIRST.
WRITE: 'Purchase Order' COLOR 3,
20 'Item' COLOR 3,
35 'Quantity' COLOR 3,
45 'Unit' COLOR 3,
54 'Net Price' COLOR 3.
ULINE.
ENDAT.

"Triggers when new PO will come into the loop


AT NEW ebeln.
v_flag = 'X'.
ENDAT.

IF v_flag = 'X'.
WRITE: / wa_ekpo-ebeln,
20 wa_ekpo-ebelp,
27 wa_ekpo-menge,
45 wa_ekpo-meins,
50 wa_ekpo-netpr.
ELSE.
WRITE: /20 wa_ekpo-ebelp,
27 wa_ekpo-menge,
45 wa_ekpo-meins,
50 wa_ekpo-netpr.
ENDIF.

"Triggers at the last occurrence of PO in the loop


AT END OF ebeln.
WRITE: /27 '=================',
50 '=============='.
SUM. "SUM adds & holds all the I/P/F data
"Here it holds for this control range

WRITE: / 'Sub Total: ' COLOR 5,


27 wa_ekpo-menge,
50 wa_ekpo-netpr.
SKIP.
ENDAT.

"Triggers at the last loop iteration only


AT LAST.
ULINE.
SUM. "SUM adds & holds all the I/P/F data
"Here it holds the total of loop range

WRITE: / 'Quantity & Net Price' COLOR 4,


/ 'Grand Total: ' COLOR 4,
27 wa_ekpo-menge,
50 wa_ekpo-netpr.
ENDAT.
CLEAR: wa_ekpo, v_flag.
ENDLOOP.
ENDIF.
Select Single and Select up to
Select single statement only selects the first record of any series of records from a database table. That
means this statement can read a single record from a database table. It keeps the data into a work area
or header line. This work area is generally a line type of internal table.

In below example we are fetching records of PO no, item no, material, plant & storage location from table
EKPO where the PO no is 3000000232. The database contains only 3 items for this PO.

REPORT zabap_gui.

TABLES: ekpo.
* Creating a custom structure of Item Table
TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo.

* Select single will fetch only the First record


* from the PO Item table
SELECT SINGLE ebeln ebelp matnr werks lgort
FROM ekpo INTO wa_ekpo
WHERE ebeln = '3000000232'.

WRITE:/ 'PO No.',


15 'Item No',
28 'Material',
48 'Plant',
55 'Storage'.
ULINE.
SKIP.

WRITE:/ wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
28 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort.

Output of this:

The database contains 3 records here. But select single statement fetches only single record from
database. Here the first record is always fetched by this statement.
Select up to statement is used to mention the rows need to be selected from the database table. If the
database contains that number of rows then it will display accordingly. Here we are declaring an internal
table to store the multiple row records and print the output as well.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

* Select up to n rows fetches n rows


* from the PO Item table
SELECT ebeln ebelp matnr werks lgort
UP TO 5 ROWS
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = '3000000232'.

WRITE:/ 'PO No.',


15 'Item No',
28 'Material',
48 'Plant',
55 'Storage'.
ULINE.
SKIP.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
28 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort.
ENDLOOP.

Select Distinct
Select distinct only selects the unique entries of the fields in the select statement. It will not allow any
duplicate entry into the internal table. In the below example we are having a selection screen where we
are defining a selection range of PO number by select option. At first we are fetching the records with
normal select statement and we find six records from the database.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.

SELECT ebeln ebelp matnr werks lgort


FROM ekpo INTO TABLE it_ekpo
WHERE ebeln IN s_ebeln.

WRITE:/ 'PO No.',


15 'Item No',
28 'Material',
48 'Plant',
55 'Storage'.
ULINE.
SKIP.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
28 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort.
ENDLOOP.

Selection Range with select option:


The output is:

Now with the similar selection range we use select distinct statement and we are getting only three
records. This is because we have selected only the PO number in select statement with distinct clause.
Now distinct will not allow any duplicate entry of PO number.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.

SELECT DISTINCT ebeln


FROM ekpo INTO TABLE it_ekpo
WHERE ebeln IN s_ebeln.

WRITE:/ 'PO No.'.


ULINE.
SKIP.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln.
ENDLOOP.

Hence the output is as follows.

Here we know that one PO can have multiple items. Hence in database table EKPO the PO entries are
having duplicate entries for different items. But selecting the PO number with distinct clause will fetch only
the unique PO number from the database. If we select here the item also with the distinct clause the SAP
system will treat both of those fields as unique. In that case the system will recognize PO number and
corresponding item number is the unique. In this way if we increase the fields in selection the system will
give uniqueness according to the combination of all those selected fields.

Select Total Field Records


How to select total records from one or more than one fields from database?
We can do that by not using the WHERE condition as simple as that. Below is an example where we
have selected PO numbers & items from table EKPO and we haven’t used WHERE condition. The
system fetches all records from those two fields and prints the output.

To know the total numbers of rows we have used describe table statement so that we can see the total
number records.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

* Not using Where condition will fetch all rows from the fields
SELECT ebeln ebelp
FROM ekpo INTO TABLE it_ekpo.

DESCRIBE TABLE it_ekpo.


WRITE:/ 'Total Entries: ', sy-tfill.
SKIP.

WRITE:/ 'PO No.', 15 'Item'.


ULINE.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln, 15 wa_ekpo-ebelp.
ENDLOOP.

Here is the output.


Select with Appending
We can directly append records into an internal table with select statement by using APPENDING clause.
Syntax is as follows.
SELECT db_field1, db_field2,…
FROM db_table APPENDING TABLE internal_table
WHERE db_field = condition.

Where is optional. By using APPENDING clause we can re-select and fetch another sort of records into
the same internal table. Here the system appends the records to the last position of the internal table.
After appending these records when we write the data then it will come one by one (not in the same row).

In below example at first we have selected PO number. So the system will append the PO numbers only.
After that in the second select the system will select the materials and append those to the last position of
the internal table and so on.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

REFRESH it_ekpo.

SELECT ebeln
FROM ekpo APPENDING TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT matnr
FROM ekpo APPENDING TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT werks
FROM ekpo APPENDING TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT lgort
FROM ekpo APPENDING TABLE it_ekpo
WHERE ebeln = '3000000232'.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln,
wa_ekpo-matnr,
wa_ekpo-werks,
wa_ekpo-lgort.
ENDLOOP.

One by one record will come for same row information.

We can use CORRESPONDING FIELDS OF statement also.


SELECT db_field1, db_field2,…
FROM db_table
APPENDING CORRESPONDING FIELDS OF TABLE internal_table
WHERE db_field = condition.

In this case the output will come with the corresponding fields. The system will put the respective data on
the respective fields of the output screen. But the records will come one by one (different rows) rather the
same row.

REPORT zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table


TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

* Creating a line type of predefined structure


DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

REFRESH it_ekpo.

SELECT ebeln
FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT matnr
FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT werks
FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo
WHERE ebeln = '3000000232'.

SELECT lgort
FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo
WHERE ebeln = '3000000232'.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/ wa_ekpo-ebeln,
wa_ekpo-matnr,
wa_ekpo-werks,
wa_ekpo-lgort.
ENDLOOP.

The output is like this.

Average Sum Maximum Minimum by Select


We can calculate the average and sum of any quantity type field directly from select statement. Similarly
we can extract the maximum value or minimum value from quantity type field. In the below example the
system fetches the data of MENGE field from EKPO table and calculate its average and sum and then
put into the packed type variable ‘average’ & ‘sum’ respectively. Similarly it fetches the maximum and
minimum values from MENGE and put it packed type variable ‘maximum’ & ‘minimum’. Here the WHERE
clause is optional. To avoid the entire field records we have used this clause.

In the database we find this MENGE field with PO number '3000000057'.

REPORT zabap_gui.

DATA:
average TYPE p DECIMALS 2,
sum TYPE p DECIMALS 2,
maximum TYPE p DECIMALS 2,
minimum TYPE p DECIMALS 2.

* Here the MENGE field of EKPO table has been used


* to calculate the average, sum, maximum & minimum
SELECT AVG( menge )
SUM( menge )
MAX( menge )
MIN( menge )
FROM ekpo
INTO (average, sum, maximum, minimum)
WHERE ebeln = '3000000057'.

WRITE: / 'Average = ', average,


/ 'Sum = ', sum,
/ 'Maximum = ', maximum,
/ 'Minimum = ', minimum.

Here is the output.


Classical Report of Single Table
The following program is displaying some useful information of material from table MARA
based on the material number by a select option in the selection screen.

Here we have created a custom structure of the internal table named it_mara. This structure
ty_mara contains Material No, Creation Date, Name, Material Type and Group. Hence output
will have only these fields. We have declared an work area wa_mara for internal table. Here
the internal table is of standard table type.

Now under INITIALIZATION event we have initialized the Program name, date and user name
which are to be displayed at the Top of page. TOP OF PAGE is another event where we are
declaring the select option s_matnr in a selection screen for input of material no.

Next we are declaring the START-OF-SELECTION event under which we mention a subroutine
get_mara. Subroutine is declared by the key word perform. Under this perform we select
required fields from MARA into internal table it_mara based on the where condition. So after
getting proper data from the MARA table we shall prepare the output in the subroutine of
get_output.

Now on the next event END-OF-SELECTION we are declaring a subroutine get_output and
inside there we are looping the internal table it_mara into wa_mara. Hence we fetch the data
from internal table to the work area and then display it with simple write statement. Since
this is a loop, so one by one record will be fetched into work area and then it will be displayed.

Now we want to display the name of the fields heading at the beginning. To do this we call
control break statement at first & endat inside the loop. At first statement will be triggered at
the first time of the loop. Similarly when the listing will be completed then At Last - endat
statement will be triggered to display ending of report message.

Next we raise the event TOP-OF-PAGE which is used to display the top message. On the top
we can write program name, user name, date etc.

REPORT zabap_gui.

*Declaring the line type of database table


TABLES: mara.

*------Declaring the local types for Internal table & Work area--------*
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr, "Material No.
ersda TYPE mara-ersda, "Creation Data
ernam TYPE mara-ernam, "Created By
mtart TYPE mara-mtart, "Material Type
matkl TYPE mara-matkl, "Material Group
END OF ty_mara.

*-----Declaring the Internal table & work area-------------------------*


DATA: wa_mara TYPE ty_mara,
it_mara TYPE STANDARD TABLE OF ty_mara,
v_repid TYPE sy-repid, "Program name
v_date TYPE sy-datum, "Current date
v_user TYPE sy-uname. "User name

*-------------Event initialization-------------------------------------*
INITIALIZATION.
v_repid = sy-repid.
v_date = sy-datum.
v_user = sy-uname.

*-Declaring the selection screen & select option for input-------------*


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

*-----Event start of selection-----------------------------------------*


START-OF-SELECTION.
PERFORM get_mara.

*---Event end of selection---------------------------------------------*


END-OF-SELECTION.
PERFORM get_output.

*---Event top of page--------------------------------------------------*


TOP-OF-PAGE.
PERFORM top_page.
*&---------------------------------------------------------------------*
*& Form get_mara
*&---------------------------------------------------------------------*
* Select data from MARA table
*----------------------------------------------------------------------*
FORM get_mara .

SELECT matnr ersda ernam mtart matkl


FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.

IF sy-subrc <> 0.
MESSAGE 'Material Doesn''t Exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM. " get_mara


*&---------------------------------------------------------------------*
*& Form get_output
*&---------------------------------------------------------------------*
* Preparing the classical output with WRITE statement
*----------------------------------------------------------------------*
FORM get_output .

IF it_mara IS NOT INITIAL.


LOOP AT it_mara INTO wa_mara.

"Control break statement – it will display one time at first line


AT FIRST.
WRITE: /3 'Material No.',
25 'Created By',
40 'Group',
55 'Type',
70 'Creation Date'.
ULINE.
SKIP.
ENDAT.

WRITE: / wa_mara-matnr,
25 wa_mara-ernam,
40 wa_mara-matkl,
55 wa_mara-mtart,
70 wa_mara-ersda.

"Control break statement – it will display one time at last line


AT LAST.
ULINE.
WRITE: /15 '~~End of Material Display~~'.
ENDAT.

ENDLOOP.
ENDIF.

ENDFORM. " get_output


*&---------------------------------------------------------------------*
*& Form top_page
*&---------------------------------------------------------------------*
* Top pf page to display top information
*----------------------------------------------------------------------*
FORM top_page .

WRITE: / 'Material Details',


/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.

ENDFORM. " top_page

Below is the output:


Classical Report of Multiple Tables
After creating a classic report for single table it's time to create a report which will contain
multiple tables. Here we shall fetch various records from various tables and then collate them
to reach required output.

We have created a report which contains multiple tables like MARA, MARC and MARD. The
materials contain different Plant and Storage Location in MARC and MARD tables respectively.
All those different plant and storage location will be displayed with material number in output
here.

Here the plant is a big item and the storage location is small item. One single plant contains
different storage locations. Materials are stored inside one of these storage locations. Now
different materials can be stored in the same storage location.

In the program we have prepared internal table it_mara from MARA table based on the Select
Option material number range. If material number is entered then only the program will give
an output.

After preparing valid information of it_mara the program selects data from MARC (plant) table
into it_marc for all entries in it_mara. Here we have to check the it_mara table if it is not
initial. If we don't give this checking then all records will be selected from MARC table and
that would be wrong. Hence we can say that this table checking is one of a prerequisites of
For All Entries.

Similarly after preparing the it_marc table we shall prepare the it_mard table from MARD
(storage location) for all entries in it_marc. Similarly the table checking of it_marc must be
there.

Now we have material (it_mara), plant (it_marc) & storage location (it_mard) information.
We have to collate all these information to display the required output. To do this we have
prepared an internal table it_out with an work area wa_out. This internal table & work area
contains the structure ty_out as per the report output requirement.

Now we are looping it_mara to fetch material & type into the output work area. Here MARA
is a master table hence we have to loop this table. Now one material can be maintained for
different storage locations under one plant or different plant. Hence to populate output table
we have to loop into it_marc and it_mard table. Here we are looping it_marc and it_mard
with WHERE clause because we are going to fetch all records of plant and storage location at
one time. So where clause will help us to point out the particular material and also will increase
the performance.

Finally at the output population we have used control break statement like AT FIRST, AT END
OF, AT LAST. With the help of that we have synchronized the output in different lines.

Following is the coding of the classical report.

REPORT zabap_gui.

*-------Declaring the line type of database tables---------------------*


TABLES: mara, marc, mard.
*------Declaring the types of work areas & internal tables-------------*
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
END OF ty_mara,

BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
xchar TYPE marc-xchar,
END OF ty_marc,

BEGIN OF ty_mard,
matnr TYPE mard-matnr,
werks TYPE mard-werks,
lgort TYPE mard-lgort,
pstat TYPE mard-pstat,
END OF ty_mard,

BEGIN OF ty_out,
matnr TYPE marc-matnr, "Material
werks TYPE marc-werks, "Plant
lgort TYPE mard-lgort, "Storage Location
mtart TYPE mara-mtart, "Material Type
xchar TYPE marc-xchar, "Batch number
pstat TYPE mard-pstat, "Maintenance Status
END OF ty_out.

*-----Declaring work areas & internal tables---------------------------*


DATA: wa_mara TYPE ty_mara,
wa_marc TYPE ty_marc,
wa_mard TYPE ty_mard,
wa_out TYPE ty_out,
it_mara TYPE STANDARD TABLE OF ty_mara,
it_marc TYPE STANDARD TABLE OF ty_marc,
it_mard TYPE STANDARD TABLE OF ty_mard,
it_out TYPE STANDARD TABLE OF ty_out,

v_prog TYPE sy-repid, "Program name


v_date TYPE sy-datum, "Current date
v_time TYPE sy-uzeit. "Current time

*----------Declaring constants to avoid the hard codes-----------------*


CONSTANTS: c_material TYPE char12 VALUE 'MATERIAL NO',
c_plant TYPE char5 VALUE 'PLANT',
c_storage TYPE char8 VALUE 'STORAGE',
c_type TYPE char6 VALUE 'M TYPE',
c_batch TYPE char6 VALUE 'BATCH',
c_maint TYPE char18 VALUE 'MAINTENANCE STATUS',
c_end TYPE char40 VALUE 'End of Material Details'.

*------Event initialization--------------------------------------------*
INITIALIZATION.
v_prog = sy-repid.
v_date = sy-datum.
v_time = sy-uzeit.
*-----------Declaring selection screen with select option for input----*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
SELECTION-SCREEN END OF BLOCK b1.

*-----Event start of selection-----------------------------------------*


START-OF-SELECTION.
PERFORM get_mara.
PERFORM get_marc.
PERFORM get_mard.

*---Event end of selection---------------------------------------------*


END-OF-SELECTION.
PERFORM get_output.
PERFORM display.

*---Event top of page--------------------------------------------------*


TOP-OF-PAGE.
PERFORM top_of_page.

*&---------------------------------------------------------------------*
*& Form GET_MARA
*&---------------------------------------------------------------------*
* Select data from MARA table
*----------------------------------------------------------------------*
FORM get_mara .

IF s_matnr IS NOT INITIAL.


SELECT matnr mtart
FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.

IF sy-subrc = 0.
SORT it_mara BY matnr.
ELSE.
MESSAGE 'Material doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.

ENDFORM. " GET_MARA


*&---------------------------------------------------------------------*
*& Form GET_MARC
*&---------------------------------------------------------------------*
* Select data from MARC table
*----------------------------------------------------------------------*
FORM get_marc .

IF it_mara IS NOT INITIAL. "Prerequisite of FOR ALL ENTRIES IN


SELECT matnr werks xchar
FROM marc INTO TABLE it_marc
FOR ALL ENTRIES IN it_mara
WHERE matnr = it_mara-matnr.

IF sy-subrc = 0.
SORT it_marc BY matnr.
ELSE.
MESSAGE 'Plant doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.

ENDFORM. " GET_MARC


*&---------------------------------------------------------------------*
*& Form GET_MARD
*&---------------------------------------------------------------------*
* Select data from MARD table
*----------------------------------------------------------------------*
FORM get_mard .

IF it_marc IS NOT INITIAL. "Prerequisite of FOR ALL ENTRIES IN


SELECT matnr werks lgort pstat
FROM mard INTO TABLE it_mard
FOR ALL ENTRIES IN it_marc
WHERE matnr = it_marc-matnr
AND werks = it_marc-werks.

IF sy-subrc = 0.
SORT it_mard BY matnr.
ELSE.
MESSAGE 'Storage Location doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.

ENDFORM. " GET_MARD


*&---------------------------------------------------------------------*
*& Form GET_OUTPUT
*&---------------------------------------------------------------------*
* Preparing the output table by using Loop
*----------------------------------------------------------------------*
FORM get_output .

IF it_mara IS NOT INITIAL.


LOOP AT it_mara INTO wa_mara.
wa_out-matnr = wa_mara-matnr.
wa_out-mtart = wa_mara-mtart.

LOOP AT it_marc INTO wa_marc


WHERE matnr = wa_mara-matnr.
wa_out-werks = wa_marc-werks.
wa_out-xchar = wa_marc-xchar.

LOOP AT it_mard INTO wa_mard


WHERE matnr = wa_marc-matnr
AND werks = wa_marc-werks.
wa_out-lgort = wa_mard-lgort.
wa_out-pstat = wa_mard-pstat.

APPEND wa_out TO it_out.


CLEAR: wa_out, wa_mara, wa_marc, wa_mard.
ENDLOOP.
ENDLOOP.
ENDLOOP.
ENDIF.

ENDFORM. " GET_OUTPUT


*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
* Displaying the classical output by using WRITE statement
*----------------------------------------------------------------------*
FORM display .

IF it_out IS NOT INITIAL.


LOOP AT it_out INTO wa_out.

AT FIRST. "Control break statement – display one time at first


WRITE: / c_material,
21 c_plant,
27 c_storage,
37 c_type,
45 c_batch,
54 c_maint.
ULINE.
SKIP.
ENDAT.

WRITE: / wa_out-matnr,
21 wa_out-werks,
27 wa_out-lgort,
37 wa_out-mtart,
45 wa_out-xchar,
54 wa_out-pstat.

IF wa_out-matnr IS INITIAL.
AT END OF matnr. "Control break statement
SKIP.
ENDAT.
ENDIF.

AT LAST. "Control break statement – display one time at last


ULINE.
WRITE: / c_end.
ENDAT.
ENDLOOP.
ENDIF.

ENDFORM. " DISPLAY


*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
* Top of page of Classical output
*----------------------------------------------------------------------*
FORM top_of_page .

WRITE: / v_prog,
/ v_date DD/MM/YYYY,
/ v_time.
ULINE.

ENDFORM. " TOP_OF_PAGE

Output:
1. Selection Screen -

2. Classical Display -
Classical Interactive Report
Interactive program generally interacts with user on the output screen. Let us suppose we
have an output for a particular program/report. Now if we want details description by double
clicking on a particular field or clicking on a button then it will be called interaction with
output. By doing this we can go to the detailed report or secondary output. When we are on
the secondary output then we can back to our first out by clicking on Back button. We can
generate 20 secondary list of output. Hence a total of 21 output screen (1 Primary
+ 20 Secondary) can be generated by Interactive program.

We have a requirement where we have to prepare a primary output containing Purchase


Order, Company Code, Category, Type and Vendor with PO Info. The secondary list will be
generated with PO Item, Material, Plant, Storage Location, Material Group, Quantity and Unit
of measure. The secondary list will be generated if we double click on PO field in primary
output.

We have mentioned the tables EKKO, EKPO and T161T. Now we have declared the structure
types of internal tables of it_ekko, it_text, it_out1 and it_ekpo. Here it_out1 contains the
combination of two internal tables it_ekko and it_text and it_ekpo is for the secondary output.
For internal operation we have declared work area of every internal tables.

Next we have mentioned the event INITIALIZATION. Under this we are calling program name,
user name and system date of the program. We have declared here selection screen begin
with block b1 and under this we are mentioning obligatory select option.

Now under START-OF-SELECTION we are declaring all the operations for primary listing. We
have made 4 subroutines. In get_ekko we select fields from EKKO into internal table it_ekko.
Then in get_t161t we select fields from t161t into internal table it_text for all entries in
it_ekko. After that we are preparing the primary output in the basic_output subroutine. Now
we have made another subroutine for displaying the primary output and that is disp_basic.

Hence the primary list is prepared now and now we have to create the secondary list. To
make the secondary list we are calling event AT LINE-SELECTION which raises functionality
when user double clicks in any field. Now we are mentioning GET CURSOR for that field and
for that value of the field. When these two are matches with Purchase Order (EBELN) field
then we are calling two subroutines for secondary list.

Subroutine get_ekpo Selects fields from EKPO into table it_ekpo for all entries in it_ekko.
Then it prepares the secondary output display in the subroutine of ekpo_output.

We also have raised events TOP-OF-PAGE which shows the header list of primary output and
TOP-OF-PAGE DURING LINE-SELECTION which shows the top list of secondary output. Here
TOP-OF-PAGE DURING LINE-SELECTION is used only for interactive reports. That means when
user double clicks on the field the TOP-OF-PAGE DURING LINE-SELECTION event triggers to
make the secondary output header.

REPORT zabap_gui.

* Declaring line structure of database tables


TABLES: ekko, ekpo, t161t.

* Declaring local structures for internal table & work area


TYPES:
BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln, "Purchase Order
bukrs TYPE ekko-bukrs, "Company Code
bstyp TYPE ekko-bstyp, "Category
bsart TYPE ekko-bsart, "Type
lifnr TYPE ekko-lifnr, "Vendor
END OF ty_ekko,

BEGIN OF ty_text,
spras TYPE t161t-spras,
bsart TYPE t161t-bsart,
bstyp TYPE t161t-bstyp,
batxt TYPE t161t-batxt, "PO Info
END OF ty_text,

BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln, "Purchase Order
ebelp TYPE ekpo-ebelp, "PO Item
matnr TYPE ekpo-matnr, "Material
werks TYPE ekpo-werks, "Plant
lgort TYPE ekpo-lgort, "Storage Location
matkl TYPE ekpo-matkl, "Material Group
menge TYPE ekpo-menge, "Quantity
meins TYPE ekpo-meins, "Unit
END OF ty_ekpo,

BEGIN OF ty_out1,
ebeln TYPE ekko-ebeln,
bukrs TYPE ekko-bukrs,
bstyp TYPE ekko-bstyp,
bsart TYPE ekko-bsart,
lifnr TYPE ekko-lifnr,
batxt TYPE t161t-batxt,
END OF ty_out1.

* Declaring work area & internal table


DATA:
wa_ekko TYPE ty_ekko, "Header table work area
it_ekko TYPE STANDARD TABLE OF ty_ekko, "Header internal table
wa_text TYPE ty_text, "Info table work area
it_text TYPE STANDARD TABLE OF ty_text, "Info internal table
wa_out1 TYPE ty_out1, "Basic output work area
it_out1 TYPE STANDARD TABLE OF ty_out1, "Basic output internal table
wa_ekpo TYPE ty_ekpo, "Item table work area
it_ekpo TYPE STANDARD TABLE OF ty_ekpo, "Item internal table

v_repid TYPE sy-repid,


v_user TYPE sy-uname,
v_date TYPE sy-datum,

v_field1 TYPE char40,


v_field2 TYPE char40,
v_value1 TYPE char40,
v_value2 TYPE char40.

* Event Initialization
INITIALIZATION.
v_repid = sy-repid. "Program Name
v_user = sy-uname. "User name
v_date = sy-datum. "Current Date

* Declaring selection screen


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

* Event Start of Selection


START-OF-SELECTION.
PERFORM get_ekko. "Get data from header table
PERFORM get_t161t. "Get data from Info table
PERFORM basic_output. "Preparing the primary output
PERFORM disp_basic. "Displaying output of first list

* Event At line selection for Double click operation


AT LINE-SELECTION.

* It passes the field and value to the current cursor position


* When double click is happened on the PO field of Primary list
GET CURSOR FIELD v_field1 VALUE v_value1.

CASE v_field1.
* When we double click on PO number on Basic output list
WHEN 'WA_OUT1-EBELN'.
PERFORM get_ekpo. "Get data from Item table
PERFORM ekpo_output. "Displaying output of second list
ENDCASE.

* It passes the field and value to the current cursor position


* When double click is happened on the Material field of Secondary list
GET CURSOR FIELD v_field2 VALUE v_value2.

CASE v_field2.
* When we double click on Material on Second list
WHEN 'WA_EKPO-MATNR'.
PERFORM get_mara. "Get Information by calling MM03 Transaction
ENDCASE.

* Event top of page for Basic list / Primary list


TOP-OF-PAGE.
PERFORM top_page1.

* Event top of page for Second list


TOP-OF-PAGE DURING LINE-SELECTION.
PERFORM top_page2.

*&---------------------------------------------------------------------*
*& Form get_ekko
*&---------------------------------------------------------------------*
* Get data from header table
*----------------------------------------------------------------------*
FORM get_ekko .

* Selection of header table data


SELECT ebeln bukrs bstyp bsart lifnr
FROM ekko INTO TABLE it_ekko
WHERE ebeln IN s_ebeln.

IF sy-subrc = 0.
SORT it_ekko BY ebeln.
ELSE.
MESSAGE 'Purchase Order doesn''t exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM. " get_ekko


*&---------------------------------------------------------------------*
*& Form get_t161t
*&---------------------------------------------------------------------*
* Get data from Info table
*----------------------------------------------------------------------*
FORM get_t161t .

* Seelction of Info table data


IF it_ekko IS NOT INITIAL. "Prerequisite of For all Entries
SELECT spras bsart bstyp batxt
FROM t161t INTO TABLE it_text
FOR ALL ENTRIES IN it_ekko
WHERE spras = sy-langu "System language at login time
AND bsart = it_ekko-bsart
AND bstyp = it_ekko-bstyp.

IF sy-subrc = 0.
SORT it_text BY bsart bstyp.
ENDIF.
ENDIF.

ENDFORM. " get_t161t


*&---------------------------------------------------------------------*
*& Form basic_output
*&---------------------------------------------------------------------*
* Preparing the primary output
*----------------------------------------------------------------------*
FORM basic_output .

* Preparing the basic output table


IF it_ekko IS NOT INITIAL.
LOOP AT it_ekko INTO wa_ekko.
wa_out1-ebeln = wa_ekko-ebeln.
wa_out1-bukrs = wa_ekko-bukrs.
wa_out1-bstyp = wa_ekko-bstyp.
wa_out1-bsart = wa_ekko-bsart.
wa_out1-lifnr = wa_ekko-lifnr.

READ TABLE it_text INTO wa_text


WITH KEY bsart = wa_ekko-bsart
bstyp = wa_ekko-bstyp BINARY SEARCH.
IF sy-subrc = 0.
wa_out1-batxt = wa_text-batxt.
ENDIF.

APPEND wa_out1 TO it_out1.


CLEAR: wa_out1, wa_ekko, wa_text.
ENDLOOP.
ENDIF.

ENDFORM. " basic_output


*&---------------------------------------------------------------------*
*& Form disp_basic
*&---------------------------------------------------------------------*
* Displaying output of first list
*----------------------------------------------------------------------*
FORM disp_basic .

IF it_out1 IS NOT INITIAL.


LOOP AT it_out1 INTO wa_out1.
AT FIRST. "Control Break Statement - triggers at first
WRITE: / 'Purchase Order',
20 'Company',
30 'Category',
40 'Type',
50 'Vendor',
65 'PO Info.'.
ULINE.
SKIP.
ENDAT.

WRITE: / wa_out1-ebeln,
20 wa_out1-bukrs,
33 wa_out1-bstyp,
40 wa_out1-bsart,
50 wa_out1-lifnr,
65 wa_out1-batxt.

AT LAST. "Control Break Statement - triggers at last


SKIP.
ULINE.
WRITE: /12 '~~End of Report~~'.
ENDAT.
ENDLOOP.
ENDIF.

ENDFORM. " disp_basic


*&---------------------------------------------------------------------*
*& Form get_ekpo
*&---------------------------------------------------------------------*
* Get data from Item table
*----------------------------------------------------------------------*
FORM get_ekpo .

* Local temporary variable for conversion


DATA: lv_ebeln TYPE ekko-ebeln.

IF v_value1 IS NOT INITIAL.

* To convert the value from output format to input format


* and passing it from input format to output format
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = v_value1
IMPORTING
output = lv_ebeln.

IF lv_ebeln IS NOT INITIAL.

* Selection of Item table


SELECT ebeln ebelp matnr werks lgort
matkl menge meins
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = lv_ebeln.

IF sy-subrc <> 0.
MESSAGE 'PO Item doesn''t Exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ENDIF.

ENDFORM. " get_ekpo


*&---------------------------------------------------------------------*
*& Form ekpo_output
*&---------------------------------------------------------------------*
* Displaying output of second list
*----------------------------------------------------------------------*
FORM ekpo_output .

* Preparing secondary output


IF it_ekpo IS NOT INITIAL.
LOOP AT it_ekpo INTO wa_ekpo.

AT FIRST.
WRITE: / 'Purchase Order',
20 'PO Item',
30 'Material',
48 'Plant',
55 'Storage',
65 'Material Group',
83 'PO Quantity',
100 'Unit'.
ULINE.
SKIP.
ENDAT.

WRITE: / wa_ekpo-ebeln,
20 wa_ekpo-ebelp,
30 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort,
70 wa_ekpo-matkl,
75 wa_ekpo-menge,
100 wa_ekpo-meins.

AT LAST.
SKIP.
ULINE.
WRITE: /12 '~~End of PO Item~~'.
ENDAT.
ENDLOOP.
ENDIF.

ENDFORM. " ekpo_output


*&---------------------------------------------------------------------*
*& Form get_mara
*&---------------------------------------------------------------------*
* Get Information by calling MM03 Transaction
*----------------------------------------------------------------------*
FORM get_mara .

* Local temporary variable for conversion


DATA: lv_matnr TYPE mara-matnr.

IF v_value2 IS NOT INITIAL.

* Converting material from output format to input format


* and passing it from input format to output format
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = v_value2
IMPORTING
output = lv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.

IF lv_matnr IS NOT INITIAL.

* Calling the MM03 transaction needs parameter ID


* which is available on domain of MATNR
SET PARAMETER ID 'MAT' FIELD lv_matnr.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.

ENDFORM. " get_mara


*&---------------------------------------------------------------------*
*& Form top_page1
*&---------------------------------------------------------------------*
* Event top of page for Basic list / Primary list
*----------------------------------------------------------------------*
FORM top_page1 .

WRITE: / 'Purchase Order Header',


/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.

ENDFORM. " top_page1


*&---------------------------------------------------------------------*
*& Form top_page2
*&---------------------------------------------------------------------*
* Event top of page for Second list
*----------------------------------------------------------------------*
FORM top_page2 .

WRITE: / 'Purchase Order Item List',


/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.

ENDFORM. " top_page2

Below is the output:

Primary Listing:
If we double click on the PO in this list then following will be generated:
Classical Interactive with Push Button
Interactive reports increase the visibility of user when the user interacts with the basic list to get detail
secondary lists. The basic list contains the brief information whereas the detail list contains the detail
information. As per the requirement the user interacts with the detailed list whenever it is needed. That is
the reason why it increases the visibility. Instead of an extensive and detailed list the user can actively
control data retrieval and display during the session through this interactive report. The user can interact
with the report with having the cursor position and entering the command.

There are several events for Interactive report:


1. At line selection (when user double clicks to a particular field value)
2. At user command (when user selects a particular field value and then clicks a push button)
3. Top of page during line selection (it displays different content at different secondary lists)

To interact by using a push button the R/3 system has an option which is menu painter. We can do this by
using the PF-STATUS. With the help of that we can create our customized menu where we can set push
button. After that we have to write functionality under the event at user command. The push button must
have a function code and by using that we can write our custom function. The system field SY-UCOMM is
used to set the different functions in different push buttons.

Here we have an example where we select Vendor account details based on the selection criteria by
select options. The report will have a basic list which will display the vendor details from LFA1 table. Then
by selecting a vendor account number if we click the push button Purchase Order then we shall go to the
first secondary list. This list is coming from the EKKO table. After that if we select any purchase order and
then click the PO item button then second secondary list will be coming. This list is coming from EKPO
table.

In this point of view we have used HIDE statement. HIDE actually hides the field value into the system to
reuse that at any time. With the help of this statement the system loads the field name, field content and
line number in which the line exists. The line number is stored in SY-LINNO field. Field symbols cannot be
specified in HIDE. This statement works independently. If we select the cursor to any blank field then also
the HIDE will store that value.

REPORT zabap_gui.

*-------Declaring database tables for using the line type--------------*


TABLES: lfa1, ekko, ekpo.

*------Declaring structures for work area & Internal table-------------*


TYPES: BEGIN OF ty_lfa1,
lifnr TYPE lfa1-lifnr,
land1 TYPE lfa1-land1,
name1 TYPE lfa1-name1,
regio TYPE lfa1-regio,
END OF ty_lfa1,

BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
bukrs TYPE ekko-bukrs,
aedat TYPE ekko-aedat,
ernam TYPE ekko-ernam,
lifnr TYPE ekko-lifnr,
END OF ty_ekko,

BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.

DATA:
*-----Declaring Work Areas---------------------------------------------*
wa_lfa1 TYPE ty_lfa1,
wa_ekko TYPE ty_ekko,
wa_ekpo TYPE ty_ekpo,

*-----Declaring Internal Tables----------------------------------------*


it_lfa1 TYPE TABLE OF ty_lfa1,
it_ekko TYPE TABLE OF ty_ekko,
it_ekpo TYPE TABLE OF ty_ekpo.

*---Event Initialization-----------------------------------------------*
INITIALIZATION.
SELECT-OPTIONS: s_lifnr FOR lfa1-lifnr.

*---Event Start of Selection-------------------------------------------*


START-OF-SELECTION.
PERFORM get_data_lfa1.
SET PF-STATUS 'PUSH_BUTTON'. "Setting PF status for Push Buttons

*---Event At User Command----------------------------------------------*


AT USER-COMMAND. "When user clicks any button

CASE sy-ucomm. "It contains the function code of any button


WHEN 'EKKO'. "Push button Purchase Order
PERFORM get_data_ekko.

WHEN 'EKPO'. "Push button Purchase Order Item


PERFORM get_data_ekpo.

WHEN 'EXIT'. "Push button Exit


LEAVE TO SCREEN 0.
WHEN 'CANCEL'. "Push button Cancel
LEAVE TO SCREEN 0.
ENDCASE.

*---Event Top of Page for Basic List-----------------------------------*


TOP-OF-PAGE.
PERFORM top_basic_lfa1.

*---Event Top of Page During Line Selection for Drilled List-----------*


TOP-OF-PAGE DURING LINE-SELECTION.
CASE sy-lsind. "It counts the drilled number
WHEN '1'.
PERFORM top_ekko.
WHEN '2'.
PERFORM top_ekpo.
ENDCASE.
*&---------------------------------------------------------------------*
*& Form get_data_lfa1
*&---------------------------------------------------------------------*
* Select data from Vendor table
*----------------------------------------------------------------------*
FORM get_data_lfa1 .

REFRESH it_lfa1.

IF s_lifnr[] IS NOT INITIAL.


SELECT lifnr land1 name1 regio
FROM lfa1 INTO TABLE it_lfa1
WHERE lifnr IN s_lifnr.

IF sy-subrc = 0.
LOOP AT it_lfa1 INTO wa_lfa1.
WRITE: / wa_lfa1-lifnr,
20 wa_lfa1-land1,
25 wa_lfa1-name1,
65 wa_lfa1-regio.

HIDE wa_lfa1-lifnr. "Vendor is keeping Hide to store the data


"into system temporarily
AT LAST.
SKIP 2.
WRITE: /25 'End of Vendor Account List'.
ENDAT.
ENDLOOP.

ELSE.
MESSAGE 'Vendor doesn''t exist' TYPE 'I'.
ENDIF.
ELSE.
MESSAGE 'Please enter a valid Vendor Account Number' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM. " get_data_lfa1


*&---------------------------------------------------------------------*
*& Form get_data_ekko
*&---------------------------------------------------------------------*
* Select data from Purchase Order Header table
*----------------------------------------------------------------------*
FORM get_data_ekko .

REFRESH it_ekko.

IF wa_lfa1-lifnr IS NOT INITIAL.


SELECT ebeln bukrs aedat ernam lifnr
FROM ekko INTO TABLE it_ekko
WHERE lifnr = wa_lfa1-lifnr.

IF sy-subrc = 0.
LOOP AT it_ekko INTO wa_ekko.
WRITE: / wa_ekko-ebeln,
15 wa_ekko-bukrs,
22 wa_ekko-aedat,
34 wa_ekko-ernam,
50 wa_ekko-lifnr.

HIDE wa_ekko-ebeln. "PO is keeping Hide to store the data


"into system temporarily
AT LAST.
SKIP 2.
WRITE: /20 'End of Purchase Order List'.
ENDAT.
ENDLOOP.
ELSE.
MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.

ENDFORM. " get_data_ekko


*&---------------------------------------------------------------------*
*& Form get_data_ekpo
*&---------------------------------------------------------------------*
* Select data from Purchase Order Item Table
*----------------------------------------------------------------------*
FORM get_data_ekpo .

REFRESH it_ekpo.

IF wa_ekko-ebeln IS NOT INITIAL.


SELECT ebeln ebelp matnr werks lgort
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = wa_ekko-ebeln.

IF sy-subrc = 0.
LOOP AT it_ekpo INTO wa_ekpo.
WRITE: / wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
22 wa_ekpo-matnr,
42 wa_ekpo-werks,
50 wa_ekpo-lgort.

AT LAST.
SKIP 2.
WRITE: /25 'End of Material List'.
ENDAT.
ENDLOOP.
ENDIF.
ELSE.
MESSAGE 'Select a Purchase Order' TYPE 'I'.
ENDIF.

ENDFORM. " get_data_ekpo


*&---------------------------------------------------------------------*
*& Form top_basic_lfa1
*&---------------------------------------------------------------------*
* Top of page of basic list - Vendor list
*----------------------------------------------------------------------*
FORM top_basic_lfa1 .

WRITE: / 'Vendor Account List' COLOR 3.


WRITE: / 'Vendor Account',
20 'Land',
25 'Name',
65 'Region'.
ENDFORM. " top_basic_lfa1
*&---------------------------------------------------------------------*
*& Form top_ekko
*&---------------------------------------------------------------------*
* Top of page for first secondary list - PO header list
*----------------------------------------------------------------------*
FORM top_ekko .

WRITE: / 'Purchase Order List' COLOR 5.


WRITE: / 'Po Number',
15 'Company',
22 'Date',
34 'Name',
50 'Vendor Account Number'.

ENDFORM. " top_ekko


*&---------------------------------------------------------------------*
*& Form top_ekpo
*&---------------------------------------------------------------------*
* Top of page for second secondary list - PO item list
*----------------------------------------------------------------------*
FORM top_ekpo .

WRITE: / 'Purchase Order List' COLOR 1.


WRITE: / 'PO Number',
15 'PO Item',
22 'Material',
42 'Plant',
50 'Storage'.

ENDFORM. " top_ekpo

You might also like