Creating a New Program Dynamically 

To create new dynamic programs during the runtime of an ABAP program, you must use an internal table. For this purpose, you should create this internal table with one character type column and a row width of 72. You can use any method you like from Filling Internal Tables to write the code of your new program into the internal table. Especially, you can use internal fields in which the contents depend on the flow of the program that you use to create the new program. The following example shows how to proceed generally:

DATA CODE(72) OCCURS 10.

APPEND 'REPORT ZDYN1.'
         TO CODE.

APPEND 'WRITE / ''Hello, I am dynamically created!''.'
         TO CODE.

Two lines of a simple program are written into internal table CODE.

In the next step you must transfer the new module, which in the above example is an executable program (report), into the program library. To do this, you can use the following statement:

Syntax

INSERT REPORT <prog> FROM <itab>.

Program <prog> is added to your current development class in the R/3 Repository. If a program with this name does not yet exist, a new program is created with the following attributes:

Either write the name of program <prog> using single quotation marks, or specify the name of a character field that contains the program name. It makes sense to use the name specified in the code as the program name, although you are not required to do so. <itab> is the internal table that contains the source code. For the above example, you could write:

INSERT REPORT 'ZDYN1' FROM CODE.

or

DATA REP(8).

REP = 'ZDYN1'

INSERT REPORT REP FROM CODE.

From the initial screen of the ABAP Editor, you can access and change all components of the new program.

You can call the ABAP Editor for the above example:

 

If a program with name <prog> already exists, the source code of that program is replaced without warning with a new one from internal table <itab>.

Therefore, you have to be careful when assigning names to programs created dynamically. This is especially true if the program that creates the new program is simultaneously used by multiple users. In this case, employ a user-specific naming convention to ensure that one user does not replace the program of another user.

The GENERATE SUBROUTINE POOL statement that creates temporary external subroutines in the main memory is an alternative option of generating dynamic programs (see Creating and Starting Temporary Subroutines).