内表相关

编程技术  /  houtizong 发布于 3年前   102

                  一. Internal Table 的宣告
                  ABAP/4中的Internal Table是一种Data
                  Structure,类似于其它语言中的STRUTURE,它可以由几个不同类型的字段(field)组成,用来表示具有不同属性的某一事物,单独一笔数据表示某个事物,多笔数据表示具有相同属性的多个事物.例如:
                  为了存取或记录某班的同学资料,我们创建如下的internal table:
                  DATA: BEGIN OF STUDENT OCCURS 20,
                               STD_ID   TYPE N,
                               NAME(10) TYPE C,
                               AGE      TYPE I,
                               BIRTH    TYPE D,
                               SCORE    TYPE P DECIMALS 2,
                        END OF STUDENT.
                  此时我们已经创建了名叫STUDENT的internal
                  table,并且为它预先申请了能够存放20笔资料的Buffer(当然,如果存取资料不止20笔,程序执行时,会自动申请系统Buffer)
                  Internal table 的定义有以下几种格式:
                  格式一.   DATA: BEGIN OF <internal table>  OCCURS  <n>,
                                     <field 1>  TYPE <type1>,
                                    [<field 2>  TYPE <type 2>,
                                     <field 3>  TYPE <type 3>,
                                      …                  ]
                                  END OF  <internal table>.
                  格式二.   TYPES: BEGIN OF <work area>,
                                       <field 1>  TYPE <type1>,
                                      [<field 2>  TYPE <type 2>,
                                       <field 3>  TYPE <type 3>,
                                      …                  ]
                                   END OF <work area>.
                            TYPES <internal table> TYPE <work area> OCCURS <n>.
                  格式三.    DATA: BEGIN OF <work area>.
                                         INCLUDE STRUCTURE <table name>.
                            DATA: END OF <work area>.
                            DATA: <internal table> LIKE <work area> OCCURS <n>.

                  二. APPEND LINE
                  格式: APPEND [<work area>  TO ] <internal table>.
                  举例一. (使用work area)
                  DATA: BEGIN OF LINE,
                               COL1 TYPE I,
                               COL2 TYPE I,
                        END OF LINE.
                  DATA  ITAB LIKE LINE OCCURS 10.
                  DO 2 TIMES.
                       LINE-COL1 = SY-INDEX.
                       LINE-COL2 = SY-INDEX ** 2.
                       APPEND LINE TO ITAB.
                  ENDDO.
                  LOOP AT ITAB INTO LINE.
                        WRITE: / LINE-COL1, LINE-COL2.
                  ENDLOOP.
                  执行结果为:
                  1 1
                  2 4
                  举例二. (不使用work area)
                          DATA: BEGIN OF ITAB OCCURS 10,
                                       COL1 TYPE I,
                                       COL2 TYPE I,
                                END OF ITAB.
                          DO 2 TIMES.
                              ITAB-COL1 = SY-INDEX.
                              ITAB-COL2 = SY-INDEX ** 2.
                              APPEND ITAB.
                          ENDDO.
                          LOOP AT ITAB.
                              WRITE: / ITAB-COL1, ITAB-COL2.
                          ENDLOOP.
                          执行结果与举例一相同.
                  举例三. (加入另一个Internal table的元素)
                         格式: APPEND LINES OF <itab1> [FROM <n1> ] [TO <n2>] TO
                  <itab2>.
                         将<itab1>的元素加入至<itab2>中,可选取自<n1>至<n2>的范围.
                         APPEND LINES OF ITAB TO JTAB.

                  三. COLLECT LINE
                  COLLECT 指令也是将元素加入Internal table中,与APPEND 的区别是:
                  COLLECT指令在非数值字段相同的情况下,将数值字段汇总.
                  格式:  COLLECT [<work area> INTO ] <itab>
                  DATA: BEGIN OF ITAB OCCURS 3,
                               COL1(3)  TYPE C,
                               COL2     TYPE I,
                         END OF ITAB.
                         ITAB-COL1 = ‘ABC’.    ITAB-COL2 = 10.
                         COLLECT  ITAB.
                         ITAB-COL1 = ‘XYZ’.    ITAB-COL2 = 20.
                         COLLECT  ITAB.
                         ITAB-COL1 = ‘ABC’.    ITAB-COL2 = 80.
                         COLLECT  ITAB.
                  此时, internal table中放的是2笔数据, 分别为:
                         ITAB-COL1        ITAB-COL2
                          ‘ABC’               90
                          ‘XYZ’               20

                  四. INSERT LINE
                  将元素插入在指定的internal table位置之前.
                  格式: INSERT [<wa> INTO] [INITIAL LINE INTO ] <itab> [INDEX
                  <idx>]
                  或者: INSERT LINES OF <itab1> [FROM <n1> TO <n2>] INTO <itab2>
                  INDEX <idx>
                  其中: <wa>即work area,工作区中的元素.
                  [INITIAL LINE INTO] :插入一笔初始化的记录.
                  <itab>: internal table
                  [INDEX <idx>]: internal table 的记录号.(新加入的元素放在此记录前面)

                  五. 读取internal table
                  格式一:
                  LOOP AT <itab> [INTO <wa>][FROM <n1> TO <n2>][WHERE
                  <conditions>]
                         <statement>
                  ENDLOOP.
                  格式二:
                  READ TABLE <itab> [INTO <wa>] [INDEX <idx> / WITH KEY
                  <conditions>]
                  举例. (格式二)
                  DATA: BEGIN OF ITAB OCCURS 10,
                               COL1 TYPE I,
                               COL2 TYPE I,
                        END OF ITAB.
                        DO 10 TIMES.
                            ITAB-COL1 = SY-INDEX.
                            ITAB-COL2 = SY-INDEX * 2.
                            APPEND ITAB.
                  ENDDO.
                  READ TABLE ITAB INDEX 3.
                  (或者: READ TABLE  ITAB WITH KEY COL1 = 3.)
                  WRITE: / ‘ITAB-COL1 = ‘, ITAB-COL1, ‘ITAB-COL2 = ‘, ITAB-COL2.
                  执行结果同样是:
                  ITAB-COL1 =      3
                  ITAB-COL2 =      6.

                  六. 修改internal table 中的值
                  格式: MODIFY <itab> [FROM <wa>][INDEX <idx>][TRANSPORTING
                  <f1><f2>…][WHERE <conditions>]
                  举例一. READ TABLE ITAB INDEX 3.
                         LINE-COL1 = 29.
                         MODIFY ITAB FROM LINE TRANSPORTING COL1.
                         将第三笔记录的COL1字段的值修改为29.
                  举例二. T_SALARY – salary = 50.
                         MODIFY T_SALARY TRANSPORTING salary WHERE birthday =
                  ‘1999/12/06’.

                  七. DELETE internal table中的字段
                  格式: DELETE <itab> INDEX <idx>.
                  或:   DELETE <itab>[FROM <n1> TO <n2>] [WHERE <conditions>]

                  八. Internal table 排序 
                  SORT <itab> [<order way>][BY <f1><f2>…]
                  其中:<order way> 有DESCENDING 和ASCENDING, Default 为ASCENDING.
                       <f1>: 为指定排序的字段.

                  九. 加总
                  SUM.
                  总和计算存放与work area中,但只能在LOOP 中使用.
                  例: LOOP AT ITAB INTO LINE.
                           SUM.
                     ENDLOOP.
                     WRITE: / LINE-COL1, LINE-COL2.

                  十. 初始化internal table
                  REFRESH <itab>.  清空<itab>中的值.
                  CLEAR   <itab>.  清空<itab>的Header Line.
                  FREE     <itab>.  释放记忆体空间.

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客