記錄ABAP開發的日常——常用新語法總結
阿新 • • 發佈:2021-12-19
前言:在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.