Entering content frameCreating and Starting Temporary Subroutines Locate the document in its SAP Library structure

The dynamic programs discussed in the other topics of this section are created in the R/3 Repository. This topic describes how you can create and call dynamic subroutines in the main memory.

You write the source code of the subroutines as explained in Creating a New Program Dynamically into an internal table. To generate the program, you use the following statement:

Syntax

GENERATE SUBROUTINE POOL <itab> NAME <prog> [<options>].

This statement creates a subroutine pool in the main memory area of the running program. You pass the source code of the subroutine pool in internal table <itab>. The statement returns the name of the generated subroutine pool in field <prog> that should have type C of length 8. You use the name in <prog> to call the external subroutines defined in table <itab> through dynamic subroutine calls as explained in Specifying the Subroutine Name at Runtime.

The subroutine pool only exists during the runtime of the generating program and can only be called from within this program. You can create up to 36 subroutine pools for one program.

If an error occurs during generation, SY-SUBRC is set to 8. Otherwise, it is set to 0.

You can use the following options <options> in the GENERATE SUBROUTINE POOL statement:

In the case of a syntax error, field <mess> contains the error message.

In the case of a syntax error, field <incl> contains the name of the include program in which the error possibly occurred.

In the case of a syntax error, field <line> contains the number of the wrong line.

In the case of a syntax error, field <word> contains the wrong word.

In the case of a syntax error, field <offs> contains the offset of the wrong word in the line.

If you use this option, you switch on the trace mode and field <trac> contains the trace output.

Note

Compared to INSERT REPORT, the GENERATE SUBROUTINE POOL statement has a better performance. Furthermore, you do not have to care about naming conventions or restrictions of the correction and transport system when you use statement GENERATE SUBROUTINE POOL.

Example

REPORT SAPMZTST.

DATA: CODE(72) OCCURS 10,
PROG(8), MSG(120), LIN(3), WRD(10), OFF(3).

APPEND 'PROGRAM SUBPOOL.'
        TO CODE.
APPEND 'FORM DYN1.'
        TO CODE.
APPEND
'WRITE / ''Hello, I am the temporary subroutine DYN1!''.'
        TO CODE.
APPEND 'ENDFORM.'
        TO CODE.
APPEND 'FORM DYN2.'
        TO CODE.
APPEND
'WRIT / ''Hello, I am the temporary subroutine DYN2!''.'
        TO CODE.
APPEND 'ENDFORM.'
        TO CODE.

GENERATE SUBROUTINE POOL CODE NAME PROG
MESSAGE MSG
LINE LIN
WORD WRD
OFFSET OFF.

IF SY-SUBRC <> 0.
WRITE: / 'Error during generation in line', LIN,
/ MSG,
/ 'Word:', WRD, 'at offset', OFF.
ELSE.
  WRITE: / 'The name of the subroutine pool is', PROG.
  SKIP 2.
PERFORM DYN1 IN PROGRAM (PROG).
  SKIP 2.
PERFORM DYN2 IN PROGRAM (PROG).
ENDIF.

In this program, a subroutine pool containing two subroutines is placed into table CODE. Note that the temporary program must contain a REPORT or PROGRAM statement. Statement GENERATE SUBROUTINE POOL generates the temporary program. The output is as follows:

This graphic is explained in the accompanying text

A generation error occurred since the WRITE statement has been misspelled in the second subroutine, DYN2. After that line has been revised:

APPEND
'WRITE / ''Hello, I am the temporary subroutine DYN2!''.'
        TO CODE.

the output looks as follows:

This graphic is explained in the accompanying text

Generation was successful. The internal program name is displayed. Then, the subroutines of the subroutine pool are called. Note that you do not need to know the program name to call the subroutines.

 

 

 

 

 

 

Leaving content frame