Generate Subroutine Pool – Best way to concatenate Dynamic Internal table

By | Last Updated on April 24, 2013 | 10,292

Generate Subroutine Pool is a very strong statement when used properly. The code snippet here shows how to use ABAP Generate Subroutine pool to concatenate dynamic table field content.

Quite few time ago, I have posted a solution to an question: Best way to concatenate with a ; in dynamic internal table at SCN.

Generate Subroutine POOL

 
*&---------------------------------------------------------------------*
*& Purpose - Generate Subroutine Pool Demo
*& Author  - Naimesh Patel
*& URL     - http://zevolving.com/?p=2189
*&---------------------------------------------------------------------*
 
TYPE-POOLS: abap.
TYPES: BEGIN OF ty_data,
       fld1 TYPE char10,
       fld2 TYPE char20,
       fld3 TYPE char30,
       END   OF ty_data.
DATA: t_data TYPE STANDARD TABLE OF ty_data,
      la_data LIKE LINE OF t_data.
DATA: la_500(500) TYPE c.
*
DATA : i_details    TYPE abap_compdescr_tab,
       la_details   LIKE LINE OF i_details.
DATA : i_ref_descr  TYPE REF TO cl_abap_structdescr.
*
DATA: prog TYPE string,
      tab  TYPE STANDARD TABLE OF string,
      mess TYPE string,
      sid  TYPE string,
      lf_string TYPE string,
      lf_field  TYPE string.
DATA: lf_cnt TYPE char10,
      lf_sep TYPE char3 VALUE "';"'.
*
START-OF-SELECTION.
*
* ... Get fields
  i_ref_descr ?= cl_abap_typedescr=>describe_by_data( la_data ).
  i_details[] = i_ref_descr->components[].
*
* ... Generate subroutine pool
  APPEND 'PROGRAM subpool.'                        TO tab.
  APPEND 'FORM concatenate using la_data changing la_500.' TO tab.
  LOOP AT i_details INTO la_details.
    lf_cnt = sy-tabix.
    CONDENSE lf_cnt.
    CONCATENATE '<FS_FLD' lf_cnt '>' INTO lf_field.
    CONCATENATE 'field-symbols: ' lf_field INTO lf_string.
    CONCATENATE lf_string 'TYPE ANY.' INTO lf_string SEPARATED BY space.
    APPEND lf_string TO tab.
    CONCATENATE 'ASSIGN COMPONENT' lf_cnt 'OF STRUCTURE LA_DATA TO'
      lf_field '.'
      INTO lf_string SEPARATED BY space.
    APPEND lf_string TO tab.
  ENDLOOP.
  lf_string = 'CONCATENATE'.
  LOOP AT i_details INTO la_details.
    lf_cnt = sy-tabix.
    CONDENSE lf_cnt.
    CONCATENATE '<FS_FLD' lf_cnt '>' INTO lf_field.
    CONCATENATE lf_string lf_field INTO lf_string SEPARATED BY space.
  ENDLOOP.
  CONCATENATE lf_string 'INTO LA_500 SEPARATED BY ' lf_sep '.'
    INTO lf_string SEPARATED BY space.
  APPEND lf_string TO tab.
  APPEND 'ENDFORM.' TO tab.
*
  prog = 'ZTEST_1'.
*
* Generate the Subroutine pool
  GENERATE SUBROUTINE POOL tab NAME prog
           MESSAGE mess
           SHORTDUMP-ID sid.
*
  IF sy-subrc = 0.
  ELSEIF sy-subrc = 4.
    MESSAGE mess TYPE 'I'.
    EXIT.
  ELSEIF sy-subrc = 8.
    MESSAGE sid TYPE 'I'.
    EXIT.
  ENDIF.
*
* ... Test data
  la_data-fld1 = 'Test'.
  la_data-fld2 = 'Testing'.
  la_data-fld3 = 'Testing3'.
  APPEND la_data TO t_data.
  CLEAR  la_data.
*
  la_data-fld1 = '1121'.
  la_data-fld2 = '54534'.
  la_data-fld3 = 'djhdjdj'.
  APPEND la_data TO t_data.
  CLEAR  la_data.
*
* ... dynamic concatenate
  LOOP AT t_data INTO la_data.
    PERFORM ('CONCATENATE') IN PROGRAM (prog) IF FOUND
      USING la_data
      CHANGING la_500.
    WRITE: / la_500.
  ENDLOOP.
 

Read more: Best way to concatenate with a ; in dynamic internal table

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

4 Comments

  • steve oldner

    Thanks!! What is this used for?

  • oo.sap.abap

    I would also like to know what are the practical uses for this ? Can you please elaborate.

  • Hello Steve / oo.sap.abap,

    Generate subroutine pool is used heavily when you would need to work with Dynamic Programming.

    Dynamic Programming, you can either go by Using Field-Symbols all the time to get the access or generate a Subroutine pool to make the dynamic coding, more kind of static. The generated program would be in the memory in that session so you would be able to call the Subroutines or Class dynamically in that session.

    Before introduction to RTTS, the I had been using this technique to generate the Program and declare my table statically in that program. I get a data reference back from that program using a dynamic call. You can find that technique at – Dynamic Internal Table with Generate Subroutine Pool

    SAP uses this heavily in BW (now BI) to generate the data load programs. Since the nature of these infocubes is a dynamic, SAP uses Generate Subroutine pool everytime a load is attempted. It would have static logic with defined field names which allow not to use Field-Symbols. Usage of Field Symbol to get an access to field values compared to static program multiple times, would be a performance drainage as System has to manage internal assignment when use FS.

    Thanks,
    Naimesh Patel

  • steve oldner

    Great things to know. Thanks for sharing!

Comments on this Post are now closed. If you have something important to share, you can always contact me.