在循环到另一个itab期间修改itab

时间:2017-07-11 07:06:20

标签: sap abap

我想做以下

Loop at itab into wa_itab.
   wa_itab2-field1 = wa_itab-field1.
   wa_itab2-field2 = wa_itab-field2.
   wa_itab2-field3 = wa_itab-field3.
   wa_itab2-field4 = wa_itab-field4.
*how to insert in itab2 if fields 1,2 & 3 do not exist or update itab2 if fields 1,2 & 3 exist?
EndLoop.

让我解释一下。 我有字段bukrs,kunnr,date,action。 itab2包含字段bukrs,kunnr,name1(来自kna1),date01(jan),action01,date02(Feb),action02 ... date12(Dec),action12。 在itab的循环期间,我想如果itab2有bukrs和kunnr的记录,那么它将根据itab-date的月份更新日期和动作,如果它不存在(bukrs,kunnr)然后将记录插入itab2。 / p>

我希望这能解释我想要的东西。

提前致谢 利亚

PS。这是程序

*&---------------------------------------------------------------------*
*& Report  Z_COLLECTORS_ACTIONS
*&
*&---------------------------------------------------------------------*
*& Description
*&
*&---------------------------------------------------------------------*
*& Change log:
*& Date          Author        Action
*&
*&---------------------------------------------------------------------*

REPORT  z_collectors_actions.

TABLES: zcollectoraction, kna1, t001.

TYPE-POOLS : slis.

TYPES: BEGIN OF ty_totalcollectoractions,
         bukrs  TYPE zcollectoraction-bukrs,
         kunnr  TYPE zcollectoraction-kunnr,
         name1  TYPE kna1-name1,
         date01 TYPE zcollectoraction-dat,
         date02 TYPE zcollectoraction-dat,
         date03 TYPE zcollectoraction-dat,
         date04 TYPE zcollectoraction-dat,
         date05 TYPE zcollectoraction-dat,
         date06 TYPE zcollectoraction-dat,
         date07 TYPE zcollectoraction-dat,
         date08 TYPE zcollectoraction-dat,
         date09 TYPE zcollectoraction-dat,
         date10 TYPE zcollectoraction-dat,
         date11 TYPE zcollectoraction-dat,
         date12 TYPE zcollectoraction-dat,
         action01 TYPE zcollectoraction-action,
         action02 TYPE zcollectoraction-action,
         action03 TYPE zcollectoraction-action,
         action04 TYPE zcollectoraction-action,
         action05 TYPE zcollectoraction-action,
         action06 TYPE zcollectoraction-action,
         action07 TYPE zcollectoraction-action,
         action08 TYPE zcollectoraction-action,
         action09 TYPE zcollectoraction-action,
         action10 TYPE zcollectoraction-action,
         action11 TYPE zcollectoraction-action,
         action12 TYPE zcollectoraction-action,
       END OF ty_totalcollectoractions.

DATA: wa_collectoraction LIKE zcollectoraction,
      it_collectoraction LIKE TABLE OF zcollectoraction,
      wa_totalcollectoractions TYPE ty_totalcollectoractions,
      it_totalcollectoractions TYPE TABLE OF ty_totalcollectoractions WITH KEY kunnr.

DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      field_catalog TYPE lvc_t_fcat,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid,
      g_save TYPE c VALUE 'X',
      g_variant TYPE disvariant,
      gx_variant TYPE disvariant,
      g_exit TYPE c.

SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-001.
PARAMETERS: variant LIKE disvariant-variant.
PARAMETERS p_bukrs TYPE bukrs OBLIGATORY.
SELECT-OPTIONS: so_kunnr FOR kna1-kunnr OBLIGATORY,
                so_date  FOR sy-datum OBLIGATORY.
SELECTION-SCREEN END OF BLOCK bl1.

INITIALIZATION.
  gx_variant-report = sy-repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save     = g_save
    CHANGING
      cs_variant = gx_variant
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 0.
    variant = gx_variant-variant.
  ENDIF.

