SAP Database - The Unofficial SAP Knowledge Base

Free Business and Tech Magazines and eBooks

Generate Temporary ABAP Program

Print This Post Email This Post Written by admin on Sep 10th, 2009 | Filed under: ABAP Programs

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 <output>.           ' TO SOURCE_TABLE,
'form display_table.               ' TO SOURCE_TABLE,
'select * from                     ' TO SOURCE_TABLE,
               F_NAME                TO SOURCE_TABLE,
'.                                 ' TO SOURCE_TABLE,
'  new-line.                       ' TO SOURCE_TABLE,
'  do.                             ' TO SOURCE_TABLE,
'    assign component sy-index     ' TO SOURCE_TABLE,
'           of structure           ' TO SOURCE_TABLE,
            F_NAME                   TO SOURCE_TABLE,
'           to <output>.           ' TO SOURCE_TABLE,
'    if sy-subrc ne 0. exit. endif.' TO SOURCE_TABLE,
'    write <output>.               ' TO SOURCE_TABLE,
'  enddo.                          ' TO SOURCE_TABLE,
'endselect.                        ' TO SOURCE_TABLE,
'endform.                          ' TO SOURCE_TABLE.
ENDFORM.

If you like this post, you may as well like these too:

  1. ABAP Program : Working with Temporary Programs 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...
  2. Program to Generate 3D graphics Simple report to create graph in ABAP using GRAPH_MATRIX_3D function module. The graph shows the performance of 3 companies for the Four quarters of a single year.The Complete code with...
  3. ABAP Program: Sample program for OLE Automation REPORT ZSOURCE2801. * Including OLE types INCLUDE OLE2INCL. * Tables and variables for later use TABLES: CUSTOMERS. DATA: APPLICATION TYPE OLE2_OBJECT, WORKBOOK TYPE OLE2_OBJECT, SHEET TYPE OLE2_OBJECT, CELLS TYPE...
  4. Call an ABAP program from a BSP Call an ABAP program from a BSP is somehow impossible due to memory context handling for a web transaction like a BSP. If you try, it would lead to an...
  5. SAP ABAP Program Types Each ABAP program has a type, which is defined in the program attributes. What is the purpose of all the different program types? The program type determines which processing blocks...



This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

  • Subscribe to RSS

  • Free Ebooks