Skip to Content
avatar image
Former Member

collect statement

hi

I am having a problem while using COLLECT statement.

I have a table ITAB1 with field WERKS.

I want collect statement to run only if ITAB-WERKS is not equal to 2130. and the output is required in table ITAB.

This is the code i have written. without IF statement the output is coming as expected but with IF statemnt its not working . can anyone suggest what is to be done.

code:

LOOP AT ITAB1.

IF ITAB1-WERKS NE '2130'.

MOVE CORRESPONDING ITAB1 INTO ITAB.

COLLECT ITAB.

ELSE.

MOVE CORRESPONDING ITAB1 INTO ITAB.

ENDIF.

ENDLOOP.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    Oct 26, 2007 at 01:22 PM

    Hello Gautam,

    Try this way:

    LOOP AT ITAB1 where WERKS NE '2130'.
    MOVE CORRESPONDING ITAB1 INTO ITAB.
    COLLECT ITAB.
    endloop.
    
    LOOP AT ITAB1 where WERKS EQ '2130'.
    MOVE CORRESPONDING ITAB1 INTO ITAB.
    append itab.
    ENDLOOP.

    Regards,

    Naimesh Patel

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2007 at 01:24 PM

    Hi gautam,

    Try like this...

    LOOP AT ITAB1 where WERKS NE '2130'.

    MOVE CORRESPONDING ITAB1 INTO ITAB.

    COLLECT ITAB.

    ENDLOOP.

    and can you to tell me what are all other fields in itab1.

    Message was edited by:

    Perez C

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2007 at 01:24 PM

    Hi,

    try this:

    LOOP AT ITAB1 where WERKS <> '2130'.

    MOVE CORRESPONDING ITAB1 INTO ITAB.

    COLLECT ITAB.

    ENDLOOP.

    Regards, Dieter

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2007 at 01:25 PM

    Hi Gautam,

    Try this:

    LOOP AT ITAB1 where WERKS NE '2130'.

    MOVE CORRESPONDING ITAB1 INTO ITAB.

    COLLECT ITAB.

    endloop.

    Reward if Useful

    Regards,

    Chitra

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2007 at 01:48 PM

    you can not move both branches into itab

    LOOP AT itab1.
      IF itab1-werks NE '2130'. 
        MOVE CORRESPONDING itab1 INTO itab.
        COLLECT itab.
      ELSE.
        MOVE CORRESPONDING itab1 INTO itab2.
        APPEND itab2.
      ENDIF.
    ENDLOOP.
    
    APPEND LINES OF itab2 to itab. 
    

    Yours did not work because there is no append for the else branch.

    And the Collect does not like it if you change the table itab with other

    command. The collect will then becomes extremely slow (implicit hashed

    key is destroyed) !

    Siegfried

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 26, 2007 at 03:22 PM

    be aware, the solution above with the 2 loops at WHERE is slower as the single loop!

    This is because the LOOP AT WHERE can not find efficiently the lines fulfilling the WHERE condition. For a standard table no sort order can be assumed.

    Add comment
    10|10000 characters needed characters exceeded