Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in appending data to final table

Former Member
0 Kudos
372

Hi,

I am working on a report in which i am fetching data on the basis of movement types and i am first storing the data in main itab and then i am storing the data of 321 in one itab and 101 in other as i have to display it in the comparision way,

Finally i am trying to append the data in the final itab so that i can display the data for mvt.101 and 321 for a material and calculate its date and time differnece.

19 REPLIES 19

Former Member
0 Kudos
330

So what is the issue now..?

0 Kudos
330

hi,

The problem is i am looping the both itabs and then in the end i am appending final itab. when i am trying to append the data after closing the loop for 321 mvt.typ itab it is displaying the correct data of 101 mvt. type in ifnal itab and repeating the data for 321 mvt. and vice versa...

Is there any way i can store th data coorectly in final itab.

here's the code:-



LOOP AT T_TAB2 INTO WA_TAB2.

            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.

         LOOP AT T_TAB3 INTO WA_TAB3.

            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.

          ENDLOOP.
        APPEND WA_FINAL TO T_FINAL.
      ENDLOOP.

      SORT T_FINAL BY PMBLNR.

0 Kudos
330

Hi,

Use where condition in the loop statement.

For Example.

LOOP AT it_mkpf INTO wa_mkpf.
  LOOP AT it_mseg INTO wa_mseg WHERE mblnr = wa_mkpf-mblnr.
    ...
    ...
   ENDLOOP.
ENDLOOP.

possible by comparing 321 and 101 's material number, order number, order type and plant detail you can get it.

0 Kudos
330

Hi G,

If i try it with same MBLNR it will not work as they are different when ever the migo is done for it,i.e. different MBLNR will be generated after every 101 and 321.

Even i tried to make link through MATNR field in the where condition it is continously repeating the data in the final itab.....

0 Kudos
330

Hi,

Are there same number of records in both the internal tables(t_tab2 and t_tab3), If so,


describe table t_tab2 lines v_count.  " (v_count is of type I).

Do v_count times.

read table t_tab2 into wa_tab2 index v_count.
read table t_tab3 into wa_tab3 index v_count.

move corresponding fields of wa_tab2 to wa_final.
move corresponding fields of wa_tab3 to wa_final.
append wa_final to it_final.

enddo.

check if it helps you.

Regards,

Swarna Munukoti.

0 Kudos
330

append lines of T_TAB2 to t_final.

now loop through final table then read the T_TAB3 based on the key ( say matnr ) and modify it.

0 Kudos
330

Hi,

I am trying to do as youhad said but it is still repeating the data of 2nd itab (321 mvt.type) by picking the last value of it in the final itab..

here's the code:-



 LOOP AT T_TAB2 INTO WA_TAB2.

            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.

      APPEND WA_FINAL TO T_FINAL.

   ENDLOOP.

   SORT T_FINAL BY PMBLNR.


   LOOP AT T_FINAL INTO WA_FINAL.

    READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_FINAL-QMATNR.
"      LOOP AT T_TAB3 INTO WA_TAB3 WHERE PMATNR = WA_FINAL-QMATNR.

            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.

*--------------------------------------------------------------------*
           CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
              EXPORTING
                DATE1                  =  WA_FINAL-DATE1
                TIME1                  =  WA_FINAL-TIME1
                DATE2                  =  WA_FINAL-DATE2
                TIME2                  =  WA_FINAL-TIME2
             IMPORTING
                DATEDIFF               =  WA_FINAL-DIFFD
                TIMEDIFF               =  WA_FINAL-DIFFT
*               EARLIEST               =
              EXCEPTIONS
                INVALID_DATETIME       = 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.
*--------------------------------------------------------------------*

"           ENDLOOP.
        modify T_FINAL from WA_FINAL.
      ENDLOOP.

How can i have the functionality of it that it should display the data in final itab.

0 Kudos
330

Hi,

Try this


SORT T_FINAL BY PMBLNR. 
sort t_tab3 by pmatnr.  
   LOOP AT T_FINAL INTO WA_FINAL.
     READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_FINAL-QMATNR.
     loop at t_tab3 into wa_tab3 index sy-tabix.
       if pmatnr = wa_final-qmatnr.
           WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.
           ---------------
      endif.
     endloop.

0 Kudos
330

but it is still repeating the data of 2nd itab (321 mvt.type) by picking the last value of it in the final itab

Its hard to understand your problem.

can you place the final itab data and explain it more precisely ..

0 Kudos
330

Hi,

Instaed of looping teh internal table twice.. Just do a small change in sequence of steps:


LOOP AT T_TAB2 INTO WA_TAB2.
 
            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.

READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_TAB2-QMATNR.

if sy-subrc eq 0. 
            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.
 
