cancel
Showing results for 
Search instead for 
Did you mean: 

Filling a field with values from multiple sources

0 Kudos

Okay, here's the problem:

I need to fill the append field ZZDMBTR in the extractstructure of 2LIS_06_INV.

This is my coding so far.

  TYPES:

BEGIN OF TY_BSEG_NEU.

  TYPES:

  EBELN TYPE BSEG-EBELN,

  EBELP TYPE BSEG-EBELP,

  BELNR TYPE BSEG-BELNR,

  BUKRS TYPE BSEG-BUKRS,

  DMBTR TYPE BSEG-DMBTR,

  GJAHR TYPE BSEG-GJAHR,

END OF TY_BSEG_NEU.

  DATA: LT_MC06M_0ITM TYPE TABLE OF MC06M_0ITM,

        ls_BSEG_NEU TYPE TY_BSEG_NEU,

        lt_BSEG_NEU TYPE TABLE OF TY_BSEG_NEU.

  FIELD-SYMBOLS: <FS_DATA> TYPE MC06M_0ITM.

  LT_MC06M_0ITM[] = C_T_DATA[].

  SELECT EBELN GJAHR EBELP DMBTR BUKRS BELNR FROM BSEG

  INTO CORRESPONDING FIELDS OF TABLE LT_BSEG_NEU

  FOR ALL ENTRIES IN LT_MC06M_0ITM

  WHERE

    BUKRS = LT_MC06M_0ITM-BUKRS AND

    GJAHR = LT_MC06M_0ITM-GJAHR AND

    EBELN = LT_MC06M_0ITM-EBELN AND

    EBELP = LT_MC06M_0ITM-EBELP AND

    BELNR = LT_MC06M_0ITM-BELNR AND

    EBELN <> ''.

  SORT LT_BSEG_NEU BY BUKRS GJAHR EBELN EBELP BELNR.

  LOOP AT LT_MC06M_0ITM ASSIGNING <FS_DATA>.

         clear lS_BSEG_NEU.

         READ TABLE LT_BSEG_NEU INTO LS_BSEG_NEU

            WITH KEY

            BUKRS = <FS_DATA>-BUKRS

            GJAHR = <FS_DATA>-GJAHR

            EBELN = <FS_DATA>-EBELN

            EBELP = <FS_DATA>-EBELP

            BELNR = <FS_DATA>-BELNR

            BINARY SEARCH.

           <FS_DATA>-ZZDMBTR = LS_BSEG_NEU-DMBTR.

   ENDLOOP.

  CLEAR C_T_DATA.

  C_T_DATA[] = LT_MC06M_0ITM[].

Problem here: I know READ can only pull the first value from LT_BSEG_NEU for the given keys but there are often more than one values in the LT_BSEG_NEU for the same key combination (EBELN GJAHR EBELP DMBTR BUKRS BELNR)

I suppose I somehow have to add up the multiple values into a local variable nested in another loop but me being a beginner I have problems wrapping my head around the logic behind.

Can anyone help me out?

View Entire Topic
former_member226999
Contributor
0 Kudos

Try this code, I have used your code and rejigged it for performance.

TYPES:

  BEGIN OF ty_bseg_neu.

TYPES:

  ebeln TYPE bseg-ebeln,

  ebelp TYPE bseg-ebelp,

  belnr TYPE bseg-belnr,

  bukrs TYPE bseg-bukrs,

  dmbtr TYPE bseg-dmbtr,

  gjahr TYPE bseg-gjahr,

END OF ty_bseg_neu.

DATA: lt_bseg_neu   TYPE TABLE OF ty_bseg_neu,

      ls_bseg_neu   TYPE ty_bseg_neu,

      lt_mc06m_0itm TYPE TABLE OF mc06m_0itm.

FIELD-SYMBOLS: <fs_data>TYPE mc06m_0itm.

* assigning data into temp working table

lt_mc06m_0itm[] = c_t_data[].

* sorting for fast select

SORT lt_mc06m_0itm BY bukrs belnr gjahr ebeln ebelp.

* removing dulicate values to increase select perfromace

DELETE ADJACENT DUPLICATES FROM lt_mc06m_0itm

      COMPARING bukrs belnr gjahr ebeln ebelp.

SELECT ebeln gjahr ebelp dmbtr bukrs belnr

  FROM bseg

  INTO CORRESPONDING FIELDS OF TABLE lt_bseg_neu

   FOR ALL ENTRIES IN lt_mc06m_0itm

WHERE  bukrs = lt_mc06m_0itm-bukrs

   AND  belnr = lt_mc06m_0itm-belnr

   AND  gjahr = lt_mc06m_0itm-gjahr

   AND  ebeln = lt_mc06m_0itm-ebeln

   AND  ebeln <> ''

   AND  ebelp = lt_mc06m_0itm-ebelp.

SORT lt_bseg_neu BY bukrs gjahr ebeln ebelp belnr.

* reassigning c_t_data values to get all records.

CLEAR lt_mc06m_0itm[].

lt_mc06m_0itm[] = c_t_data[].

LOOP AT lt_mc06m_0itm ASSIGNING <fs_data>.

  CLEAR ls_bseg_neu.

  LOOP AT lt_bseg_neu INTO ls_bseg_neu

        WHERE bukrs =<fs_data>-bukrs

          AND gjahr =<fs_data>-gjahr

          AND ebeln =<fs_data>-ebeln

          AND ebelp =<fs_data>-ebelp

          AND belnr =<fs_data>-belnr.

  <fs_data> -zzdmbtr = <fs_data>-zzdmbtr  + ls_bseg_neu-dmbtr.

  ENDLOOP.

ENDLOOP.

CLEAR c_t_data.

c_t_data[] = lt_mc06m_0itm[].

* As you are working with BSEG table which is a cluster table there could be performance issue when using EBELN  & EBELP fields in select statement as they are not in the table index. (sadly you cant create secondary index on Cluster tables)

0 Kudos

Thanks a lot. Results are looking good. We'll see how the performance goes in the prod systems. I'll take the problems as they come one by one 🙂

former_member226999
Contributor
0 Kudos

Glad to see it worked for you.

If there are performance issues in PROD then I would suggest to remove the  EBELN  & EBELP fields from the select statement.

And after selection of all records from BSEG (using BUKRS BELNR GJAHR) remove the records which are not of the EBELN & EBELP via ABAP statement. (better to put strain on ABAP engine than on database of cluster table).

Good luck for PROD.

0 Kudos

Thanks. I'll keep that in mind.