You are on page 1of 10

Internal Tables Exercises

Unit: Internal Tables


Topic: Table Types, Pointer Accesses, and Internal
Buffering of Data Records

The database accesses are already optimized. The program runtime


can be further optimized by optimizing the processing of the internal
tables.
1-1 At the moment, all internal tables are standard tables. Does it make
sense to change the type of some of the tables?
1-2 In the MATCH_ORDERS_TO_PGI subroutine, the fully filled
internal itab table is processed in a LOOP...ENDLOOP. Optimize this
LOOP and its modification of the internal table. Changing the LOOP
affects the APPLY_PGI_DOCS and CURRENCY_CONVERSION
subroutines.
1-3 Avoid identical SELECT statements in KKNAI table by buffering data
records using internal tables.
1-3-1 Define a suitable internal table for buffering the read
data records from KKNAI table. Think about which type of
table is best for buffering.

Internal Tables: Solutions


Unit: Internal Tables
Topic: Table Types, Pointer Accesses, and Internal
Buffering of Data Records

Three improvements were made to the program.

1 Table type
The internal table issue is replaced by a table of type HASHED.
***********************************************************************
DATA
************************************************************************
DATA itab TYPE TABLE OF ty_tab WITH HEADER LINE.
*DATA issue TYPE TABLE OF ty_issue WITH HEADER LINE.
DATA issue TYPE HASHED TABLE OF ty_issue
WITH UNIQUE KEY order_# ord_pos WITH HEADER LINE.

Changing the table type affects the subprograms.


****************************************************************************
SELECT_DELV_PGI
****************************************************************************
* >>> SOLUTION
* Use of aggregate functions and having clause

SELECT SUM( rfmng ) INTO issue-pgi_qty


FROM vvbfa
WHERE vbelv = itab-order_#
AND posnv = itab-ord_pos
AND vbtyp_n = 'R'
GROUP BY vbelv posnv
HAVING SUM( rfmng ) > 0.

issue-order_# = itab-order_#.
issue-ord_pos = itab-ord_pos.
* APPEND issue.
INSERT TABLE issue.

****************************************************************************
MATCH_ORDERS_TO_PGI
****************************************************************************
.....

* READ TABLE issue COMPARING order_# ord_pos.


READ TABLE issue WITH TABLE KEY order_# = <fs_itab>-order_#
ord_pos = <fs_itab>-ord_pos.

2 A LOOP on the internal table itab is performed using ASSIGNING.


************************************************************************
* FIELD SYMBOLS
************************************************************************
FIELD-SYMBOLS: <fs_itab> TYPE ty_tab.

****************************************************************************
MATCH_ORDERS_TO_PGI
****************************************************************************
* >>> SOLUTION
LOOP AT itab ASSIGNING <fs_itab>.

IF <fs_itab>-order_# NE tmp_ord OR
<fs_itab>-ord_pos NE tmp_pos.

MOVE: <fs_itab>-order_# TO issue-order_#,


<fs_itab>-ord_pos TO issue-ord_pos.

MOVE: <fs_itab>-order_# TO tmp_ord,


<fs_itab>-ord_pos TO tmp_pos.

* READ TABLE issue COMPARING order_# ord_pos.


READ TABLE issue WITH TABLE KEY order_# = <fs_itab>-order_#
ord_pos = <fs_itab>-ord_pos.

IF sy-subrc EQ 0.
MOVE: issue-pgi_qty TO mpd_pgi,
issue-pgi_qty TO crd_pgi.
ELSE.
CLEAR: mpd_pgi,
crd_pgi.
ENDIF.

ENDIF.

PERFORM apply_pgi_docs.

ENDLOOP.
****************************************************************************
APPLY_PGI_DOCS
****************************************************************************
FORM apply_pgi_docs.

