从字面看好像作用是相同的,但具体使用以来还是有区别的。如果都是从数据库表中select一次数据,添加到内表中,他们的作用相同;但是多次,他们的作用则有很大差别,INTO TABLE:将数据select出来添加到内表时会将内表先清空再添加,而APPENDING TABLE则会直接追加,实现累计效果。
解释事例:
DATA:BEGIN OF lt_matnr OCCURS 0,
matnr TYPE mara-matnr,
END OF lt_matnr.
WRITE :/ '两次INTO TABLE,内表数据:'.
SELECT matnr UP TO 1 ROWS INTO TABLE lt_matnr FROM mara.
SELECT matnr UP TO 1 ROWS INTO TABLE lt_matnr FROM mara.
LOOP AT lt_matnr.
WRITE:/ lt_matnr-matnr.
ENDLOOP.
REFRESH :lt_matnr.
WRITE :/ '-----------------------------------------------------------'.
WRITE :/ '两次APPENDING TABLE,内表数据:'.
SELECT matnr UP TO 1 ROWS APPENDING TABLE lt_matnr FROM mara.
SELECT matnr UP TO 1 ROWS APPENDING TABLE lt_matnr FROM mara.
LOOP AT lt_matnr.
WRITE:/ lt_matnr-matnr.
ENDLOOP.
运行结果
实战中的用法:
例如我们预计需新建的表数据会达到亿级别数据,从查询性能的角度考虑,我们需要将表按照一定的数据维度,将表拆分成几个去存储数据。那么在查询是就需要将几张表的数据都放到一个内表中,由于这种场景中多表的结构基本相同,所以使用APPENDING TABLE将会大大简化代码
SELECT matnr
lifnr
werks_d AS werks
oder_flag
APPENDING TABLE lt_zpu_matnr
FROM zpu_matnr_d
WHERE matnr = ls_mepoitem-matnr
AND lifnr IN (ls_mepoitem-charg,'')
AND werks_d = lv_lgort1
AND datae >= sy-datum
AND datab <= sy-datum
AND oder_flag = space
AND del_flag = space
AND enable = 'X'.
SELECT matnr
lifnr
werks_d AS werks
oder_flag
APPENDING TABLE lt_zpu_matnr
FROM zpu_matnr_l
WHERE matnr = ls_mepoitem-matnr
AND lifnr IN (ls_mepoitem-charg,'')
AND werks_d = lv_lgort1
AND datae >= sy-datum
AND datab <= sy-datum
AND oder_flag = space
AND del_flag = space
AND enable = 'X'.
SELECT matnr
lifnr
werks_s AS werks
oder_flag
APPENDING TABLE lt_zpu_matnr
FROM zpu_matnr_p
WHERE matnr = ls_mepoitem-matnr
AND lifnr IN (ls_mepoitem-charg,'')
AND werks_s = lv_lgort1
AND datae >= sy-datum
AND datab <= sy-datum
AND oder_flag = space
AND del_flag = space
AND enable = 'X'.
SELECT matnr
lifnr
werks_s AS werks
oder_flag
APPENDING TABLE lt_zpu_matnr
FROM zpu_matnr_s
WHERE matnr = ls_mepoitem-matnr
AND lifnr IN (ls_mepoitem-charg,'')
AND werks_s = lv_lgort1
AND datae >= sy-datum
AND datab <= sy-datum
AND oder_flag = space
AND del_flag = space
AND enable = 'X'.
http://bbs.sapclub.cc/thread-301-1-1.html
(出处: SAP顾问圈)