Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

sub-routines

0 Kudos

hi all the below mentioned code derived by me for joining two internal tables and get data for the output table based on select options. can anyone please let me know hoe to code the same with USING SUB-ROUTINES.

TABLES : zekko, zekpo.



TYPES : BEGIN OF ty_outp,
ebeln TYPE zekko-ebeln,
ebelp TYPE zekpo-ebelp,
matnr TYPE zekpo-matnr,
arktx TYPE zekpo-arktx,
zqnty TYPE zekpo-zqnty,
zunit TYPE zekpo-zunit,
zamnt TYPE zekpo-zamnt,
zcurr TYPE zekko-zcurr,
erdat TYPE zekko-erdat,
ename TYPE zekko-ename,
END OF ty_outp.

TYPES : tt_outp TYPE TABLE OF ty_outp.

DATA : gt_outp TYPE tt_outp,
wt_outp TYPE ty_outp.

DATA : gt_ekko TYPE TABLE OF zekko,
gt_ekpo TYPE TABLE OF zekpo,
wt_ekko TYPE zekko,
wt_ekpo TYPE zekpo.

SELECT-OPTIONS : s_ebeln FOR zekko-ebeln,
s_erdat FOR zekko-erdat,
s_matnr FOR zekpo-matnr.

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-001.
PARAMETERS : r1 RADIOBUTTON GROUP grp1 DEFAULT 'X' USER-COMMAND actn,
r2 RADIOBUTTON GROUP grp1,
r3 RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF BLOCK b2.



START-OF-SELECTION.



SELECT * FROM zekko INTO TABLE gt_ekko.


IF gt_ekko IS NOT INITIAL.

SELECT * FROM zekpo INTO TABLE gt_ekpo
FOR ALL ENTRIES IN gt_ekko WHERE ebeln = gt_ekko-ebeln.

ENDIF.

LOOP AT gt_ekko INTO wt_ekko.

READ TABLE gt_ekpo INTO wt_ekpo WITH KEY ebeln = wt_ekko-ebeln.

wt_outp-ebeln = wt_ekko-ebeln.
wt_outp-zcurr = wt_ekko-zcurr.
wt_outp-ename = wt_ekko-ename.
wt_outp-erdat = wt_ekko-erdat.
wt_outp-arktx = wt_ekpo-arktx.
wt_outp-zqnty = wt_ekpo-zqnty.
wt_outp-zamnt = wt_ekpo-zamnt.
wt_outp-zunit = wt_ekpo-zunit.
wt_outp-matnr = wt_ekpo-matnr.
wt_outp-ebelp = wt_ekpo-ebelp.


APPEND wt_outp TO gt_outp.


ENDLOOP.

CASE 'X'.

WHEN r1.

LOOP AT gt_outp INTO wt_outp
WHERE ebeln IN s_ebeln
AND matnr IN s_matnr
AND erdat IN s_erdat.

WRITE : / wt_outp-ebeln , wt_outp-ebelp , wt_outp-matnr , wt_outp-arktx , wt_outp-zqnty , wt_outp-zunit , wt_outp-zcurr , wt_outp-erdat , wt_outp-ename.

ENDLOOP.
ENDCASE.

TOP-OF-PAGE.

WRITE : / 'PO Number' , 11 'Item No' , 19 'Material no', 35 'Mat.Description', 77 'qnty' ,
84 'unit', 88 'Curncy', 95 'Crtd date' , 106 'crtd By'.

END-OF-PAGE.

1 ACCEPTED SOLUTION

mateuszadamus
Active Contributor

Hello karoncdas_31

When dividing a report into procedures it's also good to get rid of all the global variable which are not necessary. To do this, you will need to define parameters for the procedures. And to do that, you might need to define types for these parameters, if not defined in ABAP dictionary already.

tt_zekko TYPE TABLE OF zekko,   
tt_zekpo TYPE TABLE OF zekpo.

In my example I've created one main procedure to execute the logic of the report and named it RUN_REPORT. In this procedure I declared local variables which are going to be used throughout the whole program.

FORM run_report.
 DATA:
   lt_outp TYPE tt_outp,
   lt_ekko TYPE tt_zekko,
   lt_ekpo TYPE tt_zekpo.

Later these variables are used as parameters in different parts of the program.

 PERFORM read_data CHANGING lt_ekko lt_ekpo.
 PERFORM join_data USING lt_ekko lt_ekpo CHANGING lt_outp.
 PERFORM print_data USING lt_outp.

Here is a bit more about the procedures in ABAP programs from SAP Help. You should definitely read it. It might also be a good idea for you to read about Object Oriented programming, since this is really the right way to go these days. There is plenty of the information (from easy to complex) on the web.

Below is your full code.

TABLES: zekko, zekpo.

