REDUCE 循环迭代构造数据,可以计算下列几种数据
1.统计表中符合要求的数据有多少行
2.循环累加计算总金额
3.循环拼接字符串
具体代码如下
代码语言:javascript复制""""准备测试数据
DATA lv_numb TYPE i .
SELECT ryear ,racct , rbukrs , tslvt ,
tsl01 ,tsl02 , tsl03 , tsl04 ,
tsl05 ,tsl06 , tsl07 , tsl08 ,
tsl09 ,tsl10 , tsl11 , tsl12 ,
tsl13 ,tsl14 , tsl15 , tsl16
FROM faglflext
WHERE rbukrs = '1000'
AND ryear = '2020'
AND racct = '8035000001'
INTO TABLE @DATA(lt_fag)
UP TO 10 ROWS.
IF lt_fag[] IS INITIAL. "
DO 10 TIMES.
APPEND INITIAL LINE TO lt_fag ASSIGNING FIELD-SYMBOL(<fs_fag>) .
DO 20 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_fag> TO FIELD-SYMBOL(<fs_viue>) .
CASE sy-index.
WHEN 1.
<fs_viue> = '2020'.
WHEN 2.
<fs_viue> = '8035000001'.
WHEN 3.
<fs_viue> = '1000'.
WHEN 4.
<fs_viue> = '0'.
WHEN OTHERS.
<fs_viue> = <fs_viue> lv_numb .
ENDCASE.
lv_numb = lv_numb 1 .
ENDDO.
ENDDO.
ENDIF.
""统计表中符合要求的数据有多少行
DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa1 IN lt_fag WHERE ( rbukrs = '1000' ) NEXT x = x 1 ).
""循环累加计算总金额
DATA(lv_sum) = REDUCE wertv12( INIT x = 0 FOR wa2 IN lt_fag NEXT x = x wa2-tslvt wa2-tsl01 wa2-tsl02 ).
""循环拼接字符串
DATA(lv_string) = REDUCE string( INIT tex = `递增数据:` FOR n = 1 THEN N 1 UNTIL n > 9 NEXT tex = tex && n ).
REDUCE语句只能计算一个数值,类型是紧跟着REDUCE的类型,可以用基本数据类型和数据元素定义,INIT后面接变量,初始化数据,for接循环数据,可以循环表,也可以累计循环,循环表就如同正常了loop循环一样,循环每一行存储到工作区,然后做计算,累计循环用for做控制数据,then做每循环一次做的修改,until作为跳出循环的条件,next作为没循环一次需要做的计算。