【整理】ABAP 7.40新特性介紹(上)

ABAP 7.40 Quick Reference 

 

1. Inline Declarations - 內聯聲明

Description

Before 7.40

With 7.40

Data statement

DATA text TYPE string.
text = `ABC`.

DATA(text) = `ABC`.

Loop at into work area

DATA wa like LINE OF itab.
LOOP AT itab INTO wa.   
 

ENDLOOP.

LOOP AT itab INTO DATA(wa).   
  

ENDLOOP
.

Call method

DATA a1 TYPE

DATA a2 TYPE

oref->meth( IMPORTING p1 = a1

                                   IMPORTING p2 = a2).

oref->meth(

        IMPORTING p1 = DATA(a1)

        IMPORTING p2 = DATA(a2) ).

Loop at assigning

FIELD-SYMBOLS: <line> type

LOOP AT itab ASSIGNING <line>.

 

ENDLOOP.

LOOP AT itab

   ASSIGNING FIELD-SYMBOL(<line>).
   …
ENDLOOP.

Read assigning

FIELD-SYMBOLS: <line> type

READ TABLE itab

           ASSIGNING <line>.

READ TABLE itab

   ASSIGNING FIELD-SYMBOL(<line>).

Select into

table

DATA itab TYPE TABLE OF dbtab.

SELECT * FROM dbtab

   INTO TABLE itab

        WHERE fld1 =lv_fld1.

SELECT * FROM dbtab

   INTO TABLE DATA(itab) 

        WHERE fld1 = @lv_fld1.

Select single

into

SELECT SINGLE f1 f2 

  FROM dbtab

  INTO (lv_f1, lv_f2)

WHERE

WRITE: / lv_f1, lv_f2.

SELECT SINGLE f1 AS my_f1,

              F2 AS abc  

         FROM dbtab

         INTO DATA(ls_structure)

        WHERE

WRITE: / ls_structure-my_f1,              ls_structure-abc.

 

2. Table Expressions - 內表讀取

If a table line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised. No sy-subrc.

如果找不到內錶行,則觸發異常CX_SY_ITAB_LINE_NOT_FOUND 。沒有sy-subrc。

Description

Before 7.40

With 7.40

Read Table  index

READ TABLE itab INDEX idx

      INTO wa.

wa = itab[ idx ].

Read Table  using key

READ TABLE itab INDEX idx

     USING KEY key

      INTO wa.

wa = itab[ KEY key INDEX idx ].

Read Table  with key

READ TABLE itab

  WITH KEY col1 = 

           col2 = 

       INTO wa.

wa = itab[ col1 = … col2 = …].

Read Table  with key components

READ TABLE itab

      WITH TABLE KEY key

COMPONENTS col1 = 

           col2 = 

      INTO wa.

wa = itab[ KEY key col1 = 

                    col2 = …].

Does record exist?

READ TABLE itab

    TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

 

ENDIF.

IF line_exists( itab[ … ] ).

ENDIF.

Get table index

DATA idx type sy-tabix.

READ TABLE

  TRANSPORTING NO FIELDS.

  idx = sy-tabix.

DATA(idx) =

       line_index( itab[ … ] ).

NB: There will be a short dump if you use an inline expression that references a non-existent record.

        SAP says you should therefore assign a field symbol and check sy-subrc.

注意:如果使用內聯表達式引用一個不存在的記錄,則會出現Dump。SAP因此建議應該指定一個字段符號並檢查sy-subrc。

ASSIGN lt_tab[ to FIELD–SYMBOL(<ls_tab>).
IF sysubrc = 0.

ENDIF.

NB: Use itab [ table_line = … ] for untyped tables.

注意:對於非類型化的表使用itab [ table_line = … ]

 

3. Conversion Operator CONV - 轉換運算符CONV

  •  I.  Definition

CONV dtype|#( … )

dtype = Type you want to convert to (explicit)

#     = compiler must use the context to decide the type to convert to (implicit)

 

  • II. Example

Method cl_abap_codepage=>convert_to expects a string

 

4. Value Operator VALUE - 值運算符VALUE

  • I.   Definition

  變量Variables:    VALUE dtype|#( )

    結構Structures:  VALUE dtype|#( comp1 = a1 comp2 = a2 … )

    內表Tables:         VALUE dtype|#( ( … ) ( … ) … ) …

 

  •  II.  Example for structures
 TYPES:  BEGIN OF ty_columns1, “Simple structure
                     cols1 TYPE i, 
                     cols2 TYPE i, 
                   END OF ty_columns1.

      TYPES: BEGIN OF ty_columnns2,  “Nested structure
                     coln1 TYPE i, 
                     coln2 TYPE ty_columns1, 
                  END OF ty_columns2.

      DATA: struc_simple TYPE ty_columns1, 
                struc_nest    TYPE ty_columns2.

     struct_nest   = VALUE t_struct(coln1 = 1 
                                                  coln2-cols1 = 1 
                                                  coln2-cols2 = 2 ).

     OR

     struct_nest   = VALUE t_struct(coln1 = 1 
                                                   coln2 = VALUE #( cols1 = 1 
                                                   cols2 = 2 ) ).

 

  • III. Examples for internal tables

