Ever had a need to create a temporary program during the run time. Here is a simple program which will allow you to learn that.
REPORT ZSOURCE2501.
* Internal table for source code, field for name of temporary program
DATA: SOURCE_TABLE(72) OCCURS 10 WITH HEADER LINE,
PROGRAM_NAME LIKE SY-CPROG.
* Building the source code
APPEND 'report test.' TO SOURCE_TABLE.
APPEND 'form display.' TO SOURCE_TABLE.
APPEND 'write ''I am a temporary program''.' TO SOURCE_TABLE.
APPEND 'endform.' TO SOURCE_TABLE.
* Generating the temporary program
GENERATE SUBROUTINE POOL SOURCE_TABLE NAME PROGRAM_NAME.
* Calling a form externally
PERFORM DISPLAY IN PROGRAM (PROGRAM_NAME).
Now what will you do if you encounter a syntax error? add the following code after GENERATE SUBROUTINE.
* Generating the temporary program
GENERATE SUBROUTINE POOL SOURCE_TABLE NAME PROGRAM_NAME.
IF SY-SUBRC NE 0.
WRITE: / 'Syntax error, message', SYNTAX_CHECK_MESSAGE,
/ 'in line', LINE_NO.
EXIT.
ENDIF.
* Calling a form externally
PERFORM DISPLAY IN PROGRAM (PROGRAM_NAME).
Now, let’s construct a real life example….!
REPORT ZSOURCE2503.
* Variables for later use
PARAMETERS TABNAME(10) DEFAULT 'CUSTOMERS'.
DATA: SOURCE_TABLE(72) OCCURS 100 WITH HEADER LINE,
PROGRAM_NAME LIKE SY-CPROG,
SYNTAX_CHECK_MESSAGE(128),
LINE_NO TYPE I.
* Building the source code
PERFORM BUILD_THE_SOURCE_CODE USING TABNAME.
* Generating the temporary program, checking syntax errors
GENERATE SUBROUTINE POOL SOURCE_TABLE
NAME PROGRAM_NAME
MESSAGE SYNTAX_CHECK_MESSAGE
LINE LINE_NO.
IF SY-SUBRC NE 0.
WRITE: / 'Syntax error, message', SYNTAX_CHECK_MESSAGE,
/ 'in line', LINE_NO.
EXIT.
ENDIF.
* Calling a form externally
PERFORM DISPLAY_TABLE IN PROGRAM (PROGRAM_NAME).
* Form to build the source code of the temporary program
FORM BUILD_THE_SOURCE_CODE USING F_NAME.
APPEND:
'report ztmpprog. ' TO SOURCE_TABLE,
'tables ' TO SOURCE_TABLE,
F_NAME TO SOURCE_TABLE,
'. ' TO SOURCE_TABLE,
'field-symbols
Leave a Reply