新语法(一)更新中

  1. Value…for
 lt_andon = VALUE zhr_t_andon_per( FOR lw_data IN lt_person1

                 ( CORRESPONDING #( lw_data ) ) ).
     
     " Base 基于原内表数据 == APPEND
itab1 = VALUE #( BASE itab1
    col1 = 10 ( col2 = 42 col3 = 43 col4 = 44 )
              ( col2 = 32 col3 = 33 col4 = 34 ) ).

" APPEND
APPEND VALUE #( col1 = 8 col2 = 42 col3 = 43 col4 = 44 ) TO itab1.            

VALUE #( FOR IN selscreen
WHERE ( selname = ‘S_LADGR’ )
( CORRESPONDING #( ) ) ).

有一个实例可以参考

内表取值

    SELECT * FROM @lt_delive_who AS a
          WHERE a~who IN s_who
          AND a~crea_date IN s_erdat
          AND a~crea_time IN s_erzet
      INTO CORRESPONDING FIELDS OF TABLE  @data(zewm_s_delive_who_new)  .
    "TYPES t_new TYPE table of ZEWM_S_DELIVE_WHO with empty key.

lt_delive_who为内表
可以把以上语法改为

 DATA: lt_new TYPE TABLE OF zewm_s_delive_who.
    lt_new = VALUE #( FOR wa_who IN lt_delive_who WHERE ( who IN s_who AND crea_date IN s_erdat AND crea_time IN s_erzet )  ( wa_who ) ).

  1. Filter
    type STANDARD TABLE OF ty_person
    with NON-UNIQUE SORTED KEY (定义类型)
    定义例子为:
    lt_person1 TYPE HASHED TABLE OF zhr_s_per_post WITH UNIQUE KEY pernr,
    lt_andon TYPE SORTED TABLE OF zhr_s_andon_per WITH NON-UNIQUE KEY pernr orgeh,
    lt_andon1 TYPE SORTED TABLE OF zhr_s_andon_per WITH UNIQUE KEY pernr,
  lt_andon = FILTER #( lt_andon IN lt_pro_data
                            WHERE pernr = userid ).
  1. select

       SELECT SINGLE CAST( right( sobid ,8 ) AS NUMC )   "截取后8位,改变字段类型为NUMC
       FROM hrp1001
       WHERE objid = @ls_posit-objidup
       AND otype = 'S'
       AND plvar = '01'
       AND relat = '007'
       AND sclas = 'C'
       AND endda >= @sy-datum
       AND begda <= @sy-datum
       INTO  @<fs_per>-jobnumdup.
    

还可以取一列的最大值

SELECT
MATNR
INTO TABLE @DATA(LT_MAKT)
FROM MAKT
UP TO 1 ROWS
ORDER BY MATNR DESCENDING.

某些类型取某工厂+库存地点+序号拼起来的,则可以在WHERE 中使用LIKE

SELECT
MATNR
INTO TABLE @DATA(LT_MAKT)
FROM MAKT
WHERE MATNE LIKE ‘111%’.

lines

读取内表行数两种方法:
DATA(lv_line2) = lines( lt_sflight ).
读取底表行数:
"查询航班表中按照carrid分组的行数
SELECT
carrid ,
COUNT( carrid ) AS line3
FROM sflight
GROUP BY carrid
INTO TABLE @DATA(lt_table) .

line_exists

IF line_exists( flight_tab[ carrid = ‘XM’
connid = ‘6688’ ] ).
"ACTION
ENDIF.

LINE_INDEX

NEW:
DATA(indx) = line_index( flight_tab[ carrid = ‘XX’
connid = ‘88’] ).

VALUE 操作符

(1)
CLASS c1 DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE i,
END OF t_struct.
CLASS-METHODS m1 IMPORTING p TYPE t_struct.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE:‘col1:’ , p-col1,‘col2:’,p-col2.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
c1=>m1( VALUE #( col1 = 1 col2 = 2 ) ).

(2)
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 ) ).
cl_demo_output=>DISPLAY( itab ).

(3)
TYPES t_date_tab TYPE table of string with empty key.
DATA(date_tab) = VALUE t_date_tab(
( | { conv d( sy-datlo - 1 ) DATE = environment } | )
( | { sy-datlo DATE = environment } | )
( | { conv d( sy-datlo + 1 ) DATE = environment } | ) ).

cl_demo_output=>DISPLAY( date_tab ).

WITH KEY 中的检索条件比较符不能使用‘<>’(不等于)

如果只想取得内部表中不等于某条件的一条记录,那么请使用下面变通方法。
LOOP AT itab WHERE KEY <> ‘XX’.
此处取得第一条记录。
EXIT.
ENDLOOP.

