例1.求一个节点下面所有节点的某一字段之和
*&---------------------------------------------------------------------*
*& Report Z_BARRY_RECURSION2 *
*& *
*&---------------------------------------------------------------------*
*& 求一个节点下面所有节点的某一字段之和 *
*&---------------------------------------------------------------------*
AA -- BBB -- DDDD
| |
| -- EEEE
|
|- CCC -- FFFF
| |
| -- GGGG
|
|- HHH
REPORT z_barry_recursion2 .
DATA: BEGIN OF itab OCCURS 0 ,
node(10) TYPE c,
parentnode(10) TYPE c ,
folder TYPE c ,
menge TYPE i,
END OF itab.
DATA: itab2 LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: total TYPE i.
START-OF-SELECTION.
itab-menge = 1.
itab-node = 'AA'.
itab-parentnode = ''.
itab-folder = 'X'.
APPEND itab.
itab-menge = 2.
itab-node = 'BBB'.
itab-parentnode = 'AA'.
itab-folder = 'X'.
APPEND itab.
itab-node = 'CCC'.
itab-parentnode = 'AA'.
itab-folder = 'X'.
APPEND itab.
itab-node = 'HHH'.
itab-parentnode = 'AA'.
itab-folder = ''.
APPEND itab.
itab-menge = 3.
itab-node = 'DDDD'.
itab-parentnode = 'BBB'.
APPEND itab.
itab-node = 'EEEE'.
itab-parentnode = 'BBB'.
APPEND itab.
itab-node = 'FFFF'.
itab-parentnode = 'CCC'.
APPEND itab.
itab-node = 'GGGG'.
itab-parentnode = 'CCC'.
APPEND itab.
PERFORM getsum USING 'AA' total.
WRITE total .
SKIP 2.
PERFORM gettable TABLES itab2
USING 'AA' .
LOOP AT itab2.
WRITE : / itab2-node ,itab2-parentnode,itab2-menge.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form getsum
*&---------------------------------------------------------------------*
FORM getsum USING node sum.
LOOP AT itab WHERE parentnode = node .
IF itab-folder = ''.
sum = sum + itab-menge .
ENDIF.
PERFORM getsum USING itab-node sum.
ENDLOOP.
ENDFORM . "getsum
*&---------------------------------------------------------------------*
*& Form gettable
*&---------------------------------------------------------------------*
FORM gettable TABLES outtab
USING node .
LOOP AT itab WHERE parentnode = node.
IF itab-folder = ''.
APPEND itab TO outtab.
ENDIF.
PERFORM gettable TABLES outtab USING itab-node.
ENDLOOP.
ENDFORM. "gettable
例2.求一个日期的前N个月的日期
REPORT z_barry_recursion .
DATA: old_date LIKE sy-datum ,
new_date LIKE sy-datum ,
back TYPE numc3 .
old_date = '20080417'.
back = 6.
PERFORM back_months USING old_date back
CHANGING new_date.
WRITE new_date.
*&---------------------------------------------------------------------*
*& Form GET_BOM
*&---------------------------------------------------------------------*
FORM back_months USING value(currdate) value(backmonths)
CHANGING newdate.
DATA: year(4) TYPE c,
tmonths TYPE numc3,
t1months TYPE numc3,
day(2) TYPE c.
MOVE: currdate(4) TO year,
currdate+4(2) TO tmonths,
currdate+6(2) TO day.
IF tmonths <= backmonths.
currdate(4) = currdate(4) - 1.
currdate+4(2) = '12'.
backmonths = backmonths - tmonths.
PERFORM back_months USING currdate backmonths
CHANGING newdate.
ELSE.
newdate(4) = currdate(4).
newdate+6(2) = currdate+6(2).
newdate+4(2) = currdate+4(2) - backmonths.
ENDIF.
ENDFORM. "GET_BOM
阅读(6471) | 评论(0) | 转发(0) |