TYPES:
  BEGIN OF ty_outp,
    ebeln TYPE zekko-ebeln,
    ebelp TYPE zekpo-ebelp,
    matnr TYPE zekpo-matnr,
    arktx TYPE zekpo-arktx,
    zqnty TYPE zekpo-zqnty,
    zunit TYPE zekpo-zunit,
    zamnt TYPE zekpo-zamnt,
    zcurr TYPE zekko-zcurr,
    erdat TYPE zekko-erdat,
    ename TYPE zekko-ename,
  END OF ty_outp,
  tt_outp  TYPE TABLE OF ty_outp,

  tt_zekko TYPE TABLE OF zekko,
  tt_zekpo TYPE TABLE OF zekpo.

SELECT-OPTIONS: s_ebeln FOR zekko-ebeln,
s_erdat FOR zekko-erdat,
s_matnr FOR zekpo-matnr.

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-001.
PARAMETERS : r1 RADIOBUTTON GROUP grp1 DEFAULT 'X' USER-COMMAND actn,
             r2 RADIOBUTTON GROUP grp1,
             r3 RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF BLOCK b2.

START-OF-SELECTION.
  PERFORM run_report.

TOP-OF-PAGE.
  WRITE : / 'PO Number' , 11 'Item No' , 19 'Material no', 35 'Mat.Description', 77 'qnty' ,
  84 'unit', 88 'Curncy', 95 'Crtd date' , 106 'crtd By'.

END-OF-PAGE.

FORM run_report.
  DATA:
    lt_outp TYPE tt_outp,
    lt_ekko TYPE tt_zekko,
    lt_ekpo TYPE tt_zekpo.

  PERFORM read_data CHANGING lt_ekko lt_ekpo.
  PERFORM join_data USING lt_ekko lt_ekpo CHANGING lt_outp.
  PERFORM print_data USING lt_outp.
ENDFORM.

FORM read_data
  CHANGING
    ct_ekko TYPE tt_zekko
    ct_ekpo TYPE tt_zekpo.

  SELECT * FROM zekko INTO TABLE ct_ekko.

  IF gt_ekko IS NOT INITIAL.
    SELECT * FROM zekpo INTO TABLE ct_ekpo
    FOR ALL ENTRIES IN ct_ekko WHERE ebeln = ct_ekko-ebeln.
  ENDIF.
ENDFORM.

FORM join_data
  USING
    it_ekko TYPE tt_zekko
    it_ekpo TYPE tt_zekpo
  CHANGING
    ct_outp TYPE tt_outp.

  DATA:
    ls_outp TYPE ty_outp,
    ls_ekko TYPE zekko,
    ls_ekpo TYPE zekpo.

  LOOP AT it_ekko INTO ls_ekko.
    READ TABLE it_ekpo INTO ls_ekpo WITH KEY ebeln = ls_ekko-ebeln.
    ls_outp-ebeln = ls_ekko-ebeln.
    ls_outp-zcurr = ls_ekko-zcurr.
    ls_outp-ename = ls_ekko-ename.
    ls_outp-erdat = ls_ekko-erdat.
    ls_outp-arktx = ls_ekpo-arktx.
    ls_outp-zqnty = ls_ekpo-zqnty.
    ls_outp-zamnt = ls_ekpo-zamnt.
    ls_outp-zunit = ls_ekpo-zunit.
    ls_outp-matnr = ls_ekpo-matnr.
    ls_outp-ebelp = ls_ekpo-ebelp.

    APPEND ls_outp TO ct_outp.
  ENDLOOP.
ENDFORM.

FORM print_data USING it_outp TYPE tt_outp.
  DATA:
    ls_outp TYPE ty_outp.

  CASE abap_true.
    WHEN r1.
      LOOP AT it_outp INTO ls_outp
        WHERE ebeln IN s_ebeln
          AND matnr IN s_matnr
          AND erdat IN s_erdat.

        WRITE : / ls_outp-ebeln , ls_outp-ebelp , ls_outp-matnr , ls_outp-arktx , ls_outp-zqnty , ls_outp-zunit , ls_outp-zcurr , ls_outp-erdat , ls_outp-ename.
      ENDLOOP.
  ENDCASE.
ENDFORM.
Kind regards,
Mateusz
3 REPLIES 3

Sandra_Rossi
Active Contributor

People have already gave you lots of links so that you can study subroutines. See there: Subroutines. If you need further assistance, please tell us what you don't understand.

Sandra_Rossi
Active Contributor

Please use the button CODE to format your code.

mateuszadamus
Active Contributor

Hello karoncdas_31

When dividing a report into procedures it's also good to get rid of all the global variable which are not necessary. To do this, you will need to define parameters for the procedures. And to do that, you might need to define types for these parameters, if not defined in ABAP dictionary already.

tt_zekko TYPE TABLE OF zekko,   
tt_zekpo TYPE TABLE OF zekpo.

In my example I've created one main procedure to execute the logic of the report and named it RUN_REPORT. In this procedure I declared local variables which are going to be used throughout the whole program.

