#yyds干货盘点#ABAP新语法

业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述#yyds干货盘点#ABAP新语法相关的知识,希望能为你提供帮助。
2 内表操作2.2  获得内表行数?Before:

DATA: lv_rows TYPE i.
LV_ROWS= LINES( ITAB)

After:
DATA(lv_rows) = LINES( itab ).

【#yyds干货盘点#ABAP新语法】?
2.3  填充内表的值到目标内表中?Before:
DATA: gt_citys TYPE ty_citys,
gs_shipTYPE ty_ship,
gs_cityTYPE ort01.

LOOP AT gt_ships INTO gs_ship.
gs_city =gs_ship-city.
APPEND gs_city TO gt_citys.
ENDLOOP.

After:
DATA(gt_citys) = VALUE ty_citys( FOR ls_ship IN gt_ships ( ls_ship-city ) ).



3 Open SQL?3.1  取数到内表?Before:
DATA itab TYPE TABLE OF dbtab.
SELECT * FROM dbtab
INTO TABLE itab
WHERE fld1 = lv_fld1.

After:
SELECT * FROM dbta
INTO TABLE DATA(itab)
WHERE fld1 = @lv_fld1

?3.2  宿主变量?在ABAP 7.40之前,OPEN SQL 中我们通常会添加一些变量,用来作为取数的限制条件,这种方式下,Open SQL parser将open SQL语句进行解析成与底层数据可相对应SQL时,可能会出现误差,为了能清晰的区分算符两端的东西到底是ABAP变量、还是数据库内容,从而发送相应的内容给数据库。为了完成这一任务,Open SQL中的ABAP变量因此成为了完全的宿主变量(host variables)。
?宿主变量用”@  ”来标记。
?
?3.3  宿主表达式?Before:
没有宿主变量之前,书写SQL时,可能会出现下面的错误。并且不容易察觉。
SELECT carrid connid fldate seatsocc seatsmax
FROM sflight
INTO TABLE sflight_tab
WHERE seatsmax < sflight-seatsocc.

SELECT carrid, connid, fldate, seatsocc, seatsmax
FROM sflight
WHERE seatsmax < sflight~seatsocc
INTO TABLE sflight_tab.

第一种情况 sflight-seatsocc表示的是变量,而第二种sflight~seatsocc则表示的是数据库字段
他们的执行结果是不同的。
After:
SELECT carrid, connid, fldate, seatsocc, seatsmax
FROM sflight
WHERE seatsmax < @sflight-seatsocc
INTO TABLE @sflight_tab.

引入宿主变量之后,通过@符号,我们能准确区分@sflight-seatsocc
是宿主变量,也就是程序中的变量,执行结果不会出现误差。


4 其他语法?4.1 FILTER??语法定义:?
FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 […]] ).

实例:
TYPES: BEGIN OF ty_filter,
cityfrom TYPE spfli–cityfrom,
citytoTYPE spfli–cityto,
f3TYPE i,
END OF ty_filter,
ty_filter_tab TYPE HASHED TABLE OF ty_filter
WITH UNIQUE KEY cityfrom cityto.
DATA: lt_splfi TYPE STANDARD TABLE OF spfli.
SELECT * FROM spfli APPENDING TABLE lt_splfi.

DATA(lt_filter) = VALUE ty_filter_tab( f3 = 2
( cityfrom = ‘NEW YORK’cityto= ‘SAN FRANCISCO’ )
( cityfrom = ‘FRANKFURT’ cityto= ‘NEW YORK’ )).
DATA(lt_myrecs) = FILTER #( lt_splfi IN lt_filter

WHERE cityfrom = cityfrom
AND cityto = cityto ).
“Output filtered records
LOOP AT lt_myrecs ASSIGNING FIELD–SYMBOL(< ls_rec> ).
WRITE: / < ls_rec> –carrid,8 < ls_rec> –cityfrom,30
< ls_rec> –cityto,45 < ls_rec> –deptime.
ENDLOOP.



?4.2 REDUCE?语法定义:
... REDUCE type(
[let_exp]
INIT x1 = rhs1|< x1> = wrexpr1|x1|< x1> TYPE dtype1
x2 = rhs2|< x2> = wrexpr2|x2|< x2> TYPE dtype2
...
FOR for_exp1
FOR for_exp2
...
NEXT ...
x1 = rhs1|< x1> = wrexpr1
x2 = rhs2|< x2> = wrexpr2
... ) ...

实例:
DATA itab TYPE STANDARD TABLE OF i WITH EMPTY KEY.
itab = VALUE #( FOR j = 1 WHILE j < = 10 ( j ) ).
DATA(sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).


4.3  使用类转换字符串 conv?Before:
DATA textTYPE c LENGTH 255.
DATA helper TYPE string.
DATA xstrTYPE xstring.
helper = text.
xstr = cl_abap_codepage=> convert_to( source = helper ).

?After:?
DATA text TYPE c LENGTH 255.
DATA(xstr) = cl_abap_codepage=> convert_to( source = CONV string( text ) ).
OR
DATA(xstr) = cl_abap_codepage=> convert_to( source = CONV #( text ) ).




    推荐阅读