一些我工作中经常使用的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( ).

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

上一篇:阿里云掠地CDN:网宿称我们不在一条路上


下一篇:《高可用架构·中国初创故事(第3期)》一1.1 在新工作中找到你的出路