一些我工作中經常使用的ABAP新語法和函式,關鍵字,可以提高工作效率
(1) 直接批次生成資料到ABAP內表裡:
* 2017-05-14 7:17PM in Xun's house, Wiesloch
INSERT demo_join1 FROM TABLE @( VALUE #(
( a = 'a1' b = 'b1' c = 'c1' d = 'uu' )
( a = 'a2' b = 'b2' c = 'c2' d = 'uu' )
( a = 'a3' b = 'b3' c = 'c3' d = 'vv' )
( a = 'a4' b = 'b4' c = 'c4' d = 'ww' ) ) ).
(2) 採用內聯方式遍歷ABAP內表:
DATA address_annos TYPE STANDARD TABLE OF field_anno-annoname
WITH EMPTY KEY.
address_annos = VALUE #(
( 'SEMANTICS.NAME.FULLNAME' )
( 'SEMANTICS.ADDRESS.STREET' )
( 'SEMANTICS.ADDRESS.CITY' )
( 'SEMANTICS.ADDRESS.ZIPCODE' )
( 'SEMANTICS.ADDRESS.COUNTRY' ) ).
DATA address_components TYPE STANDARD TABLE OF field_anno-fieldname
WITH EMPTY KEY.
address_components = VALUE #(
FOR address_anno IN address_annos
( VALUE #( fieldannos[ annoname = address_anno ]-fieldname
DEFAULT '---' ) ) ).
(3) 兩個內表間資料的複製,兩個內表的列結構可以不同,開發人員需要額外傳入一個對映表,告訴corresponding關鍵字,源內表的哪一列應該賦到目標內表的哪一列。
* This is exactly what I want!!
REPORT demo_corresponding_vs_for.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
main,
class_constructor.
PRIVATE SECTION.
TYPES:
BEGIN OF struct,
carrier TYPE spfli-carrid,
connection TYPE spfli-connid,
departure TYPE spfli-cityfrom,
destination TYPE spfli-cityto,
END OF struct.
CLASS-DATA:
itab TYPE HASHED TABLE OF spfli
WITH UNIQUE KEY carrid connid,
result1 TYPE HASHED TABLE OF struct
WITH UNIQUE KEY carrier connection,
result2 TYPE HASHED TABLE OF struct
WITH UNIQUE KEY carrier connection,
result3 TYPE HASHED TABLE OF struct
WITH UNIQUE KEY carrier connection,
in TYPE REF TO if_demo_input,
out TYPE REF TO if_demo_output.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA(iterations) = 10.
in->request( CHANGING field = iterations ).
DO iterations TIMES.
DATA t1 TYPE i.
GET RUN TIME FIELD DATA(t11).
result1 = CORRESPONDING #(
itab MAPPING carrier = carrid
connection = connid
departure = cityfrom
destination = cityto ).
GET RUN TIME FIELD DATA(t12).
t1 = t1 + t12 - t11.
DATA t2 TYPE i.
GET RUN TIME FIELD DATA(t21).
result2 = VALUE #( FOR wa IN itab ( carrier = wa-carrid
connection = wa-connid
departure = wa-cityfrom
destination = wa-cityto ) ).
GET RUN TIME FIELD DATA(t22).
t2 = t2 + t22 - t21.
DATA t3 TYPE i.
GET RUN TIME FIELD DATA(t31).
result3 = VALUE #( FOR wa IN itab (
CORRESPONDING #(
wa MAPPING carrier = carrid
connection = connid
departure = cityfrom
destination = cityto ) ) ).
GET RUN TIME FIELD DATA(t32).
t3 = t3 + t32 - t31.
ENDDO.
IF result1 = result2 AND result1 = result3.
out->write(
|CORRESPONDING: {
CONV decfloat16( t1 / iterations )
WIDTH = 10 ALIGN = RIGHT } Microseconds\n| &&
|FOR: {
CONV decfloat16( t2 / iterations )
WIDTH = 10 ALIGN = RIGHT } Microseconds\n| &&
|FOR CORRESPONDING: {
CONV decfloat16( t3 / iterations )
WIDTH = 10 ALIGN = RIGHT } Microseconds\n|
)->line(
)->display( result1 ).
ELSE.
out->display( `What?` ).
ENDIF.
ENDMETHOD.
METHOD class_constructor.
in = cl_demo_input=>new( ).
out = cl_demo_output=>new( ).
SELECT *
FROM spfli
INTO TABLE @itab.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
(4) DISCARDING DUPLICATES的用法
REPORT demo_corresponding_duplicates.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
TYPES:
BEGIN OF line,
a1 TYPE i,
a2 TYPE i,
END OF line,
ntab1 TYPE STANDARD TABLE OF line WITH EMPTY KEY,
ntab2 TYPE SORTED TABLE OF line WITH UNIQUE KEY a1,
BEGIN OF line1,
x1 TYPE i,
x2 TYPE ntab1,
END OF line1,
BEGIN OF line2,
y1 TYPE i,
y2 TYPE ntab2,
END OF line2,
itab1 TYPE STANDARD TABLE OF line1 WITH EMPTY KEY,
itab2 TYPE SORTED TABLE OF line2 WITH UNIQUE KEY y1.
DATA(itab1) =
VALUE itab1( ( x1 = 1 x2 = VALUE #( ( a1 = 1 a2 = 2 )
( a1 = 3 a2 = 4 ) ) )
( x1 = 2 x2 = VALUE #( ( a1 = 1 a2 = 2 )
( a1 = 1 a2 = 4 ) ) )
( x1 = 1 x2 = VALUE #( ( a1 = 1 a2 = 2 )
( a1 = 3 a2 = 4 ) ) ) ).
DATA(itab2) =
CORRESPONDING itab2( itab1 DISCARDING DUPLICATES
MAPPING y1 = x1
y2 = x2 DISCARDING DUPLICATES ).
DATA(out) = cl_demo_output=>new( ).
LOOP AT itab2 INTO DATA(wa).
out->write( wa-y1
)->write( wa-y2
)->line( ).
ENDLOOP.
out->display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
(5) DESCRIBE DISTANCE BETWEEN
REPORT demo_describe_distance.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: BEGIN OF struc,
comp1 TYPE i,
comp2 TYPE x LENGTH 1,
comp3 TYPE c LENGTH 4 VALUE 'Hey',
comp4 TYPE c LENGTH 4 VALUE 'you!',
comp5 TYPE x,
END OF struc.
FIELD-SYMBOLS: <hex> TYPE x,
<result> TYPE c.
DESCRIBE DISTANCE BETWEEN:
struc AND struc-comp3 INTO DATA(off) IN BYTE MODE,
struc-comp3 AND struc-comp5 INTO DATA(len) IN BYTE MODE.
ASSIGN: struc TO <hex> CASTING,
<hex>+off(len) TO <result> CASTING.
cl_demo_output=>display(
|Offset off is { off }.\n| &&
|Length len is { len }.\n| &&
|<result> points to "{ <result> }".| ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
(6) 全動態方式呼叫RFC
REPORT demo_rfc_dynamic_dest.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA:
val_in TYPE string VALUE `val_in`,
val_in_out TYPE string VALUE `val_in_out`,
val_out TYPE string,
msg TYPE c LENGTH 80.
IF sy-uname IS INITIAL.
cl_demo_output=>display(
|Example not possible for anonymous user| ).
RETURN.
ENDIF.
DATA(in) = cl_demo_input=>new( ).
DATA(client) = sy-mandt.
in->add_field( CHANGING field = client ).
DATA(uname) = sy-uname.
in->add_field( CHANGING field = uname ).
DATA(langu) = sy-langu.
in->add_field( CHANGING field = langu ).
DATA(sysid) = sy-sysid.
in->add_field( CHANGING field = sysid ).
DATA(host) = CONV rfchost( sy-host ).
in->add_field( CHANGING field = host ).
DATA(group) = CONV rfcload( 'PUBLIC' ).
in->add_field( CHANGING field = group ).
in->request( ).
DATA(dest) = cl_dynamic_destination=>create_rfc_destination(
logon_client = client
logon_user = uname
logon_language = langu
sid = sysid
server = host
group = group ).
CALL FUNCTION 'DEMO_RFM_PARAMETERS'
DESTINATION dest
EXPORTING
p_in = val_in
IMPORTING
p_out = val_out
CHANGING
p_in_out = val_in_out
EXCEPTIONS
system_failure = 2 MESSAGE msg
communication_failure = 4 MESSAGE msg.
IF sy-subrc <> 0.
cl_demo_output=>display( |Error when calling sRFC.\n{ msg }| ).
RETURN.
ENDIF.
cl_demo_output=>display( |{ val_out }\n{ val_in_out }| ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
(7) 行內函數line_index
REPORT.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: class_constructor,
main.
PRIVATE SECTION.
CLASS-DATA
flight_tab
TYPE STANDARD TABLE OF spfli
WITH EMPTY KEY
WITH UNIQUE HASHED KEY id COMPONENTS carrid connid
WITH NON-UNIQUE SORTED KEY cities COMPONENTS cityfrom cityto.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA idx TYPE TABLE OF i.
idx = VALUE #(
( line_index( flight_tab[ carrid = 'UA'
connid = '0941'
##primkey[id] ] ) )
( line_index( flight_tab[ KEY id
carrid = 'UA'
connid = '0941' ] ) )
( line_index( flight_tab[ KEY id
carrid = 'xx'
connid = 'yyyy' ] ) )
( line_index( flight_tab[ cityfrom = 'FRANKFURT'
cityto = 'NEW YORK'
##primkey[cities] ] ) )
( line_index( flight_tab[ KEY cities
cityfrom = 'FRANKFURT'
cityto = 'NEW YORK' ] ) )
( line_index( flight_tab[ KEY cities
cityfrom = 'xxxxxxxx'
cityto = 'yyyyyyyy' ] ) ) ).
cl_demo_output=>display( idx ).
ENDMETHOD.
METHOD class_constructor.
SELECT *
FROM spfli
ORDER BY carrid, connid
INTO TABLE @flight_tab.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2673566/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SAP ABAP關鍵字語法圖和ABAP程式碼自動生成工具Code Composer
- SAP ABAP COMMIT WORK 關鍵字的一些使用要點MIT
- ABAP 740裡的新語法 - LET表示式
- 函式的關鍵字引數函式
- SQL Server 和 MySql 語法和關鍵字的區別ServerMySql
- 一些 Next Generation ABAP Platform 的新語法用例Platform
- PHP經常使用的字串函式PHP字串函式
- ?Java8新特性之Lambda表示式,函式式介面,方法引用和default關鍵字Java函式
- ABAP關鍵字 IS BOUND, IS NOT INITIAL和IS ASSIGNED的用法辨析
- 新的主鍵和外來鍵的語法
- REVERSE關鍵字之REVERSE函式函式
- javascript 函式作用域中的關鍵字this的指向JavaScript函式
- Python 函式 (關鍵字引數)Python函式
- markdown語法和快捷鍵的使用
- PHP 7.4 新語法:箭頭函式PHP函式
- C語言中關鍵字typedef、enum的使用C語言
- JDK1.8新特性:Lambda表示式語法和內建函式式介面JDK函式
- 利用ABAP 740的新關鍵字REDUCE完成一個實際工作任務
- js中宣告提升、作用域(鏈)、`this`關鍵字和箭頭函式JS函式
- Dart語法篇之函式的使用(四)Dart函式
- php生成器函式與yield關鍵字PHP函式
- out關鍵字和ref關鍵字的區別
- c語言中的關鍵字C語言
- MogDB/openGauss的三種函式穩定性關鍵字函式
- 使用Excel呼叫ABAP系統的函式Excel函式
- mysql練習 —— 關於一些函式的使用MySql函式
- [PY3]——函式——生成器(yield關鍵字)函式
- Synchronized關鍵字的使用synchronized
- JavaScript 函式語法JavaScript函式
- Go 語言 new 和 make 關鍵字的區別Go
- python語言幾個常見函式的使用Python函式
- Oracle常見數字函式Oracle函式
- CSS,我可以寫 if/else 語法了?CSS
- ABAP日期函式函式
- sap abap好用的函式函式
- 第五章:常用的高階函式和函式的補充語法函式
- ABAP和Java裡關於DEFAULT(預設)機制的一些語言特性Java
- Python函式/動態引數/關鍵字引數Python函式