Get Example source ABAP code based on a different SAP table
Standard SAP Help for GENERATE_REPORT
GENERATE SUBROUTINE POOL
Short Reference • GENERATE SUBROUTINE POOL ABAP Statement • NAME GENERATE SUBROUTINE POOL
ABAP Syntax GENERATE SUBROUTINE POOL itab NAME prog [error_handling].
What does it do? This statement generates a temporarysubroutine 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. The following can be specified for prog: An existing character-like variable An inline declaration DATA(var), where a variable of type PROGNAME is declared.
For itab, only a standard table withoutsecondary table keys is permitted. The row type of itab must be character-like. A line of source code in itab can have no more than 255 characters (if the row type has a fixed length, trailing blanks are ignored). In an internal mode, a maximum of 36 temporary subroutine pools can be created. INTHINT In fact, the length of 255 is checked only for strings. INTHINT For whatsoever reasons, tables of text fields can have INTHINT a length of 262243 characters. Of course, that should not be INTHINT exploited.
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 Fieldssy-subrcMeaning 0Generation was successful. 4The source code contains a syntax error. 8A 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.
ABAP_PGL Generic Programming ABAP Code Snippet
ABAP Code Snippet If used wrongly, dynamic programming techniques can present a serious security risk. Any dynamic content that is passed to a program from the outside must be checked thoroughly or escaped before being used in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the predefined function escape. ABAP Code Snippet ABAP Code Snippet See ABAP Command Injections.
Latest 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 r epository (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 ABAP Coding 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 tab TYPE STANDARD TABLE OF string WITH EMPTY KEY.
tab = VALUE #( ( `PROGRAM subpool.` ) ( `DATA spfli_tab TYPE TABLE OF spfli.` ) ( `LOAD-OF-PROGRAM.` ) ( ` SELECT *` <(> <)> ` FROM spfli` <(> <)> ` INTO TABLE spfli_tab.` ) ( `FORM loop_at_tab.` ) ( ` DATA spfli_wa TYPE spfli.` ) ( ` LOOP AT spfli_tab INTO spfli_wa.` ) ( ` PERFORM evaluate_wa USING spfli_wa.` ) ( ` ENDLOOP.` ) ( `ENDFORM.` ) ( `FORM evaluate_wa USING l_wa TYPE spfli.` ) ( ` cl_demo_output=>write_data( l_wa ).` ) ( `ENDFORM.` ) ).
GENERATE SUBROUTINE POOL tab NAME DATA(prog) MESSAGE DATA(mess) SHORTDUMP-ID DATA(sid).
IF sy-subrc = 0. PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND. cl_demo_output=>display( ). ELSEIF sy-subrc = 4. MESSAGE mess TYPE 'I'. ELSEIF sy-subrc = 8. MESSAGE sid TYPE 'I'. ENDIF.
Example ABAP Coding 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 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.
Example ABAP Coding 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 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.