*--------------------------------------------------------------------*
           CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
              EXPORTING
                DATE1                  =  WA_FINAL-DATE1
                TIME1                  =  WA_FINAL-TIME1
                DATE2                  =  WA_FINAL-DATE2
                TIME2                  =  WA_FINAL-TIME2
             IMPORTING
                DATEDIFF               =  WA_FINAL-DIFFD
                TIMEDIFF               =  WA_FINAL-DIFFT
*               EARLIEST               =
              EXCEPTIONS
                INVALID_DATETIME       = 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.
*--------------------------------------------------------------------*

endif.
append wa_final to it_final. " so one time append will be enough.

CLEAR: wa_final, wa_tab2, wa_tab3. "may be the repetition is because of not using clear statement
ENDLOOP.

Regards,

Swarna Munukoti.

Edited by: Swarna Munukoti on Nov 26, 2009 10:14 AM

Edited by: Swarna Munukoti on Nov 26, 2009 10:16 AM

Edited by: Swarna Munukoti on Nov 26, 2009 10:18 AM - added clear statement

0 Kudos
330

Hi,

I had tried the way you had said and it is still is repeating the data but instead of repeating the value last value it is repating the first value.

The structure of FINAL itab is like this:-



       BEGIN OF TY_FINAL,
       QMBLNR    TYPE MSEG-MBLNR,      "Material Document
       QZEILE    TYPE MSEG-ZEILE,      "Line ID
       QMATNR    TYPE MSEG-MATNR,      "ItemID
       QWERKS    TYPE MSEG-WERKS,      "Plant
       QBWART    TYPE MSEG-BWART,      "Movement Type
       QMENGE    TYPE MSEG-MENGE,      "Quantity
       QLGORT    TYPE MSEG-LGORT,      "Storgae Locaion
       QCHARG    TYPE MSEG-CHARG,      "Batch ID
       QSHKZG    TYPE MSEG-SHKZG,      "Debit/Credit Indicator
       QMTART    TYPE MARA-MTART,      "Material Type
       QMEINS    TYPE MARA-MEINS,      "U.O.M.
       QCPUDT    TYPE MKPF-CPUDT,      "Day On Which Accounting Document Was Entered
       QCPUTM    TYPE MKPF-CPUTM,      "Time of Entry
       QITEMDESC TYPE MAKT-MAKTX,      "Material Description
       PMBLNR    TYPE MSEG-MBLNR,      "Material Document
       PZEILE    TYPE MSEG-ZEILE,      "Line ID
       PMATNR    TYPE MSEG-MATNR,      "ItemID
       PLGORT    TYPE MSEG-LGORT,      "Storgae Locaion
       PMENGE    TYPE MSEG-MENGE,      "Quantity
       PCHARG    TYPE MSEG-CHARG,      "Batch ID
       PSHKZG    TYPE MSEG-SHKZG,      "Debit/Credit Indicator
       PCPUDT    TYPE MKPF-CPUDT,      "Day On Which Accounting Document Was Entered
       PCPUTM    TYPE MKPF-CPUTM,      "Time of Entry
       PBWART    TYPE MSEG-BWART,      "Movement Type
       DATE1     TYPE D,               "Date for 101 mvt.type
       DATE2     TYPE D,               "Date for 321 mvt.type
       TIME1     TYPE T,               "Time for 101 mvt.type
       TIME2     TYPE T,               "Time for 321 mvt.type
       DIFFD     TYPE P,               "Difference for date
       DIFFT     TYPE P,               "Difference for time
       MAKTG     TYPE MAKT-MAKTG,      "Material Description
       ITEMID(6) TYPE C,               "Item Id
       END OF TY_FINAL.

0 Kudos
330

Hi,

As we are clearing the values repition should not happen, until there are some duplicate records present in the internal tables.

So check in debugging mode whether there are any duplicate records are present in tab2 and tab3.

Anyway what fields are repeating(is it tab2 or tab3) ?

Regards,

Swarna Munukoti.

0 Kudos
330

Hi,

I had checked in the debug mode there are no duplicates present in the T_TAB2 AND T_TAB3 and when i am checking the value of T_FINAL here the vales of t_tab3 are repeating ....

Here's the code:-




   LOOP AT T_TAB2 INTO WA_TAB2.

            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.

"     LOOP AT T_TAB3 INTO WA_TAB3 WHERE PMATNR = WA_TAB2-QMATNR.
      READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_TAB2-QMATNR.

        if sy-subrc eq 0.

            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.

*--------------------------------------------------------------------*
           CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
              EXPORTING
                DATE1                  =  WA_FINAL-DATE1
                TIME1                  =  WA_FINAL-TIME1
                DATE2                  =  WA_FINAL-DATE2
                TIME2                  =  WA_FINAL-TIME2
             IMPORTING
                DATEDIFF               =  WA_FINAL-DIFFD
                TIMEDIFF               =  WA_FINAL-DIFFT
