• No results found

Sap Abap Programming

N/A
N/A
Protected

Academic year: 2021

Share "Sap Abap Programming"

Copied!
641
0
0

Loading.... (view fulltext now)

Full text

(1)

Internal Table Basics

Internal table is a data object in ABAP that exists only at run time of a program. It means when the program execution is complete then the internal table will be lost. We use internal table to store database table data 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 & 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 table.

2. Columns are the fields of internal table. Those fields are of different data elements declared by locally or globally.

3. The most important part of an internal table is its work area. Work area is basically the line type of an internal table. It means it has the same structure of the rows of internal table. Work area contains the same fields of same type of the rows. It is of two types – implicit & explicit work area.

A. When we declare an internal table with header line then a work area is automatically created with the same name of the table. This work area is called implicit work area which is actually the header line. There is no need to declare 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 also.

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:

(2)

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.

(3)

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. When we use nested data objects (internal table has components of structure which is

another internal table) then header line is not allowed. 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.

(4)

Table kind Index Tables 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.

(5)

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.

(6)

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.

(7)

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.

(8)

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 '=========================================='.

(9)

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.

(10)

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 '=========================================='.

(11)

Hashed table can be sorted. If we sort this table it will be as follows.

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.

(12)

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 '=========================================='.

(13)

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)'.

(14)

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.

(15)

* 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

(16)

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.

(17)

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 '=========================================='.

(18)

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.

(19)

* 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.

(20)

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 TABLE itab 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

(21)

* 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.

(22)

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 onwards. 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 onwards. It will keep only the first record. Here if we use Sorted table with non-unique key then after inserting the records we can fins 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.

(23)

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 '=========================================='.

(24)

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,

(25)

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 '=========================================='.

(26)

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.

(27)

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.

(28)

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.

(29)

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

(30)

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.

(31)

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.

(32)

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.

(33)

At the last of the table record AT END OF triggers. Here the plant occurrence & table data both are of last entry.

(34)

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,

(35)

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 ***.

(36)

The output is as follows.

(37)

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.

(38)

Similarly when there is any change of plant then system triggers it again. In this case it is at SY-TABIX = 3.

(39)

Posted by SANDIP ROY at 11/26/2015 05:23:00 PM

Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

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.

(40)

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.

(41)

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.

Now at the debugging level we see the behavior of SUM statement. At SY-TABIX = 4 one PO holds its last occurrence and the system calculates the total quantity & price by using the SUM statement. Here the total will be considered for this particular control only.

We can use SUM in AT LAST control break as well. In this case it will calculate the all total of quantity & price. Since AT LAST triggers at the last loop iteration, the SUM considers the total quantity & price of all different POs.

(42)

Below is the output.

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.

(43)

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.

(44)

* 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.

(45)

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. Output of this:

(46)

Since there are only three records in database, the system shows only 3 records in spite of being mentioned “UP TO 5 ROWS”.

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',

(47)

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:

(48)

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.

(49)

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,

(50)

* 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.

(51)

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.

(52)

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 make a default tab for every record on the output screen. But the records will come one by one (different rows) rather the same row.

REPORT zabap_gui. TABLES: ekpo.

(53)

* 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.

(54)

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.

(55)

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 )

(56)

FROM ekpo

INTO (average, sum, maximum, minimum) WHERE ebeln = '3000000057'.

WRITE: / 'Average = ', average, / 'Sum = ', sum, / 'Maximum = ', maximum, / 'Minimum = ', minimum. Here is the output.

Client Specified Select

Client specified clause switches off the automatic client handling by open SQL. If we select the MANDT (client) field then we have to use client specified clause like follows:

SELECT mandt field1 field2 ... fieldn

INTO TABLE internal_table FROM database_table CLIENT SPECIFIED "MANDT has been selected

"hence client specified is must WHERE mandt = '800'

AND field1 IN select_option.

This statement is always mentioned after the FROM clause. If the addition CLIENT SPECIFIED is specified, but the client ID in the WHERE condition is not, the SELECT statement circumvents the SAP buffering.

REPORT zabap_gui. TABLES: kna1.

* Local structure for local internal table * and work area

TYPES:

BEGIN OF ty_kna1,

mandt TYPE kna1-mandt, kunnr TYPE kna1-kunnr, land1 TYPE kna1-land1, name1 TYPE kna1-name1,

(57)

ort01 TYPE kna1-ort01, pstlz TYPE kna1-pstlz, regio TYPE kna1-regio, END OF ty_kna1.

* Local internal table & work area DATA:

it_kna1 TYPE TABLE OF ty_kna1, wa_kna1 TYPE ty_kna1.

* Selection range by select option internal table SELECT-OPTIONS: s_kunnr FOR kna1-kunnr.

START-OF-SELECTION.

* Selection of the specific fields

SELECT mandt kunnr land1 name1 ort01 pstlz regio INTO TABLE it_kna1 FROM kna1

CLIENT SPECIFIED "MANDT has been selected

"hence client specified is must WHERE mandt = '800'