Elementary line type:

TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.

DATA itab TYPE t_itab.

itab = VALUE #( ( ) ( 1 ) ( 2 ) ).

Structured line type (RANGES table):

DATA itab TYPE RANGE OF i.

itab = VALUE #( sign = ‘I’  option = ‘BT’ ( low = 1  high = 10 ) 
( low = 21 high = 30 ) 
( low = 41 high = 50 ) 
option = ‘GE’ ( low = 61 )  ).

 

5. FOR operator - FOR操作符

  • I.   Definition

FOR wa|<fs> IN itab [INDEX INTO idx] [cond]

 

  • II.  Explanation

This effectively causes a loop at itab. For each loop the row read is assigned to a work area (wa) or field-symbol(<fs>).

This wa or <fs> is local to the expression i.e. if declared in a subrourine the variable wa or <fs> is a local variable of

that subroutine. Index like SY-TABIX in loop.

這樣就高效的循環了內表itab。對於每個循環,讀取的每一行行被分配給工作區(wa)或字段符號(<fs>)。

這裏的wa或<fs>是表達式的局部變量,即如果在子例程中聲明,則變量wa或<fs>也屬於那個子程序的局部變量。類似SY-TABIX這樣的索引在循環中的使用。

Given: 

TYPES: BEGIN OF ty_ship,
           tknum TYPE tknum,     “Shipment Number
           name  TYPE ernam,     “Name of Person who Created the Object
           city  TYPE ort01,     “Starting city
           route TYPE route,     “Shipment route
       END OF ty_ship.
TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.
TYPES: ty_citys TYPE STANDARD TABLE OF ort01 WITH EMPTY KEY.

GT_SHIPS type ty_ships. -> 已填充如下值:

  • III. Example 1

Populate internal table GT_CITYS with the cities from GT_SHIPS.

 

  •  IV. Example 2

Populate internal table GT_CITYS with the cities from GT_SHIPS where the route is R0001.

Note: ls_ship does not appear to have been declared but it is declared implicitly.

 

  • V. FOR with THEN and UNTIL|WHILE

FOR i = … [THEN expr] UNTIL|WHILE log_exp

Populate an internal table as follows:

TYPES:
  BEGIN OF ty_line,
    col1 TYPE i,
    col2 TYPE i,
    col3 TYPE i,
  END OF ty_line,
  ty_tab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.

 

6. Reduction operator REDUCE - 縮減運算符REDUCE

  • I.   Definition

… REDUCE type(

INIT result = start_value

          

FOR for_exp1

FOR for_exp2

NEXT

           result = iterated_value

… )

 

  • II.  Note

 While VALUE and NEW expressions can include FOR expressions, REDUCE must include at least one FOR expression. You can use all kinds      of FOR expressions in REDUCE:

  1. with IN for iterating internal tables
  2. with UNTIL or WHILE for conditional iterations

雖然VALUENEW 表達式可以包含FOR表達式,但REDUCE必須至少包含一個FOR表達式。您可以在REDUCE中使用各種FOR表達式:

  1. 帶IN用於迭代內表
  2. UNTIL 或者 WHILE的條件迭代

 

  • III. Example 1

Count lines of table that meet a condition (field F1 contains “XYZ”).

計算滿足條件的內錶行數(字段F1包含“XYZ”的)。

 

  • IV. Example 2

Sum the values 1 to 10 stored in the column of a table defined as follows

將存儲在表中的值1和10相加,定義如下

DATA gt_itab TYPE STANDARD TABLE OF i WITH EMPTY KEY.
gt_itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).

 

  •  V.  Example 3

Using a class reference – works because “write” method returns reference to instance object

 

7. Conditional operators COND and SWITCH - 條件運算符COND 和SWITCH

  • I.   Definition

… COND dtype|#( WHEN log_exp1 THEN result1 
[ WHEN log_exp2 THEN result2 ] 
… 
[ ELSE resultn ] ) …

