06-21-2007 7:58 AM
hi all,
When do we use "for all entries" and is it a good practice of programming?
06-21-2007 8:08 AM
aatmika,
Suppose you have internal table ITAB with data of MARA table .Say "matnr" is the main field.
Based on the "matnr" you want to extract some other fields form MARC table which are not in the MARA table.
Instead of writing the select statment in LOOP like
LOOP AT ITAB.
Select * from MARC into ITAB2 where matnr eq ITAB-matnr.
ENDLOOP.
This will give bad performance because "n" number of time need to access database tabel.
Do like below....
Select * from MARC into ITAB2
for all entries in ITAB
where matnr eq ITAB-matnr.
One time need to access database table for all data.
Don't forget to reward if useful....
06-21-2007 8:02 AM
Hi,
For All Entries is used to select the data from a database table into Internal table ITAb1 based upon some entries in an internal table ITAB2 which have been already fetched from interal table. This prevents us from Looping at the Internal table ITAB2 and then for each record selecting data from the database into ITAB1.
Yeah, this is a good programming practice and is more efficient in terms of performance.
Regards,
Himanshu
06-21-2007 8:02 AM
Hi,
You can only use FOR ALL ENTRIES IN ...WHERE ...in a SELECT statement.
SELECT ... FOR ALL ENTRIES IN itab WHERE cond returns the union of the solution sets of all SELECT statements that would result if you wrote a separate statement for each line of the internal table replacing the symbol itab-f with the corresponding value of component f in the WHERE condition.Duplicates are discarded from the result set. If the internal table itab does not contain any entries, the system treats the statement as though there were no WHERE cond condition, and selects all records (in the current client).
For example:
SELECT * FROM sflight INTO wa_sflight
FOR ALL ENTRIES IN ftab
WHERE CARRID = ftab-carrid AND
CONNID = ftab-connid AND
fldate = '20010228'.
This condition, return all entries of the sflight.
When using FOR ALL ENTRIES the number of matching records is restricted to the number of records in the internal table. If the number of records in the database tables is too large then join would cause overheads in performance. Additionally a JOIN bypasses the table buffering.
Check this section on "For ALL Entries" here.
http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3a1f358411d1829f0000e829fbfe/frameset.htm
http://www.thespot4sap.com/articles/SAPABAPPerformanceTuning_ForAllEntries.asp
Regards,
Padmam.
06-21-2007 8:04 AM
Hi,
When using FOR ALL ENTRIES the number of matching records is restricted to the number of records in the internal table. If the number of records in the database tables is too large then join would cause overheads in performance. Additionally a JOIN bypasses the table buffering.
So for all entries is used for filtering out the data from the two tables based on the entries in them.
Yes, it is a good practice to use For all entries insted of Joins.
Thanks,
Anitha
06-21-2007 8:05 AM
hi..
We use for all entries, when we need to fetch data from two tables. The search key is common.
Like you may need to fetch data from BKPF and BSEG. Two tables. You can fetch BELNR's from BKPF on certain conditions and then for only those BELNR's you can fetch entries from BSEG.
This is better and faster than joins.
The syntax is as follows:
Select Belnr bukrs ..
from bkpf into corresponding fields of table it_bkpf
where budat in s_budat. <Suppose>
then check whether values are fetched or not.
if not it_bkpf[] is initial.
select belnr dmbtr <etc>
from bseg
into corresponding fields of table it_bseg
for all entries in it_bkpf
where belnr = it_bkpf-belnr.
endif.
Thanks
Ashu
06-21-2007 8:08 AM
aatmika,
Suppose you have internal table ITAB with data of MARA table .Say "matnr" is the main field.
Based on the "matnr" you want to extract some other fields form MARC table which are not in the MARA table.
Instead of writing the select statment in LOOP like
LOOP AT ITAB.
Select * from MARC into ITAB2 where matnr eq ITAB-matnr.
ENDLOOP.
This will give bad performance because "n" number of time need to access database tabel.
Do like below....
Select * from MARC into ITAB2
for all entries in ITAB
where matnr eq ITAB-matnr.
One time need to access database table for all data.
Don't forget to reward if useful....
06-21-2007 8:09 AM
Hi,
FOR ALL ENTRIES is used in cases where you might have to use a select inside a loop.
Using a select inside a loop is not a good practice as the number of database fetches increase to a large extent because of which the performance of the program degrades.
In order to avoid this scenario and achieve better performance we use FOR ALL ENTRIES.
SELECT <f1> <f2>...from <databasetable>
into <itab2>
for all entries in <itab1>
where <condition>.
Loop at <itab1>.
read table <itab2> into <wa>
where <itab2>-f1 = <itab1-f1>
endloop.
Hope it is useful.
Thanks,
Sandeep.
06-21-2007 8:11 AM
hi,
for all entries can be used in place of joins when joining more than 2 tables. as joins degrades the performance of select statements when they are incresaed. at such situations u can go for for all entries. while using for all entries u shold have a minimum of one common field in tables.
if useful reward some points.
withregards,
suresh.