* >>> SOLUTION
* All 'itab' is replaced by '<fs_itab>'
* Manufactured Promised - issue Qty
IF mpd_pgi >= <fs_itab>-mpd_qty.
mpd_pgi = mpd_pgi - <fs_itab>-mpd_qty.
<fs_itab>-mpd_qty = 0.
ELSE.

<fs_itab>-mpd_qty = <fs_itab>-mpd_qty - mpd_pgi.


mpd_pgi = 0.
ENDIF.

* Customer Requested - issue qty


IF crd_pgi >= <fs_itab>-crd_qty.
crd_pgi = crd_pgi - <fs_itab>-crd_qty.
<fs_itab>-crd_qty = 0.
ELSE.
<fs_itab>-crd_qty = <fs_itab>-crd_qty - crd_pgi.
crd_pgi = 0.
ENDIF.

* Modify itab table to reflect open quantities.


IF <fs_itab>-mpd_qty = 0 AND <fs_itab>-crd_qty = 0.
DELETE itab.
ELSE.
IF <fs_itab>-curr NE currency.
PERFORM currency_conversion.
ELSE.
<fs_itab>-ext_mpd = ( <fs_itab>-mpd_qty * <fs_itab>-itm_val ).
<fs_itab>-ext_crd = ( <fs_itab>-crd_qty * <fs_itab>-itm_val ).
ENDIF.

* The MODIFY command is not necessary anymore

* MODIFY itab.
ENDIF.

ENDFORM. " APPLY_PGI_DOCS

****************************************************************************
CURRENCY_CONVERSION
****************************************************************************
FORM currency_conversion.

CALL FUNCTION 'READ_EXCHANGE_RATE' "added hhv


EXPORTING
date = <fs_itab>-entry
foreign_currency = <fs_itab>-curr
local_currency = currency
type_of_rate = 'M'
IMPORTING
exchange_rate = imprate
foreign_factor = ffact
local_factor = tfact
EXCEPTIONS
no_rate_found = 01.

* ITAB-EXT_MPD = ( ITAB-MPD_QTY * ITAB-ITM_VAL ) * XCURR-UKURS. "hhv

<fs_itab>-ext_mpd = ( <fs_itab>-mpd_qty * <fs_itab>-itm_val )


* imprate / ffact * tfact. "hhv

* ITAB-EXT_CRD = ( ITAB-CRD_QTY * ITAB-ITM_VAL ) * XCURR-UKURS. "hhv


<fs_itab>-ext_crd = ( <fs_itab>-crd_qty * <fs_itab>-itm_val )
* imprate / ffact * tfact. "hhv

ENDFORM. " CURRENCY_CONVERSION

3 The identical SELECTs on table KKNA1 are avoided by internal buffering in the program.
************************************************************************
* TYPES
************************************************************************
TYPES: BEGIN OF ty_buffer_kkna1,
kunnr LIKE kkna1-kunnr,
name1 LIKE kkna1-name1,
END OF ty_buffer_kkna1.
.....
************************************************************************
* DATA
************************************************************************
* Buffer table KKNA1
DATA buffer_kkna1 TYPE HASHED TABLE OF ty_buffer_kkna1

WITH UNIQUE KEY kunnr WITH HEADER LINE.


.....

****************************************************************************
PROCESS_GET_NAME1
****************************************************************************
FORM process_get_name1.

* SELECT SINGLE * FROM kkna1


* WHERE kunnr EQ vvbak-kunnr.
*
* itab-name1 = kkna1-name1.

* >>> SOLUTION
READ TABLE buffer_kkna1 WITH TABLE KEY kunnr = itab-kunnr.
IF sy-subrc <> 0.
* >>> SOLUTION
SELECT SINGLE name1 FROM kkna1
INTO buffer_kkna1-name1 WHERE kunnr EQ itab-kunnr.
buffer_kkna1-kunnr = itab-kunnr.
INSERT TABLE buffer_kkna1.

ELSE.
itab-name1 = buffer_kkna1-name1.
ENDIF.

ENDFORM. " PROCESS_GET_NAME1

You might also like