ABAP 内表总结-3 内表增删改查

(三) 内表在行记录上的操作——增删改查

1、Inserting Several Lines

 

 

 

 

DATA: BEGIN OF LINE,
        LAND(3)  TYPE C,
        NAME(10) TYPE C,
        AGE      TYPE I,
        WEIGHT   TYPE P DECIMALS 2,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE
          WITH NON-UNIQUE KEY LAND NAME AGE WEIGHT.

LINE-LAND = 'G'.   LINE-NAME   = 'Hans'.
LINE-AGE  = 20.    LINE-WEIGHT = '80.00'.
INSERT LINE INTO TABLE ITAB.

LINE-LAND = 'USA'. LINE-NAME   = 'Nancy'.
LINE-AGE  = 35.    LINE-WEIGHT = '45.00'.
INSERT LINE INTO TABLE ITAB.

LINE-LAND = 'USA'. LINE-NAME   = 'Howard'.
LINE-AGE  = 40.    LINE-WEIGHT = '95.00'.
INSERT LINE INTO TABLE ITAB.

LINE-LAND = 'GB'.  LINE-NAME   = 'Jenny'.
LINE-AGE  = 18.    LINE-WEIGHT = '50.00'.
INSERT LINE INTO TABLE ITAB.

LINE-LAND = 'F'.   LINE-NAME   = 'Michele'.
LINE-AGE  = 30.    LINE-WEIGHT = '60.00'.
INSERT LINE INTO TABLE ITAB.

LINE-LAND = 'G'.   LINE-NAME   = 'Karl'.
LINE-AGE  = 60.    LINE-WEIGHT = '75.00'.
INSERT LINE INTO TABLE ITAB.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-LAND, LINE-NAME, LINE-AGE, LINE-WEIGHT.
ENDLOOP.

 

F   Michele            30         60.00
G   Hans               20         80.00
G   Karl               60         75.00
GB  Jenny              18         50.00
USA Howard             40         95.00
USA Nancy              35         45.00

 

The example fills a sorted internal table with six entries.

 

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA: ITAB LIKE STANDARD TABLE OF LINE,
      JTAB LIKE SORTED TABLE OF LINE
           WITH NON-UNIQUE KEY COL1 COL2.

DO 3 TIMES.
  LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
  LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 3.
  APPEND LINE TO JTAB.
ENDDO.

INSERT LINES OF ITAB INTO TABLE JTAB.

LOOP AT JTAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

 

         1         1          1
         2         1          1
         3         2          4
         4         2          8
         5         3          9
         6         3         27

The example creates two internal tables with the same line type but different table types. Each is filled with three lines. Then, ITAB is sorted into the sorted table JTAB.

 

 

 

 

2、Appending Summarized Lines  

 

 

The following statement allows you to summate entries in an internal table:

COLLECT <wa> INTO <itab>.

<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type.

When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as

 

 inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

 

 Example

 

 

 

 

 

DATA: BEGIN OF LINE,
        COL1(3) TYPE C,
        COL2(2) TYPE N,
        COL3    TYPE I,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE
          WITH NON-UNIQUE KEY COL1 COL2.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
ENDLOOP.

 

 

 

 

 

 

The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECT statement, the first line of ITAB is modified. The following diagram shows the three steps:

 

 

 

 

3.Reading Lines of Tables  

 

 

 

 

 

 

 

4.Changing Lines 

 

 

To change a single line, use the following statement:

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f 2> ...].

The work area <wa>, which must be compatible with the line type of the internal table, plays a double role in this statement. Not only it is used to find the line that you want to change, but it also contains the new contents. The system searches the internal table for the line whose table key corresponds to the key fields in <wa>.

 

To change one or more lines using a condition, use the following statement:

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f 2> ... WHERE <cond>.

 

The work area <wa>, which must be compatible with the line type of the internal table, contains the new contents, which will be assigned to the relevant table line using the TRANSPORTING addition. Unlike the above MODIFY statement, the TRANSPORTING addition is not optional here. Furthermore, you can only modify the key fields of the internal table if it is a standard table. If at least one line is changed, the system sets SY-SUBRC to 0, otherwise to 4.

 

 

Examples

 

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL1 = 2. LINE-COL2 = 100.

MODIFY TABLE ITAB FROM LINE.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         2      100
         3        9
         4       16

The program fills a hashed table with a list of square numbers. The MODIFY statement changes the line of the table in which the key field COL1 has the value 2.

 

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL2 = 100.

MODIFY ITAB FROM LINE TRANSPORTING COL2 WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         2      100
         3      100
         4       16

The program fills a hashed table with a list of square numbers. The MODIFY statement changes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.

 

 

 

 

 5.Deleting Lines 

 

 

 

A.Deleting a Line Using the Table Key

 

To use the table key of table <itab> as a search key, use one of the following statements:

 

DELETE TABLE <itab> FROM <wa>.

or

DELETE TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n>.

In the first case, <wa> must be a work area compatible with the line type of <itab>. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. If you do not know the name of one of the key fields until runtime, you can specify it as the content of a field <n

i > using the form (<n i >) = <f i >. If the data types of <f i > are not compatible with the key fields, the system converts them.

 

B.Deleting Several Lines Using a Condition

 

To delete more than one line using a condition, use the following statement:

DELETE <itab> WHERE <cond>.

 

C.Deleting Adjacent Duplicate Entries

 

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>
                                  [COMPARING <f1> <f 2> ...
                                             |ALL FIELDS].

The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria.

 

Examples

 

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL1 = 1.

DELETE TABLE ITAB: FROM LINE,
WITH TABLE KEY COL1 = 3.

LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         2        4
         4       16

The program fills a hashed table with a list of square numbers. The DELETE statement delete the lines from the table where the key field COL1 has the contents 1 or 3.

 

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

DELETE ITAB WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         4       16

The program fills a hashed table with a list of square numbers. The DELETE statement deletes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.

 

DATA OFF TYPE I.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE C,
END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE
          WITH NON-UNIQUE KEY COL2.

LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 2. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 3. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 4. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 5. LINE-COL2 = 'A'. APPEND LINE TO ITAB.

OFF = 0. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS.

OFF = 14. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING COL1.

OFF = 28. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB.

OFF = 42. PERFORM LIST.

FORM LIST.
  SKIP TO LINE 3.
  LOOP AT ITAB INTO LINE.
     WRITE: AT /OFF  LINE-COL1, LINE-COL2.
  ENDLOOP.
ENDFORM.

The output is:

         1 A          1 A          1 A          1 A

         1 A          1 B          2 B          2 B

         1 B          2 B          3 B          5 A

         2 B          3 B          4 B

         3 B          4 B          5 A

         4 B          5 A

         5 A

The example creates and fills a standard table. Here, the first DELETE statement deletes the second line from ITAB because the second line has the same contents as the first line. The second DELETE statement deletes the second line from the remaining table because the contents of the field COL1 is the same as in the first line. The third DELETE statement deletes the third and fourth line from the remaining table because the contents of the default key field COL2 are the same as on the second line. Although the contents of the default key are the same for the first and the fifth line, the fifth line is not deleted because it is not adjacent to the first line.

 

 

6.Processing Table Entries in Loops  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 转自:http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb362c358411d1829f0000e829fbfe/content.htm

 

 

 

 

 

 

 

 

INSERT LINES OF <itab1> [FROM <n1>][TO <n2>] INTO TABLE <itab2>.

你可能感兴趣的:(table,delete,System,output,Components,Duplicates)