START-OF-SELECTION.

  SELECT bukrs kunnr dat MAX( time ) AS time
    FROM zcollectoraction INTO CORRESPONDING FIELDS OF TABLE it_collectoraction
    WHERE bukrs = p_bukrs AND
          kunnr IN so_kunnr AND
          dat   IN so_date
    GROUP BY bukrs kunnr dat.

  LOOP AT it_collectoraction INTO wa_collectoraction.
    SELECT SINGLE * FROM zcollectoraction INTO CORRESPONDING FIELDS OF wa_collectoraction
      WHERE bukrs = wa_collectoraction-bukrs AND
            kunnr = wa_collectoraction-kunnr AND
            dat   = wa_collectoraction-dat   AND
            time  = wa_collectoraction-time.

    MODIFY it_collectoraction FROM wa_collectoraction.

    wa_totalcollectoractions-bukrs = wa_collectoraction-bukrs.
    wa_totalcollectoractions-kunnr = wa_collectoraction-kunnr.

    SELECT SINGLE name1 FROM kna1 INTO wa_totalcollectoractions-name1
      WHERE kunnr = wa_collectoraction-kunnr.

    DATA lv_month TYPE n LENGTH 2.

    lv_month = wa_collectoraction-dat+4(2).

    CASE lv_month.
      WHEN '01'.
        wa_totalcollectoractions-date01 = wa_collectoraction-dat.
        wa_totalcollectoractions-action01 = wa_collectoraction-action.
      WHEN '02'.
        wa_totalcollectoractions-date02 = wa_collectoraction-dat.
        wa_totalcollectoractions-action02 = wa_collectoraction-action.
      WHEN '03'.
        wa_totalcollectoractions-date03 = wa_collectoraction-dat.
        wa_totalcollectoractions-action03 = wa_collectoraction-action.
      WHEN '04'.
        wa_totalcollectoractions-date04 = wa_collectoraction-dat.
        wa_totalcollectoractions-action04 = wa_collectoraction-action.
      WHEN '05'.
        wa_totalcollectoractions-date05 = wa_collectoraction-dat.
        wa_totalcollectoractions-action05 = wa_collectoraction-action.
      WHEN '06'.
        wa_totalcollectoractions-date06 = wa_collectoraction-dat.
        wa_totalcollectoractions-action06 = wa_collectoraction-action.
      WHEN '07'.
        wa_totalcollectoractions-date07 = wa_collectoraction-dat.
        wa_totalcollectoractions-action07 = wa_collectoraction-action.
      WHEN '08'.
        wa_totalcollectoractions-date08 = wa_collectoraction-dat.
        wa_totalcollectoractions-action08 = wa_collectoraction-action.
      WHEN '09'.
        wa_totalcollectoractions-date09 = wa_collectoraction-dat.
        wa_totalcollectoractions-action09 = wa_collectoraction-action.
      WHEN '10'.
        wa_totalcollectoractions-date10 = wa_collectoraction-dat.
        wa_totalcollectoractions-action10 = wa_collectoraction-action.
      WHEN '11'.
        wa_totalcollectoractions-date11 = wa_collectoraction-dat.
        wa_totalcollectoractions-action11 = wa_collectoraction-action.
      WHEN '12'.
        wa_totalcollectoractions-date12 = wa_collectoraction-dat.
        wa_totalcollectoractions-action12 = wa_collectoraction-action.
      WHEN OTHERS.
    ENDCASE.

    READ TABLE it_totalcollectoractions INTO wa_totalcollectoractions
                        WITH TABLE KEY kunnr = wa_collectoraction-kunnr.
    IF sy-subrc = 0.
      MODIFY it_totalcollectoractions INDEX sy-tabix FROM wa_totalcollectoractions.
    ELSE.
      INSERT wa_totalcollectoractions INTO TABLE it_totalcollectoractions.
    ENDIF.

  ENDLOOP.

  PERFORM build_fieldcatalog_agreggate.
  PERFORM display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv_report .
  gd_repid = sy-repid.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = gd_repid
      i_callback_top_of_page  = 'TOP-OF-PAGE'  "see FORM
      i_callback_user_command = 'USER_COMMAND'
      it_fieldcat             = fieldcatalog[]
      i_save                  = 'X'
      is_variant              = g_variant
    TABLES
      t_outtab                = it_totalcollectoractions
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " DISPLAY_ALV_REPORT

*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM top-of-page.
*ALV Header declarations
  DATA: t_header TYPE slis_t_listheader,
        wa_header TYPE slis_listheader,
        t_line LIKE wa_header-info,
        ld_lines TYPE i,
        ld_linesc(10) TYPE c,
        lv_butxt TYPE butxt.

  SELECT SINGLE butxt INTO lv_butxt FROM t001
    WHERE bukrs = p_bukrs.
* Title
  wa_header-typ  = 'H'.
  wa_header-info = 'Collectors'' Action Report'.
  APPEND wa_header TO t_header.
  CLEAR wa_header.
* Date
  wa_header-typ  = 'S'.
  wa_header-key = 'Date: '.
  CONCATENATE  sy-datum+6(2) '.'
               sy-datum+4(2) '.'
               sy-datum(4) INTO wa_header-info.   "todays date
  APPEND wa_header TO t_header.
  CLEAR: wa_header.
