Tips and Tricks for the ABAP® Programming Language

Y9030005: EXPORT of Data to table INDX

A small data base application with “IMPORT”, “EXPORT” and “DELETE”. The following 3 reports belong together. It should be demonstrated that heterogeneous data e.g. from a subsystem, can be relatively simple transferred to the R/3(r) system and later administrated there.

Any data list (e.g. addresses, birthday, hobbies and so forth) is complemented with date and time and saved as a data cluster in the table INDX with the command “EXPORT …” . Through “IMPORT …” the data saved in the INDX are selected and displayed again. Here, any period of time can be selected on the basis of the time axis (DATE, TIME). Via “DELETE …” the possibility exists to delete no more required data from the respective cluster. The examples only use the field “TEXT1” as “useful information”, any other data contents can be used alternatively here in the reports.

Parameters:
The total keyword in the INDX consists of 22 bytes (in release 3.1), where 20 bytes are used. A time window can be used for the precise selection of an up/to area:

    • MOVE P_KDNR TO KEY_VON+0(6).    “e.g. customer number
    • MOVE P_DATVON TO KEY_VON+6(8).  “Date of
    • MOVE P_TIMVON TO KEY_VON+14(6). “Time of

ABAP-Source-Code

You can cut and paste the source code directly into the ABAP-Workbench.

REPORT Y9030005 LINE-SIZE 130.         "Release 3.1G, 4.5A
************************************************************************
* Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com
*
*     You can use or modify this report for your own work as long
*               as you don't try to sell or republish it.
*      In no event will the author be liable for indirect, special,
*        Incidental, or consequental damages (if any) arising out of
*                       the use of this report.
*
************************************************************************
 MOVE: 'TESTREPORT for "SELECT * FROM etab" with "EXPORT"  '
        TO SY-TITLE.
************************************************************************
*
PARAMETERS: P_KDNR(6) TYPE C DEFAULT '100007',           "= customer ID
*................Time line from.........................................
            P_DATVON TYPE D    DEFAULT '19960101',       "jhjjmmtt !!
            P_TIMVON TYPE T    DEFAULT '000001',
*................Time line until........................................
            P_DATBIS TYPE D    DEFAULT '20011231',       "jhjjmmtt !!
            P_TIMBIS TYPE T    DEFAULT '234500',
*................Only text examples.....................................
            P_EXPORT TYPE C    DEFAULT 'J',
            TEXT1(40) TYPE C DEFAULT
               'That should be only be a T e s t'.
************************************************************************
*
TABLES: INDX.
************************************************************************
*--DB-area and work area must be of the same structure           ------*
*   e.g. fields,  field string, table
DATA: BEGIN OF TEXTINDX,               "DB-area of INDX
        UZEIT LIKE SY-UZEIT,           " =  6 Bytes
        TEXT(80) TYPE C,               " = 80 Bytes
      END OF TEXTINDX.
*.......................................................................
DATA: BEGIN OF INP1,                   "work area
        UZEIT LIKE SY-UZEIT,           " =  6 Bytes
        TEXT(80) TYPE C,               " = 80 Bytes
      END OF INP1.
************************************************************************
*.......................................................................
DATA: BEGIN OF KEY_VON,                "key CT-CLUSTER of
      TEXTVON(22),
      END OF KEY_VON.
*.......................................................................
DATA: BEGIN OF KEY_BIS,                "key CT-CLUSTER up to
      TEXTBIS(22),
      END OF KEY_BIS.
*............... INDX has a primary key of 22 digits....................
DATA: KEY_LOW(22) VALUE '0000000000000000000000'.
DATA: KEY_HIGH(22) VALUE '9999999999999999999999'.
*.......................................................................
DATA: BEGIN OF IMPKEY,                          "Import key INDX
        SRTFD LIKE INDX-SRTFD,
        SRTF2 LIKE INDX-SRTF2,
      END OF IMPKEY.
*................. Clear speration of the keys would be better..........
DATA: BEGIN OF EXPKEY,                          "Export key INDX
        SRTFD LIKE INDX-SRTFD,
        SRTF2 LIKE INDX-SRTF2,
      END OF EXPKEY.
************************************************************************
******************      work fields                      ***************
 DATA: INDX_VORHANDEN(1) TYPE N.               "1 = content available
************************************************************************
*//////////////////////////////////////////////////////////////////////*
*************               Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
 END-OF-SELECTION.
*#######################################################################
* 1. In the database "INDX" data (or text) up to a length of today
*    2886 bytes per key can be inserted
* 2. The key length of the DB "INDX" is 22 chars in total
*    which can be expanded with an the addition field (INDX-SRTF2)
*#######################################################################

 PERFORM KEY-FUELLEN-INDX-LESEN.
 PERFORM LESEN-INDX.
*................. If a dataset should be exported     .................
 IF P_EXPORT EQ 'J'.
    PERFORM KEY-FUELLEN-INDX-EXPORT.
    PERFORM UEBERNAHME-TEXT.
    PERFORM EXPORT-INDX.
*#######################################################################
*  The generated data will now transfered to the INDX
*  The data wihtin int cluster can be organized in a variable way.
*  Important: The EXPORT and the IMPORT command must use the
*  same structure.
*#######################################################################
 ENDIF.
*
************************************************************************
************************************************************************
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
*
************************************************************************
*               Format the key to read from the INDX                   *
************************************************************************
 FORM KEY-FUELLEN-INDX-LESEN.            " fill the KEY of INDX
