Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  program editing →  Dynamic Program Editing →  Source Code 

GENERATE SUBROUTINE POOL

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


GENERATE SUBROUTINE POOL itab NAME prog [error_handling]. 

Effect

This statement generates a temporary subroutine pool. The source code of the subroutine pool is taken from the internal table itab. The generated subroutine pool is stored internally in the current internal mode. The eight-character name of the temporary subroutine pool is assigned to the variable prog.

For itab, only a standard table without secondary table keys is permitted. The row type of itab must be character-like. A source code line in itab may contain a maximum of 255 characters. The data object prog must also be character-like. In an internal mode, a maximum of 36 temporary subroutine pools may be created.

If the source code in itab has a syntax error, the subroutine pool is not generated and initialized using prog. The addition error_handling can be used to analyze syntax errors and generation errors. For the syntax check, the switch configuration of Switch Framework is used in the state it had when the current transaction was called.

If an exception is raised when the subroutine pool is generated, the runtime error is handled internally so that no programs are terminated. Instead, sy-subrc is set to the value 8. However, there is still a database rollback and the corresponding short dump is saved as normal. The addition SHORTDUMP-ID can be used to determine the ID of the runtime error.

In the source code of the subroutine pool, subroutines can be called from all programs that are loaded in the same internal mode by specifying the program name prog using the statement PERFORM. When a subroutine is called for the first time in the subroutine pool, this is loaded into internal mode, and the event LOAD-OF-PROGRAM is triggered.

System Fields

sy-subrc Meaning
0 Generation was successful.
4 The source code contains a syntax error.
8 A generation error occurred The resulting runtime error was handled internally.

If a runtime error occurs during the generation process (sy-subrc has the value 8), a database rollback is executed as usual.

Programming Guideline

Generic Programming


Notes

  • Since subroutines are now obsolete as a method of program modularization, a temporary subroutine pool created using GENERATE SUBROUTINE POOL should only contain a single initial subroutine that calls a method of a local class and does not contain any other functional code.
  • If the program that creates the subroutine pool is a Unicode program, the corresponding syntax rules apply for the generated subroutine pool.
  • Using the switch configuration from when the transaction was called for the syntax check ensures that the whole transaction is executed using the same switch configuration (guaranteed by Switch Framework).
  • The source code in the internal table itab must contain a complete ABAP program, including the statement that introduces the program.
  • In a temporary subroutine pool, the same global declarations and editing rules are defined as in the static subroutine pool of the repository (see table of program types).
  • The addition REDUCED FUNCTIONALITY of the introductory program statement PROGRAM also works in temporary subroutine pools and is recommended to reduce their resource use.
  • Temporarily generated subroutine pools can be executed in ABAP Debugger in single steps.
  • A temporary subroutine pool generated for an internal mode cannot be deleted explicitly. It remains available from its generation up to the point where the internal session is terminated.
  • GENERATE SUBROUTINE POOL should only be used in exceptional cases in application programs. ABAP provides many other means of dynamic programming, which generally make creating source code dynamically unnecessary (see the list in dynamic program processing).

Example

Creates and generates (dynamically) a subroutine pool that implements the event block LOAD-OF-PROGRAM and two subroutines. Depending on the return code sy-subrc, a subroutine is called or a message is issued.

DATA: prog  TYPE string, 
      tab  TYPE STANDARD TABLE OF string, 
      mess TYPE string, 
      sid  TYPE string. 

APPEND 'PROGRAM subpool.'                        TO tab. 
APPEND `DATA spfli_tab TYPE TABLE OF spfli.`     TO tab. 
APPEND `LOAD-OF-PROGRAM.`                       TO tab. 
APPEND `  SELECT *` & 
       `         FROM spfli` & 
       `         INTO TABLE spfli_tab.`          TO tab. 
APPEND `FORM loop_at_tab.`                       TO tab. 
APPEND `  DATA spfli_wa TYPE spfli.`             TO tab. 
APPEND `  LOOP AT spfli_tab INTO spfli_wa.`      TO tab. 
APPEND `    PERFORM evaluate_wa USING spfli_wa.` TO tab. 
APPEND `  ENDLOOP.`                             TO tab. 
APPEND `ENDFORM.`                               TO tab. 
APPEND `FORM evaluate_wa USING l_wa TYPE spfli.` TO tab. 
APPEND `  WRITE: / l_wa-carrid, l_wa-connid.`    TO tab. 
APPEND `ENDFORM.`                               TO tab. 

GENERATE SUBROUTINE POOL tab NAME prog 
         MESSAGE mess 
         SHORTDUMP-ID sid. 

IF sy-subrc = 0. 
  PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND. 
ELSEIF sy-subrc = 4. 
  MESSAGE mess TYPE 'I'. 
ELSEIF sy-subrc = 8. 
  MESSAGE sid TYPE 'I'. 
ENDIF. 

Example

Creates and generates (dynamically) a subroutine pool that implements a local class. The static method meth of the class can be called using the absolute type name of the class.

DATA itab TYPE TABLE OF string. 
DATA prog  TYPE string. 
DATA class TYPE string. 

APPEND `program.`                     TO itab. 
APPEND `class main definition.`       TO itab. 
APPEND `  public section.`            TO itab. 
APPEND `    class-methods meth.`      TO itab. 
APPEND `endclass.`                   TO itab. 
APPEND `class main implementation.`   TO itab. 
APPEND `  method meth.`               TO itab. 
APPEND `    message 'Test' type 'I'.` TO itab. 
APPEND `  endmethod.`                TO itab. 
APPEND `endclass.`                    TO itab. 

GENERATE SUBROUTINE POOL itab NAME prog. 

class = `\PROGRAM=` && prog && `\CLASS=MAIN`. 

CALL METHOD (class)=>meth. 

Example

Creates and generates (dynamically) a subroutine pool that implements a local class. The class is instantiated using its absolute type name, and the instance method meth is called dynamically.

DATA itab TYPE TABLE OF string. 
DATA prog  TYPE string. 
DATA class TYPE string. 
DATA oref TYPE REF TO object. 

APPEND `program.`                     TO itab. 
APPEND `class main definition.`       TO itab. 
APPEND `  public section.`            TO itab. 
APPEND `    methods meth.`            TO itab. 
APPEND `endclass.`                    TO itab. 
APPEND `class main implementation.`   TO itab. 
APPEND `  method meth.`               TO itab. 
APPEND `    message 'Test' type 'I'.` TO itab. 
APPEND `  endmethod.`                TO itab. 
APPEND `endclass.`                    TO itab. 

GENERATE SUBROUTINE POOL itab NAME prog. 

class = `\PROGRAM=` && prog && `\CLASS=MAIN`. 

CREATE OBJECT oref TYPE (class). 

CALL METHOD oref->('METH'). 

Exceptions


Catchable Exceptions

CX_SY_GENERATE_SUBPOOL_FULL

  • Cause: No further temporary subroutine pools can be generated.
    Runtime Error: GENERATE_SUBPOOL_DIR_FULL

CX_SY_GEN_SOURCE_TOO_WIDE

  • Cause: The source code is in a table consisting of strings and the table contains rows with more than 255 characters.
    Runtime Error: GEN_SOURCE_TOO_WIDE

Continue

GENERATE SUBROUTINE POOL - error_handling

GENERATE SUBROUTINE POOL - internal addition