* Bukrs
  wa_header-typ  = 'S'.
  wa_header-key = 'Company: '.
  CONCATENATE p_bukrs lv_butxt INTO wa_header-info SEPARATED BY space.
  APPEND wa_header TO t_header.
  CLEAR wa_header.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = t_header.
ENDFORM.                    "top-of-page



*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_AGREGGATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog_agreggate .

  fieldcatalog-fieldname   = 'KUNNR'.
  fieldcatalog-seltext_m   = 'Customer ID'.
  fieldcatalog-col_pos     = 0.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'NAME1'.
  fieldcatalog-seltext_m   = 'Customer Name'.
  fieldcatalog-col_pos     = 1.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION01'.
  fieldcatalog-seltext_m   = 'January'.
  fieldcatalog-col_pos     = 2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION02'.
  fieldcatalog-seltext_m   = 'February'.
  fieldcatalog-col_pos     = 3.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION03'.
  fieldcatalog-seltext_m   = 'March'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION04'.
  fieldcatalog-seltext_m   = 'April'.
  fieldcatalog-col_pos     = 5.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION05'.
  fieldcatalog-seltext_m   = 'May'.
  fieldcatalog-col_pos     = 6.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION06'.
  fieldcatalog-seltext_m   = 'June'.
  fieldcatalog-col_pos     = 7.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION07'.
  fieldcatalog-seltext_m   = 'July'.
  fieldcatalog-col_pos     = 8.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION08'.
  fieldcatalog-seltext_m   = 'August'.
  fieldcatalog-col_pos     = 9.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION09'.
  fieldcatalog-seltext_m   = 'September'.
  fieldcatalog-col_pos     = 10.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION10'.
  fieldcatalog-seltext_m   = 'Octomber'.
  fieldcatalog-col_pos     = 11.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION11'.
  fieldcatalog-seltext_m   = 'Noveber'.
  fieldcatalog-col_pos     = 10.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ACTION12'.
  fieldcatalog-seltext_m   = 'December'.
  fieldcatalog-col_pos     = 11.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.


ENDFORM.                    " BUILD_FIELDCATALOG_AGREGGATE

3 个答案:

答案 0 :(得分:4)

如果itab不是太大,那么你可以这样做。

    clear: itab2[].
    Loop at itab into wa_itab.
      wa_itab2-field1 = wa_itab-field1.
      wa_itab2-field2 = wa_itab-field2.
      wa_itab2-field3 = wa_itab-field3.
      wa_itab2-field4 = wa_itab-field4.
      ...
      read table itab2 into data(ls_itab2) with key field1 = wa_itab2-field1
                                                    field2 = wa_itab2-field2
                                                    field3 = wa_itab2-field3
                                                    ... 
                                                    binary search.
      if sy-subrc = 0 .
        " modify the line
         modify itab2 from wa_itab2 index sy-tabix.
      else.
        " append the line 
         append wa_itab2 to itab2.
         sort itab2 ascending by field1 field2 field3 ... . "the key fields.
      endif. 
    EndLoop.

答案 1 :(得分:0)

FIELD-SYMBOLS: <fs_itab2> TYPE wa_itab2.
SORT itab2 BY bukrs kunnr.
LOOP AT itab INTO wa_itab.
***CHECK IF DATES EXIST***
READ TABLE itab2 ASSIGNING <fs_itab2> WITH KEY bukrs = wa_itab-bukrs
                                               kunnr = wa_itab-kunnr
                                               BINARY SEARCH.
 if sy-subrc = 0 . "if the record are founded
    <fs_itab2>-field1 = wa_itab-field1.
    <fs_itab2>-field2 = wa_itab-field2.
    <fs_itab2>-field3 = wa_itab-field3.
    ...
 else.  
  APPEND INITIAL LINE TO itab2 ASSIGNING <fs_itab2>.
  IF <fs_itab2> IS ASSIGNED.
    <fs_itab2>-field1 = wa_itab-field1.
    <fs_itab2>-field2 = wa_itab-field2.
    <fs_itab2>-field3 = wa_itab-field3.
    ...
  ENDIF.
ENDLOOP.

这就是场符号。

答案 2 :(得分:0)

您无需在此处循环,MOVE-CORRESPONDING可以对内部表(如超级按钮)执行此操作:

MOVE-CORRESPONDING src_tab TO target.

这里是将MWST价格条件选择为两个itab,然后将当前有效的条件设为无效,然后添加了几个具有新密钥的其他无效条件,最后是第二个itab {{1 }}已更新,因此其中的所有MWST条件都因添加额外的条件而失效。

lt_upd_konh