Application Development 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: 

collect statement

gautam_totekar
Active Participant
0 Kudos

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.

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

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

6 REPLIES 6

naimesh_patel
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

try this:

LOOP AT ITAB1 where WERKS <> '2130'.

MOVE CORRESPONDING ITAB1 INTO ITAB.

COLLECT ITAB.

ENDLOOP.

Regards, Dieter

Former Member
0 Kudos

Hi Gautam,

Try this:

LOOP AT ITAB1 where WERKS NE '2130'.

MOVE CORRESPONDING ITAB1 INTO ITAB.

COLLECT ITAB.

endloop.

Reward if Useful

Regards,

Chitra

former_member194613
Active Contributor
0 Kudos

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

former_member194613
Active Contributor
0 Kudos

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.