FOR ALL ENTRIES IN
1、使用该选项后,对于最后得出的结果集系统会自动删除重复行。因此如果你要保留重复行记录时,记得在SELECT语句中添加足够字段
2、FOR ALL ENTRIES IN后面使用的内部表itab如果为空,将查出当前CLIENT端所有数据(即忽略整个WHERE语句,其他条件都会被忽略)

用 INNER JOIN代替FOR ALL ENTRIES IN
INNER JOIN 内表的时候,如果内表为空,则没有查找到数据,但是内表中如果有重复行,查找出来的数据也会有重复行。(关联的内表不能有同名的工作区/表头)

  1. 内表必需是不带表头;
  2. 内表与数据库表连接字段长度可以不一致
![在这里插入图片描述](https://img-blog.csdnimg.cn/6f5ef5ab1a4f45349346416f95a71e5f.png)

  SELECT
    acdoca~gjahr,
    acdoca~belnr,
    acdoca~docln
  FROM acdoca
  INNER JOIN @内表 AS a ON 条件
  INTO TABLE @DATA(lt_data)."赋值放在最后一行

或者用exists代替

SELECT *
  FROM mseg
  INTO TABLE lt_mseg
 WHERE EXISTS ( SELECT * FROM m_mbmps WHERE smbln = mseg~mblnr
                                        AND sjahr = mseg~mjahr
                                        AND smblp = mseg~zeile ).

MOVE-CORRESPONDING

LT_TAB1 = CORRESPONDING #( LT_TAB ).

LOOP GROUP BY

LOOP AT ITAB INTO DATA(WA) GROUP BY WA-C1.
 
    DATA(ITAB1) = VALUE ITAB( ).
 
    LOOP AT GROUP WA INTO DATA(WA1).  "遍历分组后得结果机
       ITAB1  = VALUE ITAB( BASE ITAB1 ( WA1 ) ).  "将结果集追加进表 BASE得用法
 
    ENDLOOP.
 
*   对分组后得数据进行累加
    DATA(SUM) = REDUCE I( INIT RES = 0 FOR WA2 IN ITAB1
 
                               NEXT RES = RES + WA2-C4
                      ) .
 
    IF ITAB1 IS NOT INITIAL.
      DATA(LS_ITAB) = ITAB1[ 1 ].
 
      LS_ITAB-C4 = SUM.
 
      ITAB3 = VALUE ITAB( BASE ITAB3 ( LS_ITAB ) ).
    ENDIF.
 
 
  ENDLOOP.

自定义分组

LOOP AT ITAB ASSIGNING FIELD-SYMBOL(<FS_AB>)
    GROUP BY COND  STRING(
        WHEN <FS_AB>-C1 = 'A' THEN |START|
        WHEN <FS_AB>-C1 = 'B' THEN |MIDDLE|
        ELSE |END|
        )
    INTO DATA(GROUP).
 
    IF GROUP = 'START'.
      MEMBERS = VALUE #( FOR G1 IN GROUP GROUP ( G1 ) ).
    ENDIF.
 
  ENDLOOP.

引用: S4新语法

前导零

X = |{ X ALPHA = OUT }|去X的前导零,用X= |{ X ALPHA = IN }|为X加前导零

有的会不起作用,需注意。例如新物料号,订单行号。。。。。。

参考:1

         [2.](https://www.cnblogs.com/mingdashu/p/6744637.html)

abap 字符串替换 (正则)

           REPLACE ALL OCCURRENCES OF 'A' IN lv_string WITH 'B'.  (将lv_string 中所有A 换为B ) 
     IF sy-subrc = 0.
     ENDIF.

     REPLACE ALL OCCURRENCES OF 'ABC' IN lv_string WITH space. (将lv_string 中 所有 ABC 换为 空 )
     IF sy-subrc = 0.
     ENDIF.
      

     (REGEX 表示使用正则    \d 表示单个字符    \w+  表示多个字符 )

     REPLACE ALL OCCURRENCES OF REGEX ' rownumber="\d"' IN lv_string WITH ''.
     REPLACE ALL OCCURRENCES OF REGEX 'rownumber=(")(\w+)(")' IN lv_xml WITH ''.
     IF sy-subrc = 0.
     ENDIF.
  1. RANGES语句:要用与选择表相同的结构创建内表,可使用RANGES语句,如下所示:
ranges s_HKONT for bsis-HKONT.

s_HKONT-sign = 'I'.
s_hkont-option = 'BT'.
s_hkont-low = '0010010000'.
s_hkont-high = '0010019999'.

append s_hkont.

s_HKONT-sign = 'I'.
s_hkont-option = 'BT'.
s_hkont-low = '0010020000'.
s_hkont-high = '0010029999'.

append s_hkont.

DESCRIBE TABLE lt_mat LINES lv_cont.

这行的意思是 计算内表 lt_mat 的行数 ,将行数放到变量 lv_cont 里。

该语句可以判断内表是否为空,使用语句: DESCRIBE TABLE ITAB LINES VARIABLE.判断系统变量sy-subrc是否为0,如果为0就表示不空,反之为空!

 DESCRIBE TABLE ITAB LINES VARIABLE

IF NOT P_VARIANT IS INITIAL.
    WX_VARIANT-VARIANT = P_VARIANT.
ENDIF.

TYPES:BEGIN OF ty_tab,
        tabix TYPE sy-tabix,
      END OF ty_tab.
DATA:lt_tab TYPE TABLE OF ty_tab.

lt_tab = VALUE #( ( tabix = 1 )
                  ( tabix = 1 )
                  ( tabix = 1 )
                  ( tabix = 2 ) ).

"表记录是否存在
CHECK line_exists( lt_tab[ tabix = 1 ] ).

"获取行号
DATA(lv_index) = line_index( lt_tab[ tabix = 1 ] ).

"获取行内容
DATA(ls_tab) = lt_tab[ tabix = 1 ].

"获取满足条件的行数
DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa IN lt_tab WHERE ( tabix = 1 ) NEXT x = x + 1 ).

