0% found this document useful (0 votes)
245 views4 pages

ABAP For ALL ENTRIES - Why You Need To Include KEY Fields - ABAP Help

When using the ABAP FOR ALL ENTRIES statement, it is important to include all key fields from the selected table in both the internal table and SELECT statement. Failing to do so can result in duplicate records being dropped from the result set, causing data inconsistencies. This is because the database will only retain unique records based on the fields selected. Including all key fields ensures each record in the result set is unique. The article provides an example demonstrating fewer records are returned when a non-key field is selected instead of all fields.

Uploaded by

Sameer Salam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views4 pages

ABAP For ALL ENTRIES - Why You Need To Include KEY Fields - ABAP Help

When using the ABAP FOR ALL ENTRIES statement, it is important to include all key fields from the selected table in both the internal table and SELECT statement. Failing to do so can result in duplicate records being dropped from the result set, causing data inconsistencies. This is because the database will only retain unique records based on the fields selected. Including all key fields ensures each record in the result set is unique. The article provides an example demonstrating fewer records are returned when a non-key field is selected instead of all fields.

Uploaded by

Sameer Salam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2/18/2016

ABAPFORALLENTRIESWhyyouneedtoincludeKEYfields|ABAPHelp

ZEVOLVING
> Home > Performance FOR ALL ENTRIES Why you need to include KEY fields

FOR ALL ENTRIES Why you need to include KEY fields


By Naimesh Patel | March 20, 2014 | Performance |

10,226 | 3

ABAPFORALLENTRIESishandy,butwouldcreatelotofdatainconsistenciesifyoudontuseitproperly.

Basic
Forsimplestuseof FORALLENTRIES ,youwouldwriteyourSELECTqueryusingtheFORALLENTIRESanduseoneormorefieldsfromthetableinto
WHEREcondition.

IFt_idsISNOTINITIAL.
SELECT*
INTOTABLEt_t100_all
FROMt100
FORALLENTRIESINt_ids
WHEREarbgbLIKE'0%'
ANDmsgnr=t_idsTABLE_LINE.
ENDIF.

Problem
Whenyourtablehasfewkeyfields,yougenerallytendtoselectthem,eventhoughyoudontneedthem.ButwhentherearemanyFieldsyoutendtonotinclude
theminyourtableandsubsequentlyinyourSELECTquery.
Forexample,letsseethisquery:
Incorrect FOR ALL ENTRIES

SELECTryear
drcrk
rpmax
rtcur
racct
rbukrs
rcntr
kokrs
hsl01
hsl02
hsl03
hsl04
hsl05
hsl06
hsl07
hsl08
hsl09
hsl10
hsl11
hsl12
FROMfaglflext
INTOTABLElt_fagl
FORALLENTRIESINlt_skb1
WHEREryearINlr_gjahr
ANDracct=lt_skb1saknr
ANDrbukrs=lt_skb1bukrs.

Sincethisquerydoesnthaveallthekeyfields,youwouldrunintotheissueswhenyouwilltrytoreconcileitwithsomeofthestandardtransactionFAGLB03.
TheissuewillhappenwhenyouhavesamepostingforsameGLusingsameCCindifferentperiods.Inthatcase,systemwouldonlybringoneentryinsteadof
themultiple.Ontheotherhandthestandardtransactionwouldgetyoualltheentrieswouldadifferenttotal.
WhenyoudebugthestdtransactionandreachtoSELECT,youwouldthink,thisisthesamequeryIhave.Thanwhymyquerywontwork.Soyoudownloadthe
datafrombothoftheITABYourprogramandStdtransaction.Youcomparethemandyouwouldnoticefewentriesaredefinitelymissing.Afterfewroundsof
VLOOKUPandCompareinexcel,yousuddenlyrealizethatFORALLENTRIESisdroppingfewentries.Bythetime,youarealreadyexpertinExcel.
TofixthisyouwouldthanrealizethatyouneedtoincludemorekeyfieldsintotheSELECT.NexthurdlewouldbetoexplaintoyourFunctionalcounterpartand

http://zevolving.com/2014/03/abapforallentrieswhyyouneedtoincludekeyfields/

1/4

2/18/2016

ABAPFORALLENTRIESWhyyouneedtoincludeKEYfields|ABAPHelp

youruserswhyitwasnotworking.
Ifthisprogramwasnotdevelopedbyyou,Icanimaginehowmuchyouhatethatpersonwhenyourealizedtherootcause.Asillymistake!Onbrightside,you
arenowExcelexpert

Why you need Key fields


Toimprovetheperformance,youwoulddefinitelywanttohave uniquevaluesintheITABwhichyouareusingasFORALLENTIRES .Ifyoudontpassunique
values,DBwouldreselecttherecordsforeachduplicatevalues.Finally,DBwouldremovetheduplicatesandgiveyoutheresultset.
Thisremovingofduplicatewouldcreatedatainconsistencies,ifyoudonthavekeyfieldsinyourselectionfieldspartofyourSELECTquery.Ifyouassumethat
allyourfieldswouldmakeuptheuniquevaluewithoutincludingtheKeyfields,youareinvitingtroubleinthefuture.
AddingtheKeyfieldsofthetablewouldensurethatalltheselectedrecordsareunique.IftheSELECTisajoin,makesureyouincludeallthekeyfieldsfromall
thetablesincludedinthejoin.

Compare
Letmeshowyouthissimpleexample:
T100 SELECT without SPRAS

TYPES:
BEGINOFty_t100,
arbgbTYPEt100arbgb,
msgnrTYPEt100msgnr,
textTYPEt100text,
ENDOFty_t100.

