2019.12.27 【ABAP随笔】7.40新语法 - CORRESPONDING

今天学习一下新语法 CORRESPONDING

效果和move-corresponding 类似

"1.… CORRESPONDING type( [BASE ( base )] struct|itab [mapping] )

TYPES:BEGIN OF ty_data1,
        field1 TYPE i,
        field2 TYPE i,
      END OF ty_data1,
      BEGIN OF ty_data2,
        field1 TYPE i,
        field2 TYPE i,
        field3 TYPE i,
      END OF ty_data2.

DATA(ls_data1) = VALUE ty_data1(
field1 = 1 field2 = 2
 ).

DATA(ls_data2) = VALUE ty_data2( field1 = 4 field2 = 5 field3 = 6 ).

"使用 curresponding语句可以:

ls_data2 = CORRESPONDING #( ls_data1 ).

WRITE:/ ls_data2-field1,ls_data2-field2,ls_data2-field3.
" 1 2 0 此时 ls_data2-field3 = 0 在赋值过程中被覆盖


ls_data2  =  VALUE ty_data2( field1 = 4 field2 = 5 field3 = 6 ).
"
ls_data2 = CORRESPONDING #( BASE ( ls_data2 ) ls_data1 ).

WRITE:/ ls_data2-field1,ls_data2-field2,ls_data2-field3.
"此时 ls_data2-field = 6 未被覆盖


ls_data2  =  VALUE ty_data2( field1 = 4 field2 = 5 field3 = 6 ).

"当直接创建LS_DATA3时,未知道ls_data3是什么结构时,#必须未某一结构
DATA(ls_data3) = CORRESPONDING ty_data2( BASE ( ls_data2 ) ls_data1 ).

WRITE:/ ls_data3-field1,ls_data3-field2,ls_data3-field3.


"2.类似于lookup 的功能

TYPES:BEGIN OF ty_data,
        key     TYPE i,
        remarks TYPE char10,
      END OF ty_data,
      ty_t_data        TYPE STANDARD TABLE OF ty_data WITH EMPTY KEY,
      ty_hashed_t_data TYPE HASHED TABLE OF ty_data WITH UNIQUE KEY key.

DATA(lt_data1) = VALUE ty_t_data( FOR i = 1 UNTIL i > 10 ( key = i ) ).
DATA(lt_data2) = VALUE ty_hashed_t_data( ( key = 2 remarks = `ceshi1` )
                           ( key = 4 remarks = `测试1` )
                           ( key = 6 remarks = `测试2` )
                           ( key = 7 remarks = `测试3` ) ).
"把lt_data2中的内容匹配到 lt_data1里面
DATA:BEGIN OF ls_test,
       aptyp  TYPE c,
       status TYPE c,
       datbi  TYPE sy-datum,
     END OF ls_test.

ls_test-aptyp = 'D'.
ls_test-status = '0'.

IF ls_test-aptyp = 'D' AND ls_test-status = '0' AND ls_test-datbi IS INITIAL.
  ls_test-datbi = sy-datum+0(6) && '01'."申請迄日
ENDIF.

lt_data1 = CORRESPONDING #( lt_data1 FROM lt_data2 USING key = key ).

cl_demo_output=>display( data = lt_data1 ).

"3.类似于mapping的功能,不同名字字段之间传值 (这个语法可以用户和外部接口数据赋值的时候使用)
DATA:BEGIN OF customer_oa,
       id   TYPE char10 VALUE '1000000001',
       name TYPE char35 VALUE 'OA_NAME',
       BEGIN OF information,
         company_id         TYPE char4 VALUE '6100',
         sales_organization TYPE char4 VALUE '6200',
         country            TYPE char3 VALUE 'CN',
       END OF information,
     END OF customer_oa,
     BEGIN OF customer_sap,
       kunnr TYPE kna1-kunnr,
       name1 TYPE kna1-name1,
       BEGIN OF info,
         bukrs TYPE knb1-bukrs,
         vkorg TYPE knvv-vkorg,
         land1 TYPE kna1-land1,
       END OF info,
     END OF customer_sap.

"两个名字不相同的工作区相互赋值

customer_sap = CORRESPONDING #(
customer_oa MAPPING
kunnr = id name1 = name
( info = information MAPPING
  bukrs = company_id
  vkorg = sales_organization
  land1 = country
  ) ).

WRITE:/ 'sap:' , customer_sap .
WRITE:/ 'oa:' , customer_oa.

 

-TAB 

你可能感兴趣的:(2019.12.27 【ABAP随笔】7.40新语法 - CORRESPONDING)