AND kunnr IN s_kunnr. IF sy-subrc = 0. WRITE:/ 'Clnt', 5 'Customer No', 14 'Country', 24 'Name', 60 'City', 100 'Postal', 112 'Region'. ULINE. SKIP.

LOOP AT it_kna1 INTO wa_kna1. WRITE:/ wa_kna1-mandt, 5 wa_kna1-kunnr, 14 wa_kna1-land1, 24 wa_kna1-name1, 60 wa_kna1-ort01, 100 wa_kna1-pstlz, 112 wa_kna1-regio. ENDLOOP. ENDIF.

(58)

Bypassing Buffer in Select

One of an important feature of open SQL is that it fetches the data records from the buffer of SAP system. Now fetching records directly from database may take time. Hence performance will go down. That’s why SQL fetches data from buffer.

Now if the database table changes frequently (table like transaction table) then it will be a problem to select updated data which will not be present in buffer. To avoid this problem SAP system has introduced the BYPASSING BUFFER clause in the select statement after from clause. This statement ensures that the records are updated data records fetched from the database.

REPORT zabap_gui. TABLES: kna1.

* Local structure for local internal table * and work area

TYPES:

BEGIN OF ty_kna1,

kunnr TYPE kna1-kunnr, land1 TYPE kna1-land1,

(59)

name1 TYPE kna1-name1, ort01 TYPE kna1-ort01, pstlz TYPE kna1-pstlz, regio TYPE kna1-regio, END OF ty_kna1.

* Local internal table & work area DATA:

it_kna1 TYPE TABLE OF ty_kna1, wa_kna1 TYPE ty_kna1.

* Selection range by select option internal table SELECT-OPTIONS: s_kunnr FOR kna1-kunnr.

START-OF-SELECTION.

* Selection of the specific fields

SELECT kunnr land1 name1 ort01 pstlz regio INTO TABLE it_kna1 FROM kna1

BYPASSING BUFFER "it ensures that the system fetches "data directly from the database "not from the buffer

WHERE kunnr IN s_kunnr. IF sy-subrc = 0. WRITE: /5 'Customer No', 14 'Country', 24 'Name', 60 'City', 100 'Postal', 112 'Region'. ULINE. SKIP.

LOOP AT it_kna1 INTO wa_kna1. WRITE: /5 wa_kna1-kunnr, 14 wa_kna1-land1, 24 wa_kna1-name1, 60 wa_kna1-ort01, 100 wa_kna1-pstlz, 112 wa_kna1-regio. ENDLOOP. ENDIF.

(60)

Select Dynamic Column

We can select the columns dynamically in a select statement. The syntax is like this: SELECT (local_internal_table)

FROM database_table INTO TABLE internal_table.

Here the local internal table contains the field names dynamically. This table also has a line type which holds the data of field names like this.

DATA: line TYPE char100,

itab TYPE TABLE OF line.

line = 'ebeln ebelp matnr werks lgort'. APPEND line TO itab.

Now after appending the text to the itab it can be used dynamically in select statement. Here the WHERE clause is optional. If we don’t use it then the total rows/records of the fields will have been fetched by the system.

(61)

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, * Creating a line type and internal table * to use as dynamic columns specification line TYPE char100,

itab TYPE TABLE OF line.

line = 'ebeln ebelp matnr werks lgort'. APPEND line TO itab.

SELECT (itab)

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.

(62)

Group By

Group By clause categorizes and summarizes the lines of database table based on a single field. That single field is mentioned in the group by clause. We can use multiple fields in the group by clause also. Here the database table records will be summarized according to those fields.

The fields mentioned in the group by clause cannot be specified for the aggregate function. We have to use other fields which are not specified in group by clause to calculate the aggregate function. Now we have the a PO with the following quantity.

(63)

REPORT zabap_gui. DATA:

ebeln TYPE ekpo-ebeln, po_max TYPE p DECIMALS 2, po_min TYPE p DECIMALS 2, tq_max TYPE p DECIMALS 2, tq_min TYPE p DECIMALS 2. WRITE:/ 'PO No',

15 'Max PO Quantity', 35 'Min PO Quantity', 55 'Max Target', 70 'Min Target'. SELECT ebeln

MAX( menge ) MIN( menge ) MAX( ktmng ) MIN( ktmng ) FROM ekpo INTO (ebeln, po_max, po_min, tq_max, tq_min) GROUP BY ebeln. WRITE:/ ebeln, 15 po_max, 35 po_min, 55 tq_max, 70 tq_min. ENDSELECT.

(64)

Using Cursor in ABAP

A cursor is a database object which is used to manipulate data in a set of row by row. We can say that a cursor is a set of rows with a pointer and this pointer actually points to the current row. Since cursor works row by row, it actually kills the performance. So it is better to go with another way with the help ABAP logic. In ABAP we use cursor with the following four processes.

Declare the Cursor:

 The cursor is declared by the DATA statement with keyword CURSOR. Open Cursor Statement:

 Open cursor opens a database cursor for a specific selection, defined after FOR.  It links the cursor variable (cr_spfli) to the database cursor.

 If the cursor variable is already opened then it cannot be reopened.  The statement takes the cursor position at the first row of the resulting set.

 The select statement declared after FOR doesn’t enter any record into any table or work area.  Select single statement cannot be used here.

 Only a limited number of database cursor can be open at the same time.  Open cursor actually initialize the cursor at the first position of database.