FORM run_report.
 DATA:
   lt_outp TYPE tt_outp,
   lt_ekko TYPE tt_zekko,
   lt_ekpo TYPE tt_zekpo.

Later these variables are used as parameters in different parts of the program.

 PERFORM read_data CHANGING lt_ekko lt_ekpo.
 PERFORM join_data USING lt_ekko lt_ekpo CHANGING lt_outp.
 PERFORM print_data USING lt_outp.

Here is a bit more about the procedures in ABAP programs from SAP Help. You should definitely read it. It might also be a good idea for you to read about Object Oriented programming, since this is really the right way to go these days. There is plenty of the information (from easy to complex) on the web.

Below is your full code.

TABLES: zekko, zekpo.

TYPES:
  BEGIN OF ty_outp,
    ebeln TYPE zekko-ebeln,
    ebelp TYPE zekpo-ebelp,
    matnr TYPE zekpo-matnr,
    arktx TYPE zekpo-arktx,
    zqnty TYPE zekpo-zqnty,
    zunit TYPE zekpo-zunit,
    zamnt TYPE zekpo-zamnt,
    zcurr TYPE zekko-zcurr,
    erdat TYPE zekko-erdat,
    ename TYPE zekko-ename,
  END OF ty_outp,
  tt_outp  TYPE TABLE OF ty_outp,

  tt_zekko TYPE TABLE OF zekko,
  tt_zekpo TYPE TABLE OF zekpo.

SELECT-OPTIONS: s_ebeln FOR zekko-ebeln,
s_erdat FOR zekko-erdat,
s_matnr FOR zekpo-matnr.

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-001.
PARAMETERS : r1 RADIOBUTTON GROUP grp1 DEFAULT 'X' USER-COMMAND actn,
             r2 RADIOBUTTON GROUP grp1,
             r3 RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF BLOCK b2.

START-OF-SELECTION.
  PERFORM run_report.

TOP-OF-PAGE.
  WRITE : / 'PO Number' , 11 'Item No' , 19 'Material no', 35 'Mat.Description', 77 'qnty' ,
  84 'unit', 88 'Curncy', 95 'Crtd date' , 106 'crtd By'.

END-OF-PAGE.

FORM run_report.
  DATA:
    lt_outp TYPE tt_outp,
    lt_ekko TYPE tt_zekko,
    lt_ekpo TYPE tt_zekpo.

  PERFORM read_data CHANGING lt_ekko lt_ekpo.
  PERFORM join_data USING lt_ekko lt_ekpo CHANGING lt_outp.
  PERFORM print_data USING lt_outp.
ENDFORM.

FORM read_data
  CHANGING
    ct_ekko TYPE tt_zekko
    ct_ekpo TYPE tt_zekpo.

  SELECT * FROM zekko INTO TABLE ct_ekko.

  IF gt_ekko IS NOT INITIAL.
    SELECT * FROM zekpo INTO TABLE ct_ekpo
    FOR ALL ENTRIES IN ct_ekko WHERE ebeln = ct_ekko-ebeln.
  ENDIF.
ENDFORM.

FORM join_data
  USING
    it_ekko TYPE tt_zekko
    it_ekpo TYPE tt_zekpo
  CHANGING
    ct_outp TYPE tt_outp.

  DATA:
    ls_outp TYPE ty_outp,
    ls_ekko TYPE zekko,
    ls_ekpo TYPE zekpo.

  LOOP AT it_ekko INTO ls_ekko.
    READ TABLE it_ekpo INTO ls_ekpo WITH KEY ebeln = ls_ekko-ebeln.
    ls_outp-ebeln = ls_ekko-ebeln.
    ls_outp-zcurr = ls_ekko-zcurr.
    ls_outp-ename = ls_ekko-ename.
    ls_outp-erdat = ls_ekko-erdat.
    ls_outp-arktx = ls_ekpo-arktx.
    ls_outp-zqnty = ls_ekpo-zqnty.
    ls_outp-zamnt = ls_ekpo-zamnt.
    ls_outp-zunit = ls_ekpo-zunit.
    ls_outp-matnr = ls_ekpo-matnr.
    ls_outp-ebelp = ls_ekpo-ebelp.

    APPEND ls_outp TO ct_outp.
  ENDLOOP.
ENDFORM.

FORM print_data USING it_outp TYPE tt_outp.
  DATA:
    ls_outp TYPE ty_outp.

  CASE abap_true.
    WHEN r1.
      LOOP AT it_outp INTO ls_outp
        WHERE ebeln IN s_ebeln
          AND matnr IN s_matnr
          AND erdat IN s_erdat.

        WRITE : / ls_outp-ebeln , ls_outp-ebelp , ls_outp-matnr , ls_outp-arktx , ls_outp-zqnty , ls_outp-zunit , ls_outp-zcurr , ls_outp-erdat , ls_outp-ename.
      ENDLOOP.
  ENDCASE.
ENDFORM.
Kind regards,
Mateusz