To create a new dynamic program at runtime of an ABAP program, you must use an internal table. For this purpose, create the internal table with a single column of type character and a row width of 72. You can use any process described underFilling Internal Tables to write the source code of your new program into the internal table. You can use internal fields, whose contents depend on the flow of the program you use to create the new program. The following example shows how you can carry this out:
DATA: code TYPE TABLE OF rssource-line.
* type of an editor line: rssource-line
APPEND 'REPORT ZDYN1.'
APPEND 'WRITE / ''Hello, I am dynamically created!''.'
Two lines of a simple program are added to the internal table CODE.
Now you must transfer the new module - in this case, an executable program (report) - into the program library. Use the following statement:
INSERT REPORT <prog> FROM <itab>.
If a program with this name does not already exist, it is created with the following attributes:
The new program <prog> is stored persistently on the database but is not assigned to a package. It is therefore not affected by the transport system.
If a program with the name <prog> already exists, its source code is replaced with a new one from the internal table <itab> without prior warning. All other attributes remain intact. Therefore, you must be very careful when naming dynamically generated programs.
You can explicitly enclose the name of the program <prog> in single inverted commas or specify a character field that contains the name. The program name does not have to correspond with the name in the source code, but it is advisable. <itab> is the internal table that contains the source. Hence, for the above example you can write:
INSERT REPORT 'ZDYN1' FROM code.
DATA rep(8) TYPE c.
rep = 'ZDYN1'
INSERT REPORT rep FROM code.
In the ABAP Editor initial screen, you can call and change all components of the new program.
You can call the ABAP Editor for the above example:
Instead of INSERT REPORT you can use the GENERATE SUBROUTINE POOL statement, which creates temporary external subroutines in the main memory (see