Fetch Next Cursor Statement:

 It extracts the requested rows from the database.

 We can enter the fetched data into a table or work area. The append work can also be done here.  It changes the position of the database cursor to the next line to be extracted.

 System can fetch one or more data records by this statement.  Sy-subrc will be zero when the system fetches data.

 When the cursor is at the last position of rows then the next cursor will cause sy-subrc = 4. Because no line will be extracted further.

Close Cursor Statement:

 It closes the database cursor and initializes the cursor variable.

 We should close all the open database cursor if they are no longer required.  Once the cursor is closed it no longer is accessed.

In the following example we have demonstrated a program where cursor has been used. REPORT zsr_test NO STANDARD PAGE HEADING.

TABLES spfli.

DATA: wa_spfli TYPE spfli.

"Declare cursor

data: cr_spfli TYPE cursor.

PARAMETERS p_from TYPE spfli-countryfr.

"Open Cursor

(65)

FROM spfli WHERE countryfr = p_from. IF sy-subrc = 0. WRITE: / 'Airline', 10 'Flight Number', 30 'Country From', 45 'City From', 66 'Departure airport', 86 'Country To', 100 'City To', 121 'Destination airport', 142 'Departure time', 160 'Arrival time', 175 'Distance'. ULINE. SKIP. ENDIF. DO.

"Fetch Next Cursor

FETCH NEXT CURSOR cr_spfli INTO wa_spfli.

IF sy-subrc = 0.

CHECK wa_spfli-countryfr = p_from. WRITE: /3 wa_spfli-carrid, 10 wa_spfli-connid, 30 wa_spfli-countryfr, 45 wa_spfli-cityfrom, 66 wa_spfli-airpfrom, 86 wa_spfli-countryto, 100 wa_spfli-cityto, 121 wa_spfli-airpto, 142 wa_spfli-deptime, 160 wa_spfli-arrtime, 175 wa_spfli-distance. ELSE. EXIT. ENDIF. ENDDO. "Close Cursor

CLOSE CURSOR cr_spfli. The output is as follows.

(66)

This program can be done in another way as follows. REPORT zsr_test NO STANDARD PAGE HEADING. TABLES spfli.

DATA: wa_spfli TYPE spfli,

it_spfli TYPE TABLE OF spfli.

"Declare Cursor

DATA: cr_spfli TYPE cursor.

PARAMETERS p_from TYPE spfli-countryfr.

"Open Cursor

OPEN CURSOR cr_spfli FOR SELECT * FROM spfli WHERE countryfr = p_from.

"Fetch Cursor

FETCH NEXT CURSOR cr_spfli INTO TABLE it_spfli.

IF sy-subrc = 0.

WRITE: / 'Airline',

(67)

30 'Country From', 45 'City From', 66 'Departure airport', 86 'Country To', 100 'City To', 121 'Destination airport', 142 'Departure time', 160 'Arrival time', 175 'Distance'. ULINE. SKIP.

LOOP AT it_spfli INTO wa_spfli. WRITE: /3 wa_spfli-carrid, 10 wa_spfli-connid, 30 wa_spfli-countryfr, 45 wa_spfli-cityfrom, 66 wa_spfli-airpfrom, 86 wa_spfli-countryto, 100 wa_spfli-cityto, 121 wa_spfli-airpto, 142 wa_spfli-deptime, 160 wa_spfli-arrtime, 175 wa_spfli-distance. ENDLOOP. ENDIF. "Close Cursor

CLOSE CURSOR cr_spfli. The output is similar as before.

1. 9. ALV Grid Interactive with a Button

10. ALV List Display Report 11. ALV List Interactive Report

12. F4 Help for Parameter and Select Option 13. Two ALV Grids in Single Screen using OOPs 14. ALV Hierarchical Report

15. Report to Report with Parameter 16. Report to Report with Select Option 17. At Selection Screen event

References

Related documents

We believe that joining the global PRME network can support German business schools in building up capacity with regard to responsible management education, learning from

AMI: Approximate measurement invariance; DIC: Deviance information criteria; DIF: Differential item functioning; EFA: Exploratory factor analysis; IRT: Item-response theory; LCA:

Considering the problem of deleting edges to obtain a graph with restricted maximum componet size, restricted to graphs of small treewidth, the algorithm we describe in this

- Start > Settings > Control Panel > Printer and Faxes > Right Click Installed Printer > Sharing > Share this printer > OK..

Information related to the curricula of Punjab/Federal Boards, student activities, personnel evaluation methods, Government of Punjab employment rules, current data

The intake of fat and carbohydrates did not contribute to the association between the macronutrient composition of the diet and overall health, measured by the frailty index..

The Cleveland Indians have started offering discounts on tickets through Facebook in their most recent social media campaign (McGarry, 2011). Teams can also use Facebook to

The primary purpose of these assignments is this; for you to integrate class discussions, readings, and personal experiences to assist you in developing a personally relevant