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: 

Is it possible to use where clause in the LOOP AT...ENDLOOP. statement.

Former Member
0 Kudos

Is it possible to use where clause in the LOOP AT...ENDLOOP. statement?

The general practice seen is that first using a select statement, the resultset is chosen and then LOOPING at internal table the manipulated records in the resultset are displayed as a part of output as per the requirement.

If it is possible to use where clause in LOOP AT...ENDLOOP. statement, does it lead to any performance issue or is it absurd(not a usual practice).

I am a beginer in ABAP and want to get the some ideas crystal clear.

1 ACCEPTED SOLUTION

former_member194669
Active Contributor
0 Kudos

Hi,

+

So I was under the impression that WHERE clause can only be used in SELECT STATEMENT and not in LOOP AT...ENDLOOP.

+

No this is wrong.

+

Now I hope my question is more clear to you. If I am not wrong, there can be two approaches,

1) Using a SELECT statement containing the WHERE clause, fetch a resultset and then using LOOP...ENDLOOP. statement, display the output of the report using WRITE statement.

i.e. SELECT...WHERE...ENDSELECT and then LOOP AT...ENDLOOP.

2) Using SELECT statement, fetch all possible records and using LOOP...ENDLOOP. statement filter out the records using WHERE to match the resultset and display values in that resultset using WRITE statement

i.e. SELECT...ENDSELECT and then LOOP AT...WHERE...ENDLOOP.

+

Some times the fields in the selection option will not be part of key field for table you are making select statement. In this you cannot able to use that field in the select statement ( You can but your performance get affected). So further filtering will happen only thru LOOP with WHERE clause.

LOOP with WHERE clause will be used filtering your output in the screen , ex, interactive reports, multi level report etc.

4 REPLIES 4

former_member194669
Active Contributor
0 Kudos

Hi,

LOOP Processing:

LOOP AT or into the table work area. For each line read, processed the statement block between LOOP and ENDLOOP. Within the statement block, SY-TABIX contains the index of the current line. The loop ends as soon as all lines of the table have been processed. After the ENDLOOP statement:

  • SY-SUBRC = 0 : at least one line was read;

  • SY-SUBRC = 4 : otherwise it is set to 4.

Restriction the number of lines to be processed:

  • FROM <n1> - the index of the first line to be read.

  • TO <n2> - the index of the last line to be read.

  • WHERE <condition> - the first operand of the logical expression must be a component of <itab>. Cannot be used together with the control keyword AT inside the loop.

Note. The FROM and TO options restrict the number of lines which the system has to read. The WHERE option only prevents unnecessary filling of the work area. With the WHERE option, the system must read all lines. To improve performance, you should use the FROM and TO options as much as possible. It can be also beneficial to leave the loop with the EXIT statement instead of using the WHERE option.

0 Kudos

Hi,

This one helped me in understanding the basics clearly. But I would like to further give you one example by which my question will be more clear to you and hence your further answer to it will help me in getting the idea about WHERE clause clarified.

I was required to develop a report of all held docs by the SAP USER that were created through transaction FB60, the input obtained from user should be SAP USERNAME(obligatory).

So the straight approach by me was that writing a SELECT-OPTIONS asking SAP USERNAME, and such username obtained from USER executing a report will be compared with the USERNAME stored in the column of table RFDT which stores all such held docs by a SAP USER that used transaction FB60 to temporarily hold it.

So the approach used by me was using the select statement fetch all records from the column storing the SAP USERNAMES that have held docs using FB60 transaction and where clause is used to filter out only values matching the values obtained from user i.e. using SELECT-OPTIONS.

So I was under the impression that WHERE clause can only be used in SELECT STATEMENT and not in LOOP AT...ENDLOOP.

Now I hope my question is more clear to you. If I am not wrong, there can be two approaches,

1) Using a SELECT statement containing the WHERE clause, fetch a resultset and then using LOOP...ENDLOOP. statement, display the output of the report using WRITE statement.

i.e. SELECT...WHERE...ENDSELECT and then LOOP AT...ENDLOOP.

2) Using SELECT statement, fetch all possible records and using LOOP...ENDLOOP. statement filter out the records using WHERE to match the resultset and display values in that resultset using WRITE statement

i.e. SELECT...ENDSELECT and then LOOP AT...WHERE...ENDLOOP.

Hope you will now be able to guide me better.

Regards

Ameet

former_member194669
Active Contributor
0 Kudos

Hi,

+

So I was under the impression that WHERE clause can only be used in SELECT STATEMENT and not in LOOP AT...ENDLOOP.

+

No this is wrong.

+

Now I hope my question is more clear to you. If I am not wrong, there can be two approaches,

1) Using a SELECT statement containing the WHERE clause, fetch a resultset and then using LOOP...ENDLOOP. statement, display the output of the report using WRITE statement.

i.e. SELECT...WHERE...ENDSELECT and then LOOP AT...ENDLOOP.

2) Using SELECT statement, fetch all possible records and using LOOP...ENDLOOP. statement filter out the records using WHERE to match the resultset and display values in that resultset using WRITE statement

i.e. SELECT...ENDSELECT and then LOOP AT...WHERE...ENDLOOP.

+

Some times the fields in the selection option will not be part of key field for table you are making select statement. In this you cannot able to use that field in the select statement ( You can but your performance get affected). So further filtering will happen only thru LOOP with WHERE clause.

LOOP with WHERE clause will be used filtering your output in the screen , ex, interactive reports, multi level report etc.

0 Kudos

Hi,

This was really a good answer and made many of my doubts clear.

Regards

Ameet