个人网站:【芒果个人日志】
原文地址:SAP ABAP——内表(四)【内表命令】 - 芒果个人日志 (wyz-math.cn)
作者简介: THUNDER王,一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读,同时任汉硕云(广东)科技有限公司ABAP开发顾问。在学习工作中,我通常使用偏后端的开发语言ABAP,SQL进行任务的完成,对SAP企业管理系统,SAP ABAP开发和数据库具有较深入的研究。
文章概要:本文主要介绍一下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.
效果演示——按col1升序col2降序
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.
效果演示1
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.
效果演示2