1. 程式人生 > >通過例子學習ABAP(四)--INSERT語句對於三種類型的內表不同效果

通過例子學習ABAP(四)--INSERT語句對於三種類型的內表不同效果

(1)  通過索引插入行: INSERT LINE INTO ITAB INDEX IDX.

(2)一般性的插入單行:INSERT [LINE INTO | INITIAL LINE INTO ] TABLE ITAB.

語句中增加了TABLE關鍵字,對於不同型別的內表,其意義和用法是有區別的:
1.對於標準表而言,和APPEND LINE TO ITAB。的結果一樣。

2.對於排序表而言,插入行不可以打亂關鍵字的排序順序,否則報錯。

3.對於雜湊表而言,插入過程中系統按照關鍵字對行進行定位。

例如:

REPORT demo_int_tables_insert .

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            “替換成 STANDARD 和HASHED ,看看有什麼不同。
          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.