"合计行数据
DATA(lv_tabix) = REDUCE i( INIT x = 0 FOR wa IN lt_tab WHERE ( tabix = 1 ) NEXT x = x + wa-tabix ).

substring截取字符串
concat合并字符串

 SELECT SINGLE edatu, ezeit, vbeln FROM XXXX as a               
        WHERE
        substring( ,1,10 ) = a~vbeln
        AND a~posnr = concat( '00' , substring( ,11,14 ) )
        INTO CORRESPONDING FIELDS OF .

FOR LOOP

单表的 FOR 迭代
FOR 也称为迭代表达式。

这是表 lt_flights 的简单 For 循环。Index 关键字在这里是可选的。此代码只是将数据从一个表传输到另一个表。

lt new flights = VALUE #(
FOR ls flight IN lt flights INDEX INTO lv index
seq_num = lv index
carrier = lt scarr[ carrid = ls flight-carrid ]-carrname
connect = ls flight-connid
fldate = ls flight-fldate)
).

与 LOOP AT 类似,您也可以编写除强制 ( ) 之外的 where 条件。

"Nested FOR Iterations
lt_new_flights =
  VALUE #(
    FOR ls_scarr in lt_scarr
    FOR ls_flight IN lt_flights WHERE ( carrid = ls_scarr-carrid )
    (
      carrier = ls_scarr-carrname
      connect = ls_flight-connid
      fldate  = ls_flight-fldate
    )
  ).

LET 可用于定义变量并将它们分配给目标表字段。

TYPES : BEGIN OF ty_flight,
          seq_num type i,
          carrier TYPE s_carrname,
          connect TYPE s_conn_id,
          fldate  TYPE s_date,
        END OF ty_flight.

DATA lt_new_flights TYPE STANDARD TABLE OF ty_flight.

SELECT * FROM sflight INTO TABLE @DATA(lt_flights).
IF sy-subrc EQ 0.
  SELECT * FROM scarr INTO TABLE @DATA(lt_scarr).

ENDIF.

"FOR Iteration 
lt_new_flights =
  VALUE #(
    FOR ls_flight IN lt_flights INDEX INTO lv_index
                                WHERE ( carrid = 'AA' AND
                                        connid = '0017' )
    LET lv_carrname = lt_scarr[ carrid = ls_flight-carrid ]-carrname
    IN  carrier = lv_carrname
    ( seq_num = lv_index
      connect = ls_flight-connid
      fldate  = ls_flight-fldate
    )
  ).

cl_demo_output=>display( lt_new_flights ).

嵌套 FOR 迭代
这类似于 LOOP 中的 LOOP。这样可以嵌套多个 FOR 迭代。

"Nested FOR Iterations
lt_new_flights =
  VALUE #(
    FOR ls_scarr in lt_scarr
    FOR ls_flight IN lt_flights WHERE ( carrid = ls_scarr-carrid )
    (
      carrier = ls_scarr-carrname
      connect = ls_flight-connid
      fldate  = ls_flight-fldate
    )
  ).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值