嵌套循环条件

时间:2013-12-05 11:00:38

标签: loops sap abap

假设我有这张表:

 TDID    TDLINE
 F04     04-AA
 F04     04-BB    <-- call a function
 F05     05-AA
 F05     05-BB    <-- call a function
 F06     06-AA    <-- call a function

我想调用一个函数,而TDID字段与前一个字段不同。我有下面的代码,它可以工作,但不知何故它不完美的工作(它错过了最后一行):

LOOP AT lines ASSIGNING <fs1>.
  IF <fs2> IS INITIAL.
    <fs2> = <fs1>.
  ELSE.
    li_line-tdline = <fs2>-tdline.
    APPEND li_line.

    IF <fs1>-tdid NE <fs2>-tdid.
      li_thead-tdid = <fs2>-tdid.

      CALL FUNCTION 'SAVE_TEXT'
        EXPORTING
          header          = li_thead
          savemode_direct = 'X'
        TABLES
          lines           = li_line

      CLEAR: li_thead,
             li_line.
      FREE:  li_thead,
             li_line.
    ENDIF.
  ENDIF.
ENDLOOP.

ANSWER

感谢 vwegert 获取答案:

LOOP AT lines ASSIGNING <fs1>.
  AT NEW tdid.
    REFRESH li_thead.
    REFRESH li_line.

    li_thead-tdid     = <fs1>-tdid.
    APPEND li_thead.
  ENDAT.

  li_line-tdline    = <fs1>-tdline.
  APPEND li_line.

  AT END OF tdid.
    CALL FUNCTION 'SAVE_TEXT'
      EXPORTING
        header          = li_thead
        savemode_direct = 'X'
      TABLES
        lines           = li_line
  ENDAT.
ENDLOOP.

2 个答案:

答案 0 :(得分:6)

假设该表按TDID排序,TDID左下方的字段变化频率高于TDID

LOOP AT lines ASSIGNING <fs1>.
  AT NEW tdid.
    REFRESH some_other_tab.
  ENDAT.
  APPEND <fs1> TO some_other_tab.
  AT END OF tdid.
    CALL FUNCTION ...
  ENDAT.
ENDLOOP.

答案 1 :(得分:0)

vwegert提到的不可预测性是因为应用Control语句的字段旁边的字符字段将转换为星号(*)。如果要在控制语句中使用这些值,请确保将值复制到临时表中并在其上循环而不是原始内部表,并使用原始内部表上的READ值。还要记住,control语句会考虑在语句中使用的列左侧的所有列的条件。