本文主要介绍一下SAP ABAP中内表的增删查改语句中的追加内表数据的COLLECT相关语句
COLLECT语句
对内表使用COLLECT语句时,除了关键字以外的数据类型必须为数字类型(f,i,p)。SAP ABAP中COLLECT语句以关键字为基准合计内表中数字类型的字段,如果内表没有定义关键字,则会自动以字符串类型的字段为关键字。
COLLECT wa INTO itab.
案例代码演示(关键字相同)
TYPES:BEGIN OF TY_ITAB, COL1 TYPE CHAR20, COL2 TYPE F, COL3 TYPE I, COL4 TYPE P, END OF TY_ITAB. DATA:GT_ITAB TYPE TABLE OF TY_ITAB, GS_ITAB TYPE TY_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 2. GS_ITAB-COL3 = 5. GS_ITAB-COL4 = 6. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 4. GS_ITAB-COL3 = 5. GS_ITAB-COL4 = 8. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 3. GS_ITAB-COL3 = 4. GS_ITAB-COL4 = 9. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2,GS_ITAB-COL3,GS_ITAB-COL4. ENDLOOP.
效果演示(关键字相同)
案例代码演示(关键字不同)
TYPES:BEGIN OF TY_ITAB, COL1 TYPE CHAR20, COL2 TYPE F, COL3 TYPE I, COL4 TYPE P, END OF TY_ITAB. DATA:GT_ITAB TYPE TABLE OF TY_ITAB, GS_ITAB TYPE TY_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 2. GS_ITAB-COL3 = 5. GS_ITAB-COL4 = 6. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. GS_ITAB-COL1 = 'B'. GS_ITAB-COL2 = 4. GS_ITAB-COL3 = 5. GS_ITAB-COL4 = 8. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. GS_ITAB-COL1 = 'C'. GS_ITAB-COL2 = 3. GS_ITAB-COL3 = 4. GS_ITAB-COL4 = 9. COLLECT GS_ITAB INTO GT_ITAB. CLEAR GS_ITAB. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2,GS_ITAB-COL3,GS_ITAB-COL4. ENDLOOP.
效果演示(关键字不同)