Internal Table as Parameter of Method
We can have an internal table as a parameter of a method. This case we have to Export the internal table at the declaration time of the method. And we shall import the internal table at the time of calling of the method.
In the following program we are showing item wise header information of Purchase order in two different displays. The output will contain the header information at top and item information at below of the same screen. We are getting the Purchase Order number from the input parameter.
REPORT zsr_test NO STANDARD PAGE HEADING.
TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
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,
mtart TYPE ekpo-mtart,
END OF ty_ekpo.
DATA: wa_ekko TYPE ty_ekko,
wa_ekpo TYPE ty_ekpo,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.
INITIALIZATION.
PARAMETERS p_ebeln TYPE ekko-ebeln.
*----------------------------------------------------------------------*
* CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS: m_ekko IMPORTING po TYPE ekko-ebeln
EXPORTING lt TYPE ANY TABLE,
m_ekpo.
ENDCLASS. "cls
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD m_ekko.
SELECT ebeln lifnr
FROM ekko INTO TABLE lt
WHERE ebeln = po.
IF sy-subrc = 0.
LOOP AT lt INTO wa_ekko.
AT FIRST.
WRITE: / 'Purchase Order Header:'.
WRITE: / 'PO No.',
12 'Vendor',
/ '-----------------'.
SKIP.
ENDAT.
WRITE: / wa_ekko-ebeln,
12 wa_ekko-lifnr.
CLEAR wa_ekko.
ENDLOOP.
WRITE: / '-----------------'.
SKIP 2.
ENDIF.
ENDMETHOD. "m_ekko
METHOD m_ekpo.
IF it_ekko IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort mtart
FROM ekpo INTO TABLE it_ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
IF sy-subrc = 0.
LOOP AT it_ekpo INTO wa_ekpo.
AT FIRST.
WRITE: / 'Purchase Order Item:'.
WRITE: / 'PO No.',
12 'Item',
20 'Material',
40 'Plant',
48 'Storage',
57 'Type'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
12 wa_ekpo-ebelp,
20 wa_ekpo-matnr,
40 wa_ekpo-werks,
48 wa_ekpo-lgort,
57 wa_ekpo-mtart.
CLEAR wa_ekpo.
ENDLOOP.
ULINE.
ENDIF.
ENDIF.
ENDMETHOD. "m_ekpo
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD: obj->m_ekko EXPORTING po = p_ebeln
IMPORTING lt = it_ekko,
obj->m_ekpo.
TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
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,
mtart TYPE ekpo-mtart,
END OF ty_ekpo.
DATA: wa_ekko TYPE ty_ekko,
wa_ekpo TYPE ty_ekpo,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.
INITIALIZATION.
PARAMETERS p_ebeln TYPE ekko-ebeln.
*----------------------------------------------------------------------*
* CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS: m_ekko IMPORTING po TYPE ekko-ebeln
EXPORTING lt TYPE ANY TABLE,
m_ekpo.
ENDCLASS. "cls
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD m_ekko.
SELECT ebeln lifnr
FROM ekko INTO TABLE lt
WHERE ebeln = po.
IF sy-subrc = 0.
LOOP AT lt INTO wa_ekko.
AT FIRST.
WRITE: / 'Purchase Order Header:'.
WRITE: / 'PO No.',
12 'Vendor',
/ '-----------------'.
SKIP.
ENDAT.
WRITE: / wa_ekko-ebeln,
12 wa_ekko-lifnr.
CLEAR wa_ekko.
ENDLOOP.
WRITE: / '-----------------'.
SKIP 2.
ENDIF.
ENDMETHOD. "m_ekko
METHOD m_ekpo.
IF it_ekko IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort mtart
FROM ekpo INTO TABLE it_ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
IF sy-subrc = 0.
LOOP AT it_ekpo INTO wa_ekpo.
AT FIRST.
WRITE: / 'Purchase Order Item:'.
WRITE: / 'PO No.',
12 'Item',
20 'Material',
40 'Plant',
48 'Storage',
57 'Type'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
12 wa_ekpo-ebelp,
20 wa_ekpo-matnr,
40 wa_ekpo-werks,
48 wa_ekpo-lgort,
57 wa_ekpo-mtart.
CLEAR wa_ekpo.
ENDLOOP.
ULINE.
ENDIF.
ENDIF.
ENDMETHOD. "m_ekpo
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD: obj->m_ekko EXPORTING po = p_ebeln
IMPORTING lt = it_ekko,
obj->m_ekpo.
The selection is based on the parameter of a purchase order.
The output is as follows.
Now we can achieve the same thing by using Selection range (select option). In the following program we have illustrated this.
REPORT zsr_test NO STANDARD PAGE HEADING.
TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
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,
mtart TYPE ekpo-mtart,
END OF ty_ekpo.
DATA: wa_ekko TYPE ty_ekko,
wa_ekpo TYPE ty_ekpo,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.
INITIALIZATION.
SELECT-OPTIONS s_ebeln FOR ekko-ebeln.
*----------------------------------------------------------------------*
* CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS: m_ekko IMPORTING po LIKE s_ebeln[]
EXPORTING lt TYPE ANY TABLE,
m_ekpo.
ENDCLASS. "cls
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD m_ekko.
SELECT ebeln lifnr
FROM ekko INTO TABLE lt
WHERE ebeln IN po.
IF sy-subrc = 0.
LOOP AT lt INTO wa_ekko.
AT FIRST.
WRITE: / 'Purchase Order Header:'.
WRITE: / 'PO No.',
12 'Vendor',
/ '-----------------'.
SKIP.
ENDAT.
WRITE: / wa_ekko-ebeln,
12 wa_ekko-lifnr.
CLEAR wa_ekko.
ENDLOOP.
WRITE: / '-----------------'.
SKIP 2.
ENDIF.
ENDMETHOD. "m_ekko
METHOD m_ekpo.
IF it_ekko IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort mtart
FROM ekpo INTO TABLE it_ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
IF sy-subrc = 0.
LOOP AT it_ekpo INTO wa_ekpo.
AT FIRST.
WRITE: / 'Purchase Order Item:'.
WRITE: / 'PO No.',
12 'Item',
20 'Material',
40 'Plant',
48 'Storage',
57 'Type'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
12 wa_ekpo-ebelp,
20 wa_ekpo-matnr,
40 wa_ekpo-werks,
48 wa_ekpo-lgort,
57 wa_ekpo-mtart.
CLEAR wa_ekpo.
ENDLOOP.
ULINE.
ENDIF.
ENDIF.
ENDMETHOD. "m_ekpo
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD: obj->m_ekko EXPORTING po = s_ebeln[]
IMPORTING lt = it_ekko,
obj->m_ekpo.
TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
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,
mtart TYPE ekpo-mtart,
END OF ty_ekpo.
DATA: wa_ekko TYPE ty_ekko,
wa_ekpo TYPE ty_ekpo,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo.
INITIALIZATION.
SELECT-OPTIONS s_ebeln FOR ekko-ebeln.
*----------------------------------------------------------------------*
* CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS: m_ekko IMPORTING po LIKE s_ebeln[]
EXPORTING lt TYPE ANY TABLE,
m_ekpo.
ENDCLASS. "cls
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD m_ekko.
SELECT ebeln lifnr
FROM ekko INTO TABLE lt
WHERE ebeln IN po.
IF sy-subrc = 0.
LOOP AT lt INTO wa_ekko.
AT FIRST.
WRITE: / 'Purchase Order Header:'.
WRITE: / 'PO No.',
12 'Vendor',
/ '-----------------'.
SKIP.
ENDAT.
WRITE: / wa_ekko-ebeln,
12 wa_ekko-lifnr.
CLEAR wa_ekko.
ENDLOOP.
WRITE: / '-----------------'.
SKIP 2.
ENDIF.
ENDMETHOD. "m_ekko
METHOD m_ekpo.
IF it_ekko IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort mtart
FROM ekpo INTO TABLE it_ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
IF sy-subrc = 0.
LOOP AT it_ekpo INTO wa_ekpo.
AT FIRST.
WRITE: / 'Purchase Order Item:'.
WRITE: / 'PO No.',
12 'Item',
20 'Material',
40 'Plant',
48 'Storage',
57 'Type'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
12 wa_ekpo-ebelp,
20 wa_ekpo-matnr,
40 wa_ekpo-werks,
48 wa_ekpo-lgort,
57 wa_ekpo-mtart.
CLEAR wa_ekpo.
ENDLOOP.
ULINE.
ENDIF.
ENDIF.
ENDMETHOD. "m_ekpo
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
CALL METHOD: obj->m_ekko EXPORTING po = s_ebeln[]
IMPORTING lt = it_ekko,
obj->m_ekpo.
Here we are using the selection screen with selection range.
The output is as follows.