… SWITCH dtype|#( operand 
WHEN const1 THEN result1 
[ WHEN const2 THEN result2 ] 
… 
[ ELSE resultn ] ) …

 

  • II.  Example for COND
DATA(time) =

  COND string(

    WHEN sy-timlo < ‘120000’ THEN

      |{ sy-timlo TIME = ISO } AM|

    WHEN sy-timlo > ‘120000’ THEN

      |{ CONV t( sy-timlo – 12 * 3600 )

TIME = ISO } PM|

    WHEN sy-timlo = ‘120000’ THEN

      |High Noon|

    ELSE

      THROW cx_cant_be( ) ).

 

  •  III. Example for SWITCH
DATA(text) =
NEW class( )->meth(
                     SWITCH #( sy-langu
                              WHEN ‘D’ THEN `DE`
                              WHEN ‘E’ THEN `EN`
                               ELSE THROW cx_langu_not_supported( ) ) ).

 

8. CORRESPONDING operator - CORRESPONDING 運算符

  • I.   Definition

… CORRESPONDING type( [BASE ( base )] struct|itab [mapping|except] )

 

  • II.  Example Code
TYPES: BEGIN OF line1, col1 TYPE i, col2 TYPE i, END OF line1.
TYPES: BEGIN OF line2, col1 TYPE i, col2 TYPE i, col3 TYPE i, END OFline2.

DATA(ls_line1) = VALUE line1( col1 = 1 col2 = 2 ).
WRITE: / ‘ls_line1 =’ ,15 ls_line1–col1, ls_line1–col2.
DATA(ls_line2) = VALUE line2( col1 = 4 col2 = 5 col3 = 6 ).
WRITE: / ‘ls_line2 =’ ,15 ls_line2–col1, ls_line2–col2,ls_line2–col3.
SKIP 2.

ls_line2 = CORRESPONDING #( ls_line1 ).
WRITE: / ‘ls_line2 = CORRESPONDING #( ls_line1 )’

     ,70 ‘Result is ls_line2 = ‘     

        ,ls_line2–col1, ls_line2–col2, ls_line2–col3.
SKIP.

ls_line2 = VALUE line2( col1 = 4 col2 = 5 col3 = 6 ).   “Restore ls_line2
ls_line2 = CORRESPONDING #( BASE ( ls_line2 ) ls_line1 ).
WRITE: / ‘ls_line2 = CORRESPONDING #( BASE ( ls_line2 ) ls_line1 )’

        , 70 ‘Result is ls_line2 = ‘, ls_line2–col1

        , ls_line2–col2, ls_line2–col3.
SKIP.

ls_line2 = VALUE line2( col1 = 4 col2 = 5 col3 = 6 ).   “Restore ls_line2
DATA(ls_line3) = CORRESPONDING line2( BASE ( ls_line2 ) ls_line1).
WRITE: / ‘DATA(ls_line3) = CORRESPONDING line2( BASE ( ls_line2 ) ls_line1 )’

         , 70 ‘Result is ls_line3 = ‘ , ls_line3–col1

         , ls_line3–col2, ls_line3–col3.

 

  • III. Output

 

  • IV. Explanation

Given structures ls_line1 & ls_line2 defined and populated as above.

給定結構ls_line1和ls_line2的定義和填充如上所述。

1、The contents of ls_line1 are moved to ls_line2 where there is a matching column name. Where there is no match the column of ls_line2 is initialised.

當有匹配的列名時將ls_line1的內容移動到ls_line2。如果沒有匹配項,則初始化ls_line2的列。

2、This uses the existing contents of ls_line2 as a base and overwrites the matching columns from ls_line1.This is exactly like MOVE-CORRESPONDING.

將ls_line2的現有內容作爲基礎,並根據ls_line1中的匹配列進行覆蓋。這完全像MOVE-CORRESPONDING

3、This creates a third and new structure (ls_line3) which is based on ls_line2 but overwritten by matching columns of ls_line1.

創建第三個新結構(ls_line3),該結構基於ls_line2,然後ls_line2被ls_line1的匹配列覆蓋。

 

  • V.  Additions MAPPING and EXCEPT

MAPPING allows you to map fields with non-identically named components to qualify for the data transfer.

映射允許您映射名字不匹配的組件字段,以符合數據傳輸的條件。

   … MAPPING  t1 = s1 t2 = s2

EXCEPT allows you to list fields that must be excluded from the data transfer.

EXCEPT允許您列出必須從數據傳輸中排除的字段。

   … EXCEPT  {t1 t2 …}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章