本文主要介绍一下SAP ABAP中内表的命令,主要包括了内表赋值、内表初始化、内表排序、内表属性四个方面
内表赋值
两个内表结构一致赋值
不带表头行内表赋值
MOVE itab1 TO itab2. "第一种方式 itab1 = itab2. "第二种方式
带表头行内表赋值
MOVE itab1[] TO itab2[]. "第一种方式 itab1[] = itab2[]. "itab1和itab2均为带表头行内表 itab1[] = itab2. "itab1为带表头行内表,itab2为不带表头行内表 itab1 = itab2[]. "itab2为带表头行内表,itab1为不带表头行内表
两个内表结构不一致赋值
PS:【MOVE-CORRESPONDING】语句可以将两个内表中对应的字段进行赋值,而不用管其字段所在位置和顺序
不带表头行内表赋值
MOVE-CORRESPONDING itab1 TO itab2.
带表头行内表赋值
MOVE-CORRESPONDING itab1[] TO itab2[]. "itab1和itab2均为带表头行内表 MOVE-CORRESPONDING itab1[] TO itab2. "itab1为带表头行内表,itab2为不带表头行内表 MOVE-CORRESPONDING itab1 TO itab2[]. "itab2为带表头行内表,itab1为不带表头行内表
内表初始化
同时删除表数据和释放内存空间
CLEAR itab. "itab为不带表头行内表 CLEAR itab[]. "itab为带表头行内表
只删除表数据而不释放内存空间
REFRESH itab. "itab为不带表头行内表 REFRESH itab[]. "itab为带表头行内表
释放内存空间
FREE itab. "itab为不带表头行内表 FREE itab[]. "itab为带表头行内表
内表排序
PS:无论是整表排序还是按字段排序,若不指定排序方式则都默认为升序!
整表排序(除了排序表以外)
SORT itab [ASCENDING|DESCENDING]
案例代码演示
TYPES:BEGIN OF GTY_ITAB, COL1 TYPE c, COL2 TYPE i, END OF GTY_ITAB. DATA:GT_ITAB TYPE STANDARD TABLE OF GTY_ITAB WITH NON-UNIQUE KEY COL1. DATA:GS_ITAB TYPE GTY_ITAB. GS_ITAB-COL1 = 'B'. GS_ITAB-COL2 = 1. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 3. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'C'. GS_ITAB-COL2 = 4. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'D'. GS_ITAB-COL2 = 2. APPEND GS_ITAB TO GT_ITAB. WRITE:'内表排序前'. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2. ENDLOOP. ULINE. SORT GT_ITAB ASCENDING. WRITE:'内表排序后'. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2. ENDLOOP.
效果演示
指定排序字段(除了排序表以外)
SORT itab BY f1 [ASCENDING|DESCENDING] f2 [ASCENDING|DESCENDING] ... fn [ASCENDING|DESCENDING].
案例代码演示
TYPES:BEGIN OF GTY_ITAB, COL1 TYPE c, COL2 TYPE i, END OF GTY_ITAB. DATA:GT_ITAB TYPE STANDARD TABLE OF GTY_ITAB . DATA:GS_ITAB TYPE GTY_ITAB. GS_ITAB-COL1 = 'B'. GS_ITAB-COL2 = 1. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'A'. GS_ITAB-COL2 = 3. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'C'. GS_ITAB-COL2 = 4. APPEND GS_ITAB TO GT_ITAB. GS_ITAB-COL1 = 'C'. GS_ITAB-COL2 = 2. APPEND GS_ITAB TO GT_ITAB. WRITE:'内表排序前'. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2. ENDLOOP. ULINE. SORT GT_ITAB BY COL1 COL2 DESCENDING. WRITE:'内表排序后'. LOOP AT GT_ITAB INTO GS_ITAB. WRITE:/ GS_ITAB-COL1,GS_ITAB-COL2. ENDLOOP.
效果演示
内表属性
DESCRIBE TABLE itab [LINES gv_lines] [OCCURS gv_occurs] [KIND gv_kind]
其中:LINES返回的是内表包含的数据件数,OCCURS返回的是内表初始大小,KIND返回的是内表的类型(T代表标准表;S代表排序表;H代表哈希表)
案例代码演示
TYPES:BEGIN OF GTY_ITAB, COL1 TYPE I, COL2 TYPE I, END OF GTY_ITAB. DATA:GT_ITAB TYPE SORTED TABLE OF GTY_ITAB INITIAL SIZE 40 WITH UNIQUE KEY COL1. DATA:GS_ITAB TYPE GTY_ITAB. DATA:GV_LINES TYPE I. DATA:GV_OCCURS TYPE I. DATA:GV_KIND TYPE C. GS_ITAB-COL1 = 0. DO 50 TIMES. GS_ITAB-COL1 = GS_ITAB-COL1 + 1. GS_ITAB-COL2 = GS_ITAB-COL1 * 2. APPEND GS_ITAB TO GT_ITAB. ENDDO. DESCRIBE TABLE GT_ITAB LINES GV_LINES OCCURS GV_OCCURS KIND GV_KIND. WRITE:/ '该内表的数据件数是:',GV_LINES,/'该内表的初始大小是:',GV_OCCURS,/'该内表的类型是:',GV_KIND.
效果演示
PS:假如不设置初始大小【INTIAL SIZE n】则内表初始大小默认为0!!!
TYPES:BEGIN OF GTY_ITAB, COL1 TYPE I, COL2 TYPE I, END OF GTY_ITAB. DATA:GT_ITAB TYPE SORTED TABLE OF GTY_ITAB WITH UNIQUE KEY COL1.'不设置内表初始大小 DATA:GS_ITAB TYPE GTY_ITAB. DATA:GV_LINES TYPE I. DATA:GV_OCCURS TYPE I. DATA:GV_KIND TYPE C. GS_ITAB-COL1 = 0. DO 50 TIMES. GS_ITAB-COL1 = GS_ITAB-COL1 + 1. GS_ITAB-COL2 = GS_ITAB-COL1 * 2. APPEND GS_ITAB TO GT_ITAB. ENDDO. DESCRIBE TABLE GT_ITAB LINES GV_LINES OCCURS GV_OCCURS KIND GV_KIND. WRITE:/ '该内表的数据件数是:',GV_LINES,/'该内表的初始大小是:',GV_OCCURS,/'该内表的类型是:',GV_KIND.
效果演示