DATA:t_idsTYPESTANDARDTABLEOFt100msgnr.
DATA:t_t100_allTYPESTANDARDTABLEOFt100.
DATA:t_t100TYPESTANDARDTABLEOFty_t100.

APPEND'001'TOt_ids.
APPEND'002'TOt_ids.

IFt_idsISNOTINITIAL.
SELECTarbgb
msgnr
text"commenttoseemorerecordsaredropping
INTOTABLEt_t100
FROMt100
FORALLENTRIESINt_ids
WHEREarbgbLIKE'0%'
ANDmsgnr=t_idsTABLE_LINE.
WRITE:/'WithoutAllKeyFields',sydbcnt.
ENDIF.

Now,thiscodewithallthekeyfields
T100 with SPRAS (with all key fields)

IFt_idsISNOTINITIAL.
SELECT*
INTOTABLEt_t100_all
FROMt100
FORALLENTRIESINt_ids
WHEREarbgbLIKE'0%'
ANDmsgnr=t_idsTABLE_LINE.
WRITE:/'WithALLKeyFields',sydbcnt.
ENDIF.

IfyoucommentouttheTEXTfieldfromthe1stquery,youwouldseemorerecordsarebeingdropped.Thisisduetothefactthat,DBwouldonlyretainthe
uniquerecords.DBwouldcomparetheentirerecordwithallrecordsandwoulddroptherows.JustlikeDELETEADJACENTDUPLICATESFROMitab
COMPARINGALLFIELDS.
BasedonentriesinsystemwhereIexecutedthis:

http://zevolving.com/2014/03/abapforallentrieswhyyouneedtoincludekeyfields/

2/4

2/18/2016

ABAPFORALLENTRIESWhyyouneedtoincludeKEYfields|ABAPHelp

Conclusion
WhenyouuseFORALLENTRIES,alongwithotherthings,youwoulddefinitelywanttomakesureyouhaveallthekeyfieldsintheinternaltableandyouare
selectingthemaswelltoavoidthedatainconsistencies.

Tags
DataIntegrity

Performance

Naimesh Patel

{263 articles}

I'mSAPABAPConsultantformorethanadecade.IliketoexperimentwithABAPespeciallyOO.IhavebeenSDNTopContributor.
Follow:
Exploreallofhis263articles.

3 Comments
fawcs

# March 24th, 2014 at 7:06 am


Notonlythat,theduplicatedentriesaredeletedafterwefetchthedatafromtheDatabase,soyouhavetomakesureyoufetchtheexact
datayouneed!
Iveranintoaproblemwheresomeonedidntdeletetheduplicatedentriesandtheselectbroughtsomuchdatathattheprogramwould
causeashortdump(nomorememory)whenittheselectended

bilen Cekic

# March 27th, 2014 at 10:26 pm


Greatarticlelikeothers.Firstofallappreciateyourworks.
AsaBPCconsultantcurrently,ialsocodemyownABAPworksandtryingtoimplementdesignpatternsonmyworksbygettinghelp
fromyourarticlesthxagain andiamalsoperformancemaniacalsomademanyimprovementsonHANAsystemsandBPCbyusing
someparallelizationlogic.
Fortheforallentries,justwanttoaddsomeinfo,ontheBASISsideithassomeparametersthatcanbechanged,thoseare
rsdb/max_blocking_factor
rsdb/min_blocking_factor
rsdb/max_in_blocking_factor
rsdb/min_in_blocking_factor.
WhatFAEdois,itsplitsyourselectqueriesbasedonparametersandsendselectstatements.Forexamplewehave1000matnrin
internaltableandweareselectingMARA.
Itcreateselectstatementthatcontains5MATNRineachstatement(whichmeanscreates200queries).
Disadvantageis,itkillssystemresources:),SAPsuggeststousejoins.WhileourtradionalRDMSsystemsarechanging,this
approachesallsowillchange.InHANAyougetfasterresponseifyouuseJOINSforexample.
CommentisabitfarfromyourmaintopicbutreleatedwithFAEalso

Naimesh Patel

# March 28th, 2014 at 9:08 am


HelloBilen,
ThanksforthedetailsabouttheparameterswhichcaninfluencehowFAEwouldwork.WecanalsoinfluencetheSELECTusingHINTS
forthespecificSELECTifitistakinglongruntime.
Iguess,IshouldhaveonemorearticleonFAEwiththesedetailsonhowtofurtherimprovetheperformance.
Thanks,
NaimeshPatel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

http://zevolving.com/2014/03/abapforallentrieswhyyouneedtoincludekeyfields/

3/4

2/18/2016

ABAPFORALLENTRIESWhyyouneedtoincludeKEYfields|ABAPHelp

Subscribe
Keep in touch. We would do great working together.
1400
SubscribetoEmail
Follow@zevolving

your email

ABAPHelpBlog
3,063likes

LikePage

Share

Bethefirstofyourfriendstolikethis

ABAP Help
Follow

+1

+ 1,483

Current Poll
DoyoulikeQualityAssuranceofyourBuild/Design?
YES. I live for it!
I don't mind
NO. I hate it

Vote
View Results

Search
SearchinZevolving

zevolving.comisfoundedbyandmaintainedbyNaimeshPatel.YoucanhelpbysubmittingyourarticlesviaWriteaPost.ThesiteisbuiltonWordpress.
HappyDesigning!
About Subscribe Advertise Licence
Allproductnamesaretrademarksoftheirrespectivecompanies.zevolving.comisnotaffiliatedwithSAPAG.

http://zevolving.com/2014/03/abapforallentrieswhyyouneedtoincludekeyfields/

4/4

You might also like