ERP Database - The Unofficial ERP Knowledge Base

Facebook Twitter del.icio.us Digg it
ERP Database contains a huge collection of articles related to ERP System and Software. Many of the articles are specifically related to SAP. All these ERP/SAP articles are freely available to everyone.

If you would like to submit an article or share any document related to ERP or any specific ERP software. Please mail it to support@erpdb.info. Please make sure that the documents are not copyrighted.
Social Bookmarks:

ABAP Program : Working with Temporary Programs

Print This Post Email This Post Written by admin on Jan 30th, 2008 | Filed under: ABAP Programs, Tips & Tricks

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.



Share

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

  1. Generate Temporary ABAP Program 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. ABAP Programs: Working with get events REPORT ZSOURCE1504. * Work areas TABLES BOOKINGS. * Reading data GET BOOKINGS. WRITE / BOOKINGS-FLDATE....
  3. ABAP Programs: Working with tables from the Dictionary REPORT ZSOURCE0501. * Declaration of a work area for a Dictionary tableTABLES CUSTOMERS.* Reading all entries of the database table and displaying each entrySELECT * FROM CUSTOMERS. WRITE: / CUSTOMERS-NAME.ENDSELECT.———————ABAPer,...
  4. ABAP Programs: Working with Field symbols REPORT CHAP2401. * Defining a Field Symbol FIELD-SYMBOLS < FS>. * Variable for later use DATA FIELD VALUE 'X'. * Assigning a field to a Field Symbol ASSIGN FIELD TO...
  5. ABAP Programs: Working with hide statements REPORT ZSOURCE1704. * work area TABLES CUSTOMERS. * Internal table DATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE. * Processing data START-OF-SELECTION. SELECT * FROM CUSTOMERS INTO TABLE ALL_CUSTOMERS....
  6. ABAP Programs: Working with Select-Options REPORT ZSOURCE1602. * Work area TABLES CUSTOMERS. * Defining Select-Options SELECT-OPTIONS S_NAME FOR CUSTOMERS-NAME. SELECT * FROM CUSTOMERS WHERE NAME IN S_NAME. WRITE / CUSTOMERS-NAME. ENDSELECT....
  7. ABAP Programs:Sample dialog program (flight reservation) * This program source contains all modules and subroutines of the * flight reservation program, but screen and GUI status definitions * are not included. *&———————————————————————* *&———————————————————————* *& Include MSABBTOP...
  8. 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 OLE2_OBJECT....
  9. Working with BADI’s in SAP ABAP Business addins are the enhancements to standard SAP’s version. They can be inserted to accommodate the business requirements.  Here are the steps in working with a custom BADI inside SAP....
  10. 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...
  11. 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...
  12. Create a Program in SAP ABAP You can navigate to the ABAP Editor through the Menu. You can also create favorites for certain transactions and put it into your custom menu. You can type SE38 from...
  13. ABAP Program to Send Mail with Attachment This ABAP program use the function module ‘SO_NEW_DOCUMENT_ATT_SEND_API1′ to send the email with attachment. There are five steps involved in sending the email with attachment. Add Recipients Put in the...
  14. ABAP Program:Reading data from a file REPORT ZSOURCE2602. * Data declarations for later use TABLES CUSTOMERS. PARAMETERS FILENAME(128) DEFAULT '/usr/tmp/testfile.dat' LOWER CASE. DATA: MSG_TEXT(50), ALL_CUSTOMER_NAMES LIKE CUSTOMERS-NAME OCCURS 100 WITH HEADER LINE. * Opening the File...
  15. ABAP Program:Transferring data to a file (presentation server) REPORT ZSOURCE2603. * Data declarations for later use PARAMETERS FILENAME(128) DEFAULT 'c:usersdefaulttestfile.dat' LOWER CASE. TABLES CUSTOMERS. DATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE. * Get data for file...



One Response to “ABAP Program : Working with Temporary Programs”

  1. Wow…. great post. Keep up the good work man.

Leave a Reply