Loading

记录ABAP开发的日常——常用新语法总结

前言:在7.4版本以后,SAP ABAP语言做出了一些调整,让一些语法书写起来更加方便快捷,类似于其他语言的语法糖,不过一些稍微老的系统版本还不支持这些新写法,所以在使用方面主要看个人的习惯了,当然以前的写法肯定是要会的,这个新的语法作为了解也是可以的,今天就记录一下目前本人所接触的一些新语法,当然新语法肯定不止这些,更多的还是需要小伙伴去发掘,话不多说,上代码。

新语法练习

**********************************************************************
* PROGRAM DETAIL                                                     *
**********************************************************************
* --T-CODE:ZDEMO_NEW_GRAMMAR
* --AUTHOR:秋の夕
* --CREATE DATE:19.12.2021 13:19:28
* --DESCRIBLE:新语法总结练习
**********************************************************************
* EDIT LOG                                                           *
**********************************************************************
* VERSION          DATE              AUTHOR           REMARK
* <DEVK90XXX>     <YYYY/MM/DD>       <...>            <REASON>

REPORT zdemo_new_grammar.
**********************************************************************
* DECLARATIONS                                                       *
**********************************************************************
TABLES:ztdemo.
* 根据数据生成内表
WRITE: / | >----------------------无声明:根据数据生成内表 -------------------------<|.
SELECT * INTO TABLE @DATA(gt_data) FROM ztdemo.

* 自定义结构
TYPES:BEGIN OF ty_data,
        id       TYPE i,
        name     TYPE char10,
        language TYPE char2,
        age      TYPE i,
      END OF ty_data.

DATA:itab  TYPE STANDARD TABLE OF ty_data WITH EMPTY KEY.

* 添加数据
** ---->单个赋值
itab = VALUE #(
                ( id = '1' name = '张三' language = 'ZH' age = '18' )
                ( id = '2' name = '李四' language = 'ZH' age = '19' )
                ( id = '3' name = '王五' language = 'EN' age = '20' )
                ( id = '4' name = '赵六' language = 'EN' age = '21' )
                ).

WRITE: / | >----------------------单个赋值 -------------------------<|.
LOOP AT itab INTO DATA(wa).
  WRITE:/  |{ wa-id } - { wa-name } - { wa-language } - { wa-age WIDTH = 20 ALIGN = LEFT }|.
ENDLOOP.

** ---->基于内表赋值
WRITE: / | >----------------------基于内表赋值 -------------------------<|.
itab = VALUE #( BASE itab
                ( id = '5' name = '孙七' language = 'EN' age = '23' )
                ( id = '6' name = '蔡九' language = 'ZH' age = '21' )
                ).
LOOP AT itab INTO wa.
  WRITE:/  |{ wa-id }  - { wa-name } - { wa-language } - { wa-age WIDTH = 20 ALIGN = LEFT }|.
ENDLOOP.

*---------------------------------------------内表行数
WRITE: / | >----------------------内表行数 -------------------------<|.
DATA(tlines) = lines( itab ).
WRITE:/ |表行数:{ tlines }|.
*
*---------------------------------------------按照指定索引查找内表数据
WRITE: / | >----------------------按照指定索引查找内表数据 -------------------------<|.
DATA(wa_data1) = itab[ 2 ] .
WRITE:/ |行号为2的姓名:{ wa_data1-name }|.
*
*---------------------------------------------按照指定条件查找内表数据
WRITE: / | >----------------------按照指定条件查找内表数据 -------------------------<|.
DATA(wa_data2) = itab[ name = '张三' ] .
WRITE:/ |姓名为张三的行号: { wa_data2-id } |.
*
WRITE: / | >----------------------判断是否存在记录 -------------------------<|.
IF line_exists( itab[ id = 3 age = '20' ] ).
  WRITE:/ |内表存在id为3,年龄为20的记录| .
ENDIF.

WRITE: / | >----------------------按照语言分组 -------------------------<|.
LOOP AT itab INTO DATA(group) GROUP BY ( language = group-language  ).
  WRITE: / | 分组语言:{ group-language }|.
  LOOP AT GROUP group INTO DATA(ls_data).
    WRITE: / |属于语言:{ group-language } 的id - { ls_data-id } |.
  ENDLOOP.
ENDLOOP.


WRITE: / | >----------------------条件查询汇总 -------------------------<|.
SELECT
  vbrp~vgbel,
  vbrp~vgpos,
SUM( CASE WHEN vbrk~vbtyp = 'N' THEN - vbrp~fkimg
          WHEN vbrk~vbtyp = 'M' THEN   vbrp~fkimg END ) AS fkimg
FROM vbrk
INNER JOIN vbrp ON vbrk~vbeln = vbrp~vbeln
GROUP BY vbrp~vgbel ,vbrp~vgpos
INTO TABLE @DATA(lt_vbrp) UP TO 10 ROWS.
LOOP AT lt_vbrp INTO DATA(ls_vbrp).
  WRITE:/ | { ls_vbrp-vgbel }-{ ls_vbrp-vgpos }-{ ls_vbrp-fkimg } |.
ENDLOOP.

效果展示

image

posted @ 2021-12-19 13:28  Gjboy666  阅读(1448)  评论(0编辑  收藏  举报