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

How to do Loop on table?

Former Member
0 Likes
1,229

Hi,

I have a database Table that have 3 column : ID, Name, Contact No. with a total of 50 record.

Problem 1: Loop through all data in each column.

Eg.

I need to do a loop on all data in ID column to check for null value. After ID column, will move to check Name column's data and lastly Contact No column' s data.

How to do that?

Problem 2: Loop through all data in only particular column.

Eg. Only want to check for ID column.

How to get the column name and do the loop on that particular column?

Regards,

Rayden

1 ACCEPTED SOLUTION
Read only

abdulazeez12
Active Contributor
0 Likes
1,194

Move the database table entries to an internal table with the columns that you want and make the search..

12 REPLIES 12
Read only

abdulazeez12
Active Contributor
0 Likes
1,195

Move the database table entries to an internal table with the columns that you want and make the search..

Read only

0 Likes
1,194

Hi Shakir,

How do i move the column that is needed to itab?

Regards,

Rayden

Read only

0 Likes
1,194

Hi Rayden-

Declare an internal table with the field of your database table:

Data: begin of itab occurs 0,

ID type Database tabel-ID,

end of itab.

select ID from DBtable into table itab.

then do the processing..

loop at itab where ID = <condition>.

endloop.

Hope this will help.

Read only

JozsefSzikszai
Active Contributor
0 Likes
1,194

hi Rayden,

What do you want to achieve by these looping?

thanks

ec

Read only

0 Likes
1,194

Hi Eric,

I want to do a simple validation on the table, by using ABAP code. Eg. To validate mandatory for certain column or all column. Any idea how to do that?

Regards,

Rayden

Read only

0 Likes
1,194

Hi

decalre a internal table of type ur database table

and write a select query like what ever fileds you want from that data base , if you want all then select *

and store that values in that internal table

and loop at that internal table into workarea of that table

<b>ex</b> LOOP AT IT_SOBID INTO WA_SOBID.

then it will capture total line values

ID name AND etc..

then you cn chek that very esyly

<b>reward if usefull</b>

Read only

0 Likes
1,194

Hi Naresh,

If i were to do a loop on all data for 3 column. Let say there is certains field is blank. So i want to get the row number and column name for the error field how to do that? Any code sample for reference?

Regards,

Rayden

Read only

0 Likes
1,194

Rayden, Check the code given below......

data: itab like table of Ztable with header line.

select * from ztable into table itab.

loop at itab.

write:/ ITAB.

if itab-id is initial.

write: 'ID is INITIAL'.

endif.

if itab-name is initial.

write: 'Name is INITIAL'.

endif.

if itab-contact is initial.

write: 'Contact is INITIAL'.

endif.

endloop.

Reward points if useful, get back in case of query...

Cheers!!!

Read only

Former Member
0 Likes
1,194

Hi,

you can write:

loop at itab into wa where ID <> ' '.

endloop.

in this loop it will check for ID field only.

Read only

0 Likes
1,194

Hi Sonali,

So if i have 3 column to loop i have to code 3 loop statement? How to do it in a more dynamic way. Eg. User select Name. The Loop statement will able to loop the Name column, or ID for ID column. By using 1 loop statement. How to achieve that?

Regards,

Rayden

Read only

Former Member
0 Likes
1,194

Hi Rayden,

Its very simple.

Get the records those you want into an internal table using <b>SELECT</b> statement.

Now your internal table is having 3 fields with ID, Name and Contact.

If you want to find only records in which ID is null... U can use loop as below.

loop at itab where ID is initial.


endloop.

And one more thing is you can't loop through at only one field. If you are looping means entaire record will be processed... Ofcourse there are some cases with Control break statements like... AT NEW, AT END OF, etc,.

Reward If Helpful.

Regards

--

Sasidhar Reddy Matli.

Read only

Former Member
0 Likes
1,194

try like this

first write

data: begin of itab occurs 0,

id type ..........

name type ..............

contact number ...........

end of itab.

select-options: s_id ........................

select id

name

contact from [u r ztable]

in to table itab

where id = s_id.

do the sorting criteria ............

sort itab by id.

delete adjacent duplicate records..........

then write

if itab[] is not initial.

then write loop........

loop at itab..

if itab-id = ' ' and name = ' ' and contact = ' '.

do processing

else.

....

.....

....

endif.

if u want to check the id name contact indvidually

write if id = ' '

move it into work area

if name = ' '.

.....

i hope this would be helpful

reward points if helpful...............