*
*..........    Fill key    .............................................
      MOVE KEY_LOW TO KEY_VON.                "all 0
      MOVE P_KDNR    TO KEY_VON+0(6).     "Text number acc. to parameter
      MOVE P_DATVON    TO KEY_VON+6(8).      "Date of  acc. to parameter
      MOVE P_TIMVON    TO KEY_VON+14(6).     "Time of  acc. to parameter
*.......................................................................
      MOVE KEY_HIGH TO KEY_BIS.               "all 9
      MOVE P_KDNR    TO KEY_BIS+0(6).         "Text number (s.o.)
      MOVE P_DATBIS    TO KEY_BIS+6(8).       "Date of acc. to parameter
      MOVE P_TIMBIS    TO KEY_BIS+14(6).      "Time of acc. to parameter
*
 ENDFORM.

************************************************************************
*-            THE FILE INDX WILL BE READ SEQUENTIELL                  -*
************************************************************************
FORM LESEN-INDX.
*......................................................................*
*
  SELECT * FROM INDX
     WHERE RELID EQ 'YT'
           AND SRTFD BETWEEN KEY_VON
                     AND     KEY_BIS
           ORDER BY PRIMARY KEY.                  "ascending please
*                 Commit work.         "if mass data should be processed
*
     MOVE INDX-SRTFD TO IMPKEY-SRTFD .
     MOVE INDX-SRTF2 TO IMPKEY-SRTF2 .
*#######################################################################
     PERFORM LESENINDX-IMPORT.
*#######################################################################
     IF INDX_VORHANDEN EQ 1.
        CLEAR INDX_VORHANDEN.
        PERFORM DRUCKEN-INDX.                  "print out the found text
     ELSE.
     ENDIF.
*
  ENDSELECT.
 IF INDX-SRTFD IS INITIAL.
    WRITE: /1 'No data in INDX available, RELID/Key: YT'.
    WRITE: /10 'KEY_VON:', KEY_VON, 'KEY_BIS:', KEY_BIS.
 ENDIF.
*
ENDFORM.
************************************************************************
*                   Read the Clusters YT of the  DB INDX               *
************************************************************************
FORM LESENINDX-IMPORT.
*
  IMPORT TEXTINDX TO INP1 FROM DATABASE INDX(YT) ID IMPKEY.
  IF SY-SUBRC EQ 0.
     INDX_VORHANDEN = 1.
  ELSE.
    PERFORM FEHLER-BEIM-IMPORT.
  ENDIF.
*
ENDFORM.
************************************************************************
*   Error message, if the Cluster YT is not in INDX                    *
*   .. although Index set display it. (= DB not consistent)
************************************************************************
FORM FEHLER-BEIM-IMPORT.
*= rare case
     WRITE: / '*****************************************************'.
     WRITE: / 'ATTENTION, ERROR THROUGH READING INDX IN  CLUTSER YT:'.
     WRITE: / '         KEY: ', IMPKEY-SRTFD, IMPKEY-SRTF2.
     WRITE: / '         DB INDX (Cluster YT) not consistent '.
     WRITE: / '*****************************************************'.
     STOP.

ENDFORM.
************************************************************************
*           Print (not prepared) form Cluster YT out of INDX           *
************************************************************************
 FORM DRUCKEN-INDX.
*
    WRITE: /1  'Key:', INDX-SRTFD COLOR 5.
    WRITE: /1  'Data value:', INP1-UZEIT, INP1-TEXT.
    ULINE.
 ENDFORM.
************************************************************************
*              Create key for the  Export in INDX                      *
************************************************************************
FORM KEY-FUELLEN-INDX-EXPORT.  "Determine key for EXPORT
*
      MOVE KEY_LOW TO EXPKEY-SRTFD.       "all 0
      MOVE P_KDNR    TO EXPKEY+0(6).      "text number from parameter
      MOVE SY-DATUM    TO EXPKEY+6(8).    "todays date
      MOVE SY-UZEIT    TO EXPKEY+14(6).   "current time
      MOVE 0 TO EXPKEY-SRTF2.             "additional key Zusatzkey is 0
*.......................................................................

ENDFORM.
************************************************************************
*           Arbitrary text can be placed in the data area ...          *
************************************************************************
FORM UEBERNAHME-TEXT.
*
   MOVE SY-UZEIT TO INP1-UZEIT.
   MOVE TEXT1  TO INP1-TEXT.
*
ENDFORM.
************************************************************************
*        EXPORT OF INP1 OVER TEXTINDX IN DB INDX (YT)              *
************************************************************************
 FORM EXPORT-INDX.
*
  LOCAL INDX.  "The EXPORT changes only the local copy of INDX.
               "After the form is left, the origin will be reestablished
               "The source key will be used for a possible forward
               "reading with SELECT.
*.......................................................................
   MOVE INP1 TO TEXTINDX.
   EXPORT TEXTINDX TO DATABASE INDX(YT) ID EXPKEY.
*.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .*
   IF SY-SUBRC NE 0.
      WRITE: / 'Attention, Error on EXPORT in INDX'.
      WRITE: / 'KEY:', EXPKEY.
   ELSE.
      WRITE: / 'The export was carried out:' COLOR 6.
      PERFORM DRUCKEN-INDX.
   ENDIF.
*......................................................................*
*
ENDFORM.