*               EARLIEST               =
              EXCEPTIONS
                INVALID_DATETIME       = 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.
*--------------------------------------------------------------------*

          ENDIF.
        APPEND WA_FINAL TO T_FINAL.
        CLEAR: wa_final, wa_tab2, wa_tab3.
"        ENDLOOP.
      ENDLOOP.

      SORT T_FINAL BY PMBLNR.

0 Kudos
330

Hi,

I dont quite understand what your problem is.

But did you check with my code?

0 Kudos
330

Hi,

I had tried ur logic too but it did not worked.... i really dont why it is not able to fill the final interenal table? the common link between t_tab2 and t_tab3 is only MATNR and both the itab are getting filled corrctly..... problem comes when it is trying to fill the data in the final internal table.

here 's the code which i am really trying to make it correct:-




   LOOP AT T_TAB2 INTO WA_TAB2.

            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.

      READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_TAB2-QMATNR.

            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.

           CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
              EXPORTING
                DATE1                  =  WA_FINAL-DATE1
                TIME1                  =  WA_FINAL-TIME1
                DATE2                  =  WA_FINAL-DATE2
                TIME2                  =  WA_FINAL-TIME2
             IMPORTING
                DATEDIFF               =  WA_FINAL-DIFFD
                TIMEDIFF               =  WA_FINAL-DIFFT
*               EARLIEST               =
              EXCEPTIONS
                INVALID_DATETIME       = 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.

        APPEND WA_FINAL TO T_FINAL.
        CLEAR:  wa_tab2, wa_tab3.
      ENDLOOP.

      SORT T_FINAL BY PMBLNR.

0 Kudos
330

Hi,

Check for su-subrc eq 0 after read statement.Also clear wa_final along with wa_tab2 and wa_tab3 (else there are chances of duplicate values of tab3 in it_final).


LOOP AT T_TAB2 INTO WA_TAB2.
 
            WA_FINAL-QMBLNR = WA_TAB2-QMBLNR.
            WA_FINAL-QZEILE = WA_TAB2-QZEILE.
            WA_FINAL-QMATNR = WA_TAB2-QMATNR.
            WA_FINAL-QMENGE = WA_TAB2-QMENGE.
            WA_FINAL-QLGORT = WA_TAB2-QLGORT.
            WA_FINAL-QCHARG = WA_TAB2-QCHARG.
            WA_FINAL-DATE2  = WA_TAB2-QCPUDT.
            WA_FINAL-TIME2  = WA_TAB2-QCPUTM.
 
      READ TABLE T_TAB3 INTO WA_TAB3 WITH KEY PMATNR = WA_TAB2-QMATNR.
 
if sy-subrc eq 0. " Add this check

            WA_FINAL-PMBLNR = WA_TAB3-PMBLNR.
            WA_FINAL-PZEILE = WA_TAB3-PZEILE.
            WA_FINAL-PMATNR = WA_TAB3-PMATNR.
            WA_FINAL-PMENGE = WA_TAB3-PMENGE.
            WA_FINAL-PLGORT = WA_TAB3-PLGORT.
            WA_FINAL-PCHARG = WA_TAB3-PCHARG.
            WA_FINAL-DATE2  = WA_TAB3-PCPUDT.
            WA_FINAL-TIME2  = WA_TAB3-PCPUTM.
 
           CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
              EXPORTING
                DATE1                  =  WA_FINAL-DATE1
                TIME1                  =  WA_FINAL-TIME1
                DATE2                  =  WA_FINAL-DATE2
                TIME2                  =  WA_FINAL-TIME2
             IMPORTING
                DATEDIFF               =  WA_FINAL-DIFFD
                TIMEDIFF               =  WA_FINAL-DIFFT
*               EARLIEST               =
              EXCEPTIONS
                INVALID_DATETIME       = 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.
 
endif.

        APPEND WA_FINAL TO T_FINAL.
        CLEAR:  wa_tab2, wa_tab3, wa_final." clear wa_final as well
      ENDLOOP.
 
      SORT T_FINAL BY PMBLNR.

Now check if the problem still persists.

Regards,

Swarna Munukoti

0 Kudos
330

Hi,

I am doing it as you had said but it is still repeating the 1st row present in T_TAB3 where as i had checked in the t_tab3 there is no duplicate data present in it but it is giving problem while appending in the T_FINAL.

In T_TAB2 and T_TAB3 the common field is just MATNR and rest of the data is different as it will have material document are different due to movement types....

0 Kudos
330

Hello,

The only solution for your problem is to debug the program and check how it is populating the duplicates and take the necessary action. We cannot assume in what scenario you are executing and the data you are getting in the internal tables at runtime.

Vikranth

0 Kudos
330

hi,

ok ,i will do it... thanks for the information.