Intro to ABAP - Chapter 07a

Document Sample
Intro to ABAP - Chapter 07a Powered By Docstoc
					           Processing Internal Tables




BC170_2.07.1
                        Objectives
• The participants will be able to:
      – Process an Internal Table
      – Use Control Level Processing on Internal Tables
      – Describe the following commands:
               • BINARY SEARCH
               • DELETE … WHERE
      – Copy an Internal Table’s data to a new Internal
        Table
      – Define the various Internal Table types available in
        SAP
BC170_2.07.2
            Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
                                                       This LOOP AT IT_EMPTAB
           COUNTRY     LIKE EMPLOYEE-COUNTRY,
                                                         statement allows for a
           NAME1       LIKE EMPLOYEE-NAME1,
                                                        logical expression in a
           SALES       LIKE EMPLOYEE-SALES,
                                                       WHERE clause to limit the
       END OF EMP.
                                                       processing of the internal
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP INITIAL
                                                                 table.
SIZE 10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
  MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.               If no internal table
  APPEND IT_EMPTAB.                                    entries qualify under the
ENDSELECT.                                              logical expression, the
LOOP AT IT_EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’.     statement within the
  WRITE: / IT_EMPTAB-COUNTRY, IT_EMPTAB-NAME1,         loop is not executed and
  IT_EMPTAB-SALES.
                                                         SY-SUBRC is set to 4.
ENDLOOP.
IF SY-SUBRC NE 0. WRITE: / ‘NO ENTRIES’. ENDIF.


 BC170_2.07.3
                   System Field SY-TABIX
REPORT Y170DM46.
TABLES: EMPLOYEE.


TYPES: BEGIN OF EMP,
           COUNTRY     LIKE EMPLOYEE-COUNTRY,
           NAME1       LIKE EMPLOYEE-NAME1,
       END OF EMP.
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
                                                           screen output
PARAMETERS:            P_START LIKE SY-TABIX DEFAULT 10,
                       P_END LIKE SY-TABIX DEFAULT 20.
SELECT * FROM EMPLOYEE.
  MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
  APPEND IT_EMPTAB.
ENDSELECT.
                                                            SY-TABIX
LOOP AT IT_EMPTAB FROM START TO END.
  WRITE: / SY-TABIX, IT_EMPTAB-COUNTRY, IT_EMPTAB-NAME1.
ENDLOOP.

  BC170_2.07.4
               Accumulating Data within an
                    Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE.                                COLLECT <EMPTAB>.
TYPES: BEGIN OF EMP,                              Country   Sales
                                                                       Header
         COUNTRY LIKE EMPLOYEE-COUNTRY,            D        400,000
                                                                        Line
         SALES     LIKE EMPLOYEE-SALES,            USA    1,000,000
       END OF EMP.                                 GB       500,000
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
                                                   D      7,800,000

                                                           screen output
SELECT * FROM EMPLOYEE.                                  A        371,065.00
 MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.               CH        45,305.00
  COLLECT IT_EMPTAB.                                     D      8,200,000.00
ENDSELECT.                                               F              0.00
LOOP AT IT_EMPTAB.
                                                         GB       500,000.00
                                                         NL       577,000.00
  WRITE: / IT_EMPTAB-COUNTRY, IT_EMPTAB-SALES.           NO           234.00
ENDLOOP.                                                 USA    1,000,000.00
                                                         HK             0.00


    BC170_2.07.5
                Sorting an Internal Table
REPORT Y170DM44.
TABLES: EMPLOYEE.                            Sorting options:
TYPES: BEGIN OF EMP,
                                             1) SORT IT_EMPTAB - sorts
         COUNTRY LIKE EMPLOYEE-COUNTRY,         the entries of the internal
         NAME1      LIKE EMPLOYEE-NAME1,        table IT_EMPTAB in
         SALES      LIKE EMPLOYEE-SALES,        ascending order.
        END OF EMP.
                                             2) SORT IT_EMPTAB BY
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP      <field> - sorts the table on
          INITIAL SIZE 10 WITH HEADER LINE.
                                                one or more fields within
SELECT * FROM EMPLOYEE.                         the table.
  MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
  APPEND IT_EMPTAB.
ENDSELECT.
SORT IT_EMPTAB BY SALES DESCENDING.              screen output
LOOP AT IT_EMPTAB.
  WRITE: / IT_EMPTAB-COUNTRY, IT_EMPTAB -NAME1,
         IT_EMPTAB -SALES.
ENDLOOP.
 BC170_2.07.6
               Control Level Processing

• AT FIRST
• AT NEW < field >
• AT END OF < field >
• AT LAST




BC170_2.07.7
          Reading a Single Table Entry
               REPORT Y170DM47.
               TABLES: EMPLOYEE.
               TYPES: BEGIN OF EMP,
                   COUNTRY LIKE EMPLOYEE-COUNTRY,
                   NAME1    LIKE EMPLOYEE-NAME1,
                   END OF EMPTAB.


               DATA: IT_EMPTAB TYPE STANDARD TABLE OF
               EMP INITIAL SIZE 10 WITH HEADER LINE.


               SELECT * FROM EMPLOYEE.
                MOVE-CORRESPONDING EMPLOYEE TO
                   IT_EMPTAB.
                 APPEND IT_EMPTAB.
               ENDSELECT.


               READ TABLE ….
BC170_2.07.8
        Reading a Single Table Entry -
                  Options
   READ TABLE <ITAB> options:
   1) READ TABLE <ITAB >.
   2) READ TABLE <ITAB > WITH KEY <k1> = <v1>…
                                     <kn> = <vn>.
   3) READ TABLE <ITAB > WITH TABLE KEY <k1> = <v1> ...
                                     <kn> = <vn>.
   4) READ TABLE <ITAB > WITH KEY = <value>.
   5) READ TABLE <ITAB > WITH KEY . . . BINARY SEARCH.
   6) READ TABLE <ITAB > INDEX <i>.
   7) READ TABLE <ITAB > COMPARING <f1> <f2> . . . .
   8) READ TABLE <ITAB > COMPARING ALL FIELDS.
   9) READ TABLE <ITAB > TRANSPORTING <f1> <f2> . . . .
   10) READ TABLE <ITAB > TRANSPORTING NO FIELDS.


BC170_2.07.9
                  Maintaining Internal Tables
SELECT * FROM EMPLOYEE.
  MOVE-CORRESPONDING EMPLOYEE TO
IT_EMPTAB.
  APPEND IT_EMPTAB.                       INSERT <ITAB> INDEX <i>.
ENDSELECT.                                MODIFY <ITAB> INDEX <i>.
                                          DELETE <ITAB> INDEX <i>.
READ TABLE IT_EMPTAB INDEX 1.
MOVE ‘ABC’ TO IT_EMPTAB-NAME1.
MODIFY IT_EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
   WRITE / ‘Attempt to modify failed.’.
ELSE.                                       Check SY-SUBRC after
    WRITE: / IT_EMPTAB-COUNTRY,             every attempt to change
IT_EMPTAB-NAME1.                            an internal table entry.
ENDIF.
INSERT IT_EMPTAB INDEX 1.
DELETE IT_EMPTAB INDEX SY-TABIX.


  BC170_2.07.10
        Working with an Internal Table
           without a Header Line
                 APPEND <work area> TO <internal table>.


            COLLECT <work area> INTO <internal table>.


                INSERT <work area> INTO <internal table>.


                MODIFY <internal table> FROM <work area>.


         READ TABLE <internal table> INTO <work area>.


                LOOP AT <internal table> INTO <work area>.
BC170_2.07.11
                    Deleting an Internal Table

CLEAR <internal table>
• Initializes the
  header line.
                         REFRESH <internal table>
• Internal table lines • Deletes all
  remain unchanged. table lines.
                                                 FREE <internal table>
                         • Storage space
                           is not released.      • Deletes all
                                                   table lines.
                         • Paging is released.
                                                 • Storage space
                         • Header line             is released.
                           remains unchanged.
                                                 • Header line
                                                   remains
                                                   unchanged.
    BC170_2.07.12
         Information about an Internal Table
REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
             COUNTRY LIKE EMPLOYEE-COUNTRY,
             NAME1       LIKE EMPLOYEE-NAME1,
         END OF EMP,
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
         LINE_COUNT TYPE I,
                                              DESCRIBE TABLE <internal table>
         INITIAL_COUNT TYPE I.                      LINES <var1>
SELECT * FROM EMPLOYEE.                             OCCURS <var2>.
  MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
   APPEND IT_EMPTAB.
ENDSELECT.
DESCRIBE TABLE IT_EMPTAB
         LINES LINE_COUNT
         OCCURS INITIAL_COUNT.                           screen output
WRITE: / ‘ lines:’, LINE_COUNT,
          / ‘occurs:’, INITIAL _COUNT.

 BC170_2.07.13
                Calling the SAP Table Editor
REPORT Y170DM50.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
        COUNTRY       LIKE EMPLOYEE-COUNTRY,
        NAME1         LIKE EMPLOYEE-NAME1,
     END OF EMP,
DATA: IT_EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
SELECT * FROM EMPLOYEE.
  MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
  APPEND IT_EMPTAB.
ENDSELECT.
EDITOR-CALL FOR IT_EMPTAB.
CHECK SY-SUBRC EQ 0.
LOOP AT IT_EMPTAB WHERE NAME1 EQ ‘Maurice Cheeks’.
                                                     screen output
   WRITE: / IT_EMPTAB-COUNTRY, IT_EMPTAB-NAME1.
ENDLOOP.
IF SY-SUBRC NE 0. WRITE: / ‘No records.’. ENDIF.

     BC170_2.07.14
                 The Binary Search


    READ TABLE itab WITH KEY key BINARY SEARCH




BC170_2.07.15
                Deleting Records with WHERE


          LOOP AT ITAB WHERE FLDATE = ‘19970101’.
          DELETE ITAB.
          ENDLOOP.




          DELETE ITAB WHERE FLDATE = ‘19970101’.




BC170_2.07.16
                    Copying Internal Tables


 SELECT * FROM KNB1 INTO TABLE ITAB1.
 LOOP AT ITAB1.
 ITAB2 = ITAB1.
 APPEND ITAB2
 ENDLOOP.



                SELECT * FROM KNB1 INTO TABLE ITAB1.
                ITAB2[] = ITAB1[].


BC170_2.07.17
                          Summary
• The participants should now be able to:
     – Process an Internal Table
     – Use Control Level Processing on Internal Tables
     – Describe the following commands:
                • BINARY SEARCH
                • DELETE … WHERE
     – Copy an Internal Table’s data to a new Internal Table
     – Define the various Internal Table types available in
       SAP
BC170_2.07.18

				
DOCUMENT INFO
Shared By:
Stats:
views:7
posted:12/3/2012
language:Unknown
pages:18
Description: sap abap introduction, very specific,welcome download
201212 29 201212 29
About download professional profile