Tips and Tricks for the ABAP® Programming Language

Y9020011: Constructions of an internal table with “… SORTED BY v2”

The internal table ITAB1 is declared with a OCCURS specification of 10 elements. If the number of existing table lines is greater than 10 lines, additional memory space is provided. If the addition ” … SORTED BY v2″ is applied in the APPEND command, no automatic expansion occurs. The table size is fixed according to the OCCURS specification.

“FORM AUFBAU-APPEND-ITAB1”
In the DO-loop, the table header line is generated 13 times and inserted with “APPEND ITAB1 SORTED BY XFELD” into the data base. A maximum of 10 table lines are made available within the table ITAB1. The smaller key term at a given time in “v2” is overwritten. Also in case of identical keys, the smaller key is removed.

ABAP-Source-Code

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

REPORT Y9020011 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.
*
************************************************************************
 BREAK-POINT.
 MOVE: 'Creating of an internal table with ''APPEND itab SORTED BY..'' '
        TO SY-TITLE.
************************************************************************
***************         Internal tables           **********************
*//////////////////////////////////////////////////////////////////////*
DATA: BEGIN OF ITAB1 OCCURS 10,
        XFELD(3) TYPE N,                "this should be sorted ascending
        YFELD(10) TYPE C,
        CFELD(15) TYPE N,
      END OF ITAB1.
*//////////////////////////////////////////////////////////////////////*
*************            -  Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
 PERFORM AUFBAU-APPEND-ITAB1.
 PERFORM DISPLAY-ITAB1.
*
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
*
*
************************************************************************
*      Creating of the table  (OCCURS 10) with a do loop  (DO 15 TIMES)
************************************************************************
 FORM AUFBAU-APPEND-ITAB1.
*
 DATA: Z_FELD(10) TYPE N.
 FIELD-SYMBOLS: .
*.......................................................................
  WRITE: /5 'Step 1: Creating the internal table ITAB1 ' COLOR 5.
  WRITE: /5 '                13 lines will be inserted:' COLOR 6.
  ULINE. SKIP 2.
*.......................................................................
  MOVE '123'      TO ITAB1-XFELD.
  MOVE ' ARTIKEL-'  TO ITAB1-YFELD.
  GET RUN TIME FIELD Z_FELD.
*.......................................................................
  DO 13 TIMES.
     IF SY-INDEX LE 8.
        ADD 10 TO ITAB1-XFELD.
     ENDIF.
     GET RUN TIME FIELD Z_FELD.
     ADD Z_FELD TO ITAB1-CFELD.
     ASSIGN SY-ABCDE+SY-INDEX(1) TO .       "look here while debug!
     MOVE  TO ITAB1-YFELD+9(1).
*#######################################################################
     APPEND ITAB1 SORTED BY XFELD.
*#######################################################################
     WRITE: /1 ITAB1-XFELD,
             5 ITAB1-YFELD,
            17 ITAB1-CFELD.
*
  ENDDO.
*
 ENDFORM.
************************************************************************
*                  Display the internal table ITAB
************************************************************************
 FORM DISPLAY-ITAB1.
*
  ULINE. SKIP 2.
  WRITE: /5 'Step 2: The table ITAB only consists of 10 lines:' COLOR 5.
  ULINE. SKIP 2.
*.......................................................................
 LOOP AT ITAB1.
  WRITE: /1 ITAB1-XFELD,
          5 ITAB1-YFELD,
         17 ITAB1-CFELD.
 ENDLOOP.
*
 ENDFORM.
************************************************************************
************************************************************************
******************* END OF PROGRAM *************************************