Ads

What is For All Entries Statement in SAP ABAP


What is For All Entries Statement in SAP ABAP


When we fetch the data from multiple tables, SELECT FOR ALL ENTRIES is the best alternative for SELECT WITH JOINS in SAP ABAP. It reduces the database load.


For All Entries Syntax:
SELECT <Fields>  From <DT> into table <Itab2> FOR ALL ENTRIES IN <itab1> WHERE <KF> = <ITAB>-<KF>.

 'FOR ALL ENTRIES' is the addition of a select statement. It allows comparing header(parent) internal table When selecting the item(child) data with internal table-filed in the where condition. Then Data is retrieved from the item table equal based on the where condition internal table key field.
What is For All Entries Statement in SAP ABAP
What is For All Entries Statement in SAP ABAP


WHERE - FOR ALL ENTRIES

Syntax

... FOR ALL ENTRIES IN itab WHERE ... col operator itab-filed ...

What is for all entries statement?


The entire Select statement is checked for each individual row of the internal table. The result data of the SELECT statement is output fetched by the individual checks. Rows that appear in duplicate are removed from the result Internal table automatically. If the internal table that is for all entries in ITAB  is empty, the whole WHERE condition is ignored, and all Records from the database are placed in the result internal table.

WHERE condition can contain multiple where conditions using AND and OR. However, if FOR ALL ENTRIES is specified, there must be at least one comparison with a column of the internal table itab that can be specified statically or dynamically. In a SELECT statement with FOR ALL ENTRIES, the addition ORDER BY can only be used with the addition PRIMARY KEY.


The addition FOR ALL ENTRIES is only possible before WHERE conditions of the SELECT statement.

The addition FOR ALL ENTRIES has the same effect as when the addition DISTINCT is specified in the definition of the selected query. Unlike DISTINCT, the rows are not always deleted from the database system but instead are sometimes first deleted from the result internal table on the application server.

The addition FOR ALL ENTRIES ignores SAP buffering for Tables with single record buffering.

Tables with generic buffering if the condition after FOR ALL ENTRIES prevents precisely one generic area from being specified exactly.

In all other cases, SAP buffering is used and the addition FOR ALL ENTRIES can be a more efficient alternative to join expressions.

If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result internal table of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result Internal Table. If <itab> is empty, the addition FOR ALL ENTRIES is ignored, and all entries are read.

You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.

Before using For all statement in Select -condition

Parent internal table must not be empty ( If it is empty, where condition fails and it will get all records from the database).

Remove all duplicate entries in the parent internal table.

The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared.

 Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.


The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:


Define all primary keys of your database table in your select statement. It ensures that you have no duplicates in your hit list

Check that the table used in the "For all entries" statement is not empty. This avoids executing the select statement if no result is expected.

"FOR ALL ENTRIES" behaves like a "SELECT DISTINCT" statement. That means all duplicates are removed. As a consequence, some relevant entries will not appear in the selection hit list!

How to use for all entries statement in the ABAP program


REPORT  Zforallentries.

*Select FOR ALL ENTRIES statement.
data: it_ekko type STANDARD TABLE OF ekko,
      wa_ekko like line of it_ekko,
      it_ekpo type standard table of ekpo,
      wa_ekpo like line of it_ekpo.

SELECT *
  UP TO 10 ROWS  "only return first 10 hits
  FROM ekko
  INTO TABLE it_ekko.
loop at it_ekko into wa_ekko.
  write:/ wa_ekko-ebeln.
endloop.

IF sy-subrc EQ 0.
* The FOR ALL ENTRIES statement only retrieves data that matches
* entries within a particular internal table.
  SELECT *
    FROM ekpo
    INTO TABLE it_ekpo
    FOR ALL ENTRIES IN it_ekko
    WHERE ebeln EQ it_ekko-ebeln.
  write:/ 'FOR ALL ENTRIES comand '.
  loop at it_ekpo into wa_ekpo.
    write:/ wa_ekpo-ebeln, wa_ekpo-ebelp.
  endloop.
ENDIF.


Conclusion

If you know more about  For All Entries Statement in SAP ABAP, please comment below, or share this post to help others.


Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced