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: 
Read only

Loop internal table

Former Member
0 Likes
2,272

I need to loop my internal table. Need to perform if else statement in the loop.

What is the coding for looping internal table? I also need to check if i have successfully loop all the rows.

Will reward points immidiately if my question is solved. Thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,213

DATA: BEGIN OF itab OCCURS 10,

qty type I,

END OF itab.

DO 25 TIMES. itab-qty = sy-index. APPEND itab.

ENDDO.

LOOP AT itab.

if itab-qty ne 0.

WRITE: /1 itab-qty.

endif.

ENDLOOP.

20 REPLIES 20
Read only

Former Member
0 Likes
2,213

Loop at itab into watab.

if watab-qty < 100.

watab-count = watab-count + 1.

elseif watab-qty > 100 and watab-qty < 200.

watab-range = watab-range + 1.

endif.

modify itab from watab.

endloop.

Read only

Former Member
0 Likes
2,213

Hi,

Try this.


loop at itab.
  if itab-field1 > 1.
     processing...
  else.
    processing.
  endif.
endloop.

Or in the ABAP editor, place your cursor in the loop word and press F1 so you can see the documentation for LOOP statement. The same for the IF statement. You can learn a lot from their docs.

Read only

former_member404244
Active Contributor
0 Likes
2,213

Hi,

do like this

Loop at itab.

if itab-field1 eq <some value>.

do this

else.

do this.

exit.

endloop.

U can get number of lines in an internal table

describe table itab lines v_lines.

Regards,

Nagaraj

Read only

Former Member
0 Likes
2,213

Hi,

LOOP AT itab result [cond].

if result [cond]

write :

else

write :

...

ENDLOOP

u can check the sy-tabix whether u have looped at all the records.

Regards,

Sai

Read only

Former Member
0 Likes
2,213

Hi,

data line_count type i, row_count type i.

describe itab lines line_count.

Loop at itab.

if condition

row_count = row_count + 1,

else.

row_count = row_count + 1,

endif.

endloop.

if line_count = row_count.

all rows processed.

endif.

Reward if useful!

Read only

Former Member
0 Likes
2,213

Can i have the declaration and all the fields input?

I'm new to ABAP. I need sample code to guide me through.

I need a working code so i can understand how it works.

Read only

Former Member
0 Likes
2,213

Hi,

Same i have given,

data line_count type i, row_count type i.

data: itab type table of mara.

data: wa_itab like line of itab.

start-of-selection.

select * from mara into table itab.

describe itab lines line_count.

Loop at itab into wa_itab.

if wa_itab-matnr = 'A'

row_count = row_count + 1,

else.

row_count = row_count + 1,

endif.

endloop.

if line_count = row_count.

all rows processed.

endif.

Reward if useful!

Read only

Former Member
0 Likes
2,214

DATA: BEGIN OF itab OCCURS 10,

qty type I,

END OF itab.

DO 25 TIMES. itab-qty = sy-index. APPEND itab.

ENDDO.

LOOP AT itab.

if itab-qty ne 0.

WRITE: /1 itab-qty.

endif.

ENDLOOP.

Read only

Former Member
0 Likes
2,213

I have a database table name ZSTUD04.

This is my code. Is there something wrong?

The write statement does not appear.

Actually i'm doing validation methods. If the data in the database table

does not telly with the validation rules(stored in OS), it will highlight the cell that has something wrong. So my supervisor ask me to do a loop on all rows first den check according to the OS table.

Validate if the fields are mandatory, data type, unique check... etc.

Simply has no idea on how to do it.

TABLES ZSTUD04.

data: itab TYPE TABLE OF ZSTUD04,

watab TYPE ZSTUD04.

loop at itab into watab.

if watab-eyear > 2005.

  • processing...

WRITE 'Above 2005'.

else.

  • processing.

WRITE 'Below 2005'.

endif.

endloop.

Read only

0 Likes
2,213

hi jocelyn,

is this code complete .. i mean where r u moving the data into the internal table specified.

Read only

0 Likes
2,213

Hi,

ur code seems to be ok..only thing u have to check whether u have data in the internal table iatb..Also put a brak point at "LOOP AT ITAB INTO WATAB" and see whether ur write statement is triggering or not.

Regards,

Nagaraj

Read only

Former Member
0 Likes
2,213

hi

to move dat to the corresponding internal table use select statement.

Read only

Former Member
0 Likes
2,213

Ermmm... i'm trying to create an internal table for my ZSTUD04 database table.

data: itab TYPE TABLE OF ZSTUD04,

watab TYPE ZSTUD04.

Is this the way to declare an internal table? Is watab my internal table?

Sorry I'm new to ABAP language.

Get very confuse with the overwhelming infomation in the forum.

Read only

0 Likes
2,213

hi,

the internal table declaration is fine.. only thing is you have to copy data from zstud04 to itab.

you can use :

select * from zstud04 into corresonding fields of itab.

after that you can proceed with your if-else conditions.

hope this helps..

Read only

0 Likes
2,213

hi,

Yes its an internal table.

<b>data: itab TYPE TABLE OF ZSTUD04,

watab TYPE ZSTUD04</b>.

to make watab an work area, the declaration should be

<b>data: itab TYPE TABLE OF ZSTUD04, "internal table

data: watab LIKE line of itab.</b> " work area

Read only

Former Member
0 Likes
2,213

I have added. But still nothing appear. This is my code.

Any suggestion to do it another way?

TABLES ZSTUD03.

data: itab TYPE TABLE OF ZSTUD03,

watab TYPE ZSTUD03.

loop at itab into watab.

select * from ZSTUD03 into corresponding fields of watab.

ENDSELECT.

if watab-eyear > 2005.

  • processing...

WRITE 'Above 2005'.

else.

  • processing.

WRITE 'Below 2005'.

endif.

endloop.

Read only

0 Likes
2,213

TABLES ZSTUD03.

data: itab TYPE TABLE OF ZSTUD03,
watab TYPE ZSTUD03.
select * from ZSTUD03 into table itab.

loop at itab into watab.

if watab-eyear > 2005.
* processing...
WRITE 'Above 2005'.
else.
* processing.
WRITE 'Below 2005'.
endif.
endloop. 


reward if useful.

Read only

0 Likes
2,213
TABLES ZSTUD03.

data: itab TYPE TABLE OF ZSTUD03.
data: watab like line of itab.

select * from ZSTUD03 into corresponding fields of table itab.

loop at itab into watab.
if watab-eyear > 2005.
* processing...
WRITE 'Above 2005'.
else.
* processing.
WRITE 'Below 2005'.
endif.
endloop. 

<b>Hope this works.</b>

Message was edited by:

Runal Singh

Read only

Former Member
0 Likes
2,213

try this..

TABLES ZSTUD03.

data: itab TYPE TABLE OF ZSTUD03,

watab like line of ZSTUD03.

select * from ZSTUD03 into corresponding fields of itab.

endselect.

loop at itab into watab.

if watab-eyear > 2005.

  • processing...

WRITE 'Above 2005'.

else.

  • processing.

WRITE 'Below 2005'.

endif.

endloop.

Message was edited by:

ritika malhotra

Read only

Former Member
0 Likes
2,213

Thanks everyone for the response.

I can understand now how to do a basic loop.