<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: LOOP....ENDLOOP and SELECT Statement Issue in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355456#M1640545</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;gt; &lt;/P&gt;&lt;P&gt;1) Use SORTED tables with unique keys (Removes any SORT command).&lt;/P&gt;&lt;P&gt;2) DO NOT use statement DELETE inside your LOOP. Instead use a DELETE WHERE outside...&lt;/P&gt;&lt;P&gt;&amp;gt; &lt;/P&gt;&lt;P&gt;&amp;gt; Kr,&lt;/P&gt;&lt;P&gt;&amp;gt; Manu.&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;1) Yes, sorted table would be an option, but since when using sorted tables and moreover UNIQUE keys became a STANDARD? And I would like to see a proof link.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Really? Why? And please show me the coding sample to delete all entries from ITAB1 where the corresponding entry does not exist in ITAB2 using DELETE WHERE outside of the loop.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 10 Nov 2011 09:42:37 GMT</pubDate>
    <dc:creator>yuri_ziryukin</dc:creator>
    <dc:date>2011-11-10T09:42:37Z</dc:date>
    <item>
      <title>LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355438#M1640527</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Following is the query.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
*BSIK
SELECT A~LIFNR A~BELNR A~XBLNR A~BLART A~BUDAT A~GJAHR A~SHKZG A~DMBTR 
A~WRBTR A~DMBT1 A~DMBT2 B~NAME1 INTO CORRESPONDING FIELDS OF TABLE ITAB 
FROM BSIK AS A INNER JOIN LFA1 AS B ON A~LIFNR EQ B~LIFNR WHERE BUKRS EQ 
P_BUKRS AND BUDAT IN S_BUDAT AND A~LIFNR IN S_LIFNR AND A~BELNR IN S_BELNR 
AND A~BLART IN S_BLART.
*BSAK
SELECT A~LIFNR A~BELNR A~XBLNR A~BLART A~BUDAT A~GJAHR A~SHKZG A~DMBTR 
A~WRBTR A~DMBT1 A~DMBT2 B~NAME1 APPENDING CORRESPONDING FIELDS OF TABLE ITAB 
FROM BSAK AS A INNER JOIN LFA1 AS B ON A~LIFNR EQ B~LIFNR WHERE BUKRS EQ 
P_BUKRS AND BUDAT IN S_BUDAT AND A~LIFNR IN S_LIFNR AND A~BELNR IN S_BELNR 
AND A~BLART IN S_BLART.
"Here my ITAB has 465322 records.
LOOP AT ITAB.
SELECT SINGLE AWKEY INTO G_AWKEY FROM BKPF CLIENT SPECIFIED WHERE MANDT EQ SY-MANDT 
AND BUKRS EQ P_BUKRS AND BELNR EQ ITAB-BELNR AND GJAHR EQ ITAB-GJAHR.
IF SY_SUBRC = 0.
SELECT SINGLE EBELN INTO G_EBELN FROM RSEG CLIENT SPECIFIED WHERE MANDT EQ SY-MANDT 
AND BELNR EQ G_AWKEY(10) AND GJAHR EQ ITAB-GJAHR AND EBELN IN S_EBELN.
        IF NOT G_EBELN IS INITIAL.
        ITAB-EBELN = G_EBELN.
        MODIFY ITAB.
        ELSE.
        DELETE ITAB INDEX SY-TABIX.
        ENDIF.
ELSE.
DELETE ITAB INDEX SY_TABIX.
ENDIF.
ENDLOOP.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here my LOOP......ENDLOOP is taking too much time (15 minutes). Can anybody suggest me what to do in this case. I have also tried FOR ALL ENTRIES. It is also taking lot of time. This is my first time I am working with FI data and Tables.I have also searched GOOGLE. But no use.Please suggest is there any other way to achieve this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 19:04:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355438#M1640527</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T19:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355439#M1640528</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Nani&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You  should do your data selection one time and out of Loop  EndLopp&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try with this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;IF  NOT ITAB[]  IS INITIAL

SELECT *  
INTO  TABLE  WT_BKPF
FROM BKPF CLIENT SPECIFIED 
FOR ALL ENTRIES IN ITAB
WHERE MANDT EQ SY-MANDT 
    AND BUKRS EQ P_BUKRS 
    AND BELNR EQ ITAB-BELNR 
   AND GJAHR EQ ITAB-GJAHR.

IF SY-SUBRC EQ 0.

SORT  WT_BKPF BY BELNR GJAHR . 

SELECT  * 
   INTO TABLE  WT_RSEG
FROM RSEG CLIENT SPECIFIED 
FOR ALL ENTRIES IN  ITAB 
   WHERE MANDT EQ SY-MANDT 
    AND GJAHR EQ ITAB-GJAHR 
    AND EBELN IN S_EBELN.

IF SY-SUBRC EQ 0.

   SORT  WT_RSEG  BY BELNR GJAHR  EBELN . 

ENDIF.


ENDIF.

LOOP AT ITAB

   READ TABLE  WT_BKPF  
   WITH KEY   BELNR =   ITAB-BELNR
                   GJAHR  =   ITAB-GJAHR
    BINARY SERACH.

  IF SY-SUBRC EQ 0.

    READ TABLE  WT_RSEG
     WITH KEY   BELNR =   ITAB-BELNR
                       GJAHR  =   ITAB-GJAHR
                      BELNR = WT_BKPF -G_AWKEY(10)
        BINARY SERACH.

  IF SY-SUBRC EQ 0.

        IF NOT    WT_RSEG-EBELN IS INITIAL.
        
            ITAB-EBELN =   WT_RSEG-EBELN.
            MODIFY ITAB.
    
         ENDIF.


  ELSE.

        DELETE ITAB INDEX SY-TABIX.
  ENDIF.


ELSE.


DELETE ITAB INDEX SY_TABIX.

ENDIF.
.


ENDLOOP

ENDIF.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;Hamza KOUACHI&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 19:32:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355439#M1640528</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T19:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355440#M1640529</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Hamza,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply. Can you also tell me how to declare WT_BKPF and WT_RSEG?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 19:41:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355440#M1640529</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T19:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355441#M1640530</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Nani,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;there are more way to declare it &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can  use this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: WT_BKPF  TYPE  bkpf WITH HEADER LINE.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: WT_RSEG  TYPE  rseg  WITH HEADER LINE.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;you can also declare  TYPES with only fields  wich you need&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;Hamza KOUACHI&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:00:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355441#M1640530</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355442#M1640531</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Also, make a field symbol of your internal table and work area type that will considerably improve the performance. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please go thru the Document: &lt;A href="http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb387a358411d1829f0000e829fbfe/content.htm" target="test_blank"&gt;http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb387a358411d1829f0000e829fbfe/content.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reetesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:04:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355442#M1640531</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355443#M1640532</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Hamza,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have declared the internal tables as you said. But my select * from RSEG is taking too much time. I can say it is forever thru that select * statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it's a performance issue. Any suggestions?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:05:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355443#M1640532</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355444#M1640533</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Reetesh,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply. Can you please give me an example/example link instead of sap help link.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:08:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355444#M1640533</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355445#M1640534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As a rule NEVER do a SELECT *, unless you really want all the records which is very unlikely. And also use key fields in where conditions and remove MANDT field from your query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As discussed earlier you &amp;lt;FS&amp;gt; instead of Internal Tables and Work area to get better result because this uses reference (memory address) hence to and fro is quicker.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reetesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:13:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355445#M1640534</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355446#M1640535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Use delete adjacent duplicates  command like mentioned below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
itab2 = itab.

delete ADJACENT DUPLICATES FROM itab2 COMPARING gjahr.
SELECT  * 
   INTO TABLE  WT_RSEG
   FROM RSEG CLIENT SPECIFIED 
   FOR ALL ENTRIES IN  ITAB2 
          WHERE MANDT EQ SY-MANDT 
          AND GJAHR EQ ITAB2-GJAHR 
          AND EBELN IN S_EBELN.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vijay&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:22:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355446#M1640535</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355447#M1640536</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here you go: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://help-abap.zevolving.com/2009/12/use-of-field-symbols-vs-work-area/" target="test_blank"&gt;http://help-abap.zevolving.com/2009/12/use-of-field-symbols-vs-work-area/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://sample-code-abap.blogspot.com/2008/07/sap-abap-dynamic-internal-table-and.html" target="test_blank"&gt;http://sample-code-abap.blogspot.com/2008/07/sap-abap-dynamic-internal-table-and.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reetesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:25:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355447#M1640536</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355448#M1640537</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nani - have you run transaction ST05 to see where the problem is?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:34:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355448#M1640537</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355449#M1640538</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As a rule NEVER do a SELECT *, unless you really want all the records which is very unlikely. And also use key fields in where conditions and remove MANDT field from your query.&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;SELECT * does not return all the records. It simply returns all of the fields of a single record. It is unlikely that this would be a big problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 20:35:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355449#M1640538</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T20:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355450#M1640539</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rob,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have run SE30 and not able to get thru the statement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT  belnr gjahr ebeln  INTO TABLE  WT_RSEG
FROM RSEG FOR ALL ENTRIES IN  ITAB 
   WHERE GJAHR EQ ITAB-GJAHR 
       AND EBELN IN S_EBELN.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I think my WHERE condition is not satisfied because I have not &lt;/P&gt;&lt;P&gt;specified BELNR in my WHERE condition. I tried specifying &lt;/P&gt;&lt;P&gt;BELNR in WHERE condition as below.  &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT  belnr gjahr ebeln  INTO TABLE  WT_RSEG
FROM RSEG FOR ALL ENTRIES IN ITAB 
   WHERE belnr eq itab-awkey     "I have also tried belnr eq itab-awkey+0(10)
       AND GJAHR EQ ITAB-GJAHR 
       AND EBELN IN S_EBELN.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it is giving me an error saying that "When you use the addition "FOR ALL ENTRIES IN itab" the fields "BELNR" and "ITAB_AWKEY" must have the same type and same length.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do I need to declare my BELNR/AWKEY. Please suggest.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 22:20:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355450#M1640539</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T22:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355451#M1640540</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1753130"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or check the LDB ( Logical data base which suits for your requirement)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Nov 2011 22:42:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355451#M1640540</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-08T22:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355452#M1640541</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is how I have modified my code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
*BSIK
SELECT A~LIFNR A~BELNR A~XBLNR A~BLART A~BUDAT A~GJAHR A~SHKZG A~DMBTR 
A~WRBTR A~DMBT1 A~DMBT2 B~NAME1 INTO CORRESPONDING FIELDS OF TABLE ITAB 
FROM BSIK AS A INNER JOIN LFA1 AS B ON A~LIFNR EQ B~LIFNR WHERE BUKRS EQ 
P_BUKRS AND BUDAT IN S_BUDAT AND A~LIFNR IN S_LIFNR AND A~BELNR IN S_BELNR 
AND A~BLART IN S_BLART.
*BSAK
SELECT A~LIFNR A~BELNR A~XBLNR A~BLART A~BUDAT A~GJAHR A~SHKZG A~DMBTR 
A~WRBTR A~DMBT1 A~DMBT2 B~NAME1 APPENDING CORRESPONDING FIELDS OF TABLE ITAB 
FROM BSAK AS A INNER JOIN LFA1 AS B ON A~LIFNR EQ B~LIFNR WHERE BUKRS EQ 
P_BUKRS AND BUDAT IN S_BUDAT AND A~LIFNR IN S_LIFNR AND A~BELNR IN S_BELNR 
AND A~BLART IN S_BLART.

IF NOT ITAB[] IS INITIAL.
    SELECT BELNR GJAHR AWKEY INTO TABLE IT_BKPF
                             FROM BKPF FOR ALL ENTRIES IN ITAB
                             WHERE BUKRS EQ P_BUKRS
                               AND BELNR EQ ITAB-BELNR
                               AND GJAHR EQ ITAB-GJAHR.
    IF SY-SUBRC EQ 0.
      SORT IT_BKPF BY BELNR GJAHR.
 "This select statement is taking forever. (performance issue)
      SELECT BELNR GJAHR EBELN INTO TABLE IT_RSEG   
                               FROM RSEG FOR ALL ENTRIES IN ITAB
                               WHERE GJAHR EQ ITAB-GJAHR
                                 AND EBELN IN S_EBELN.
      IF SY-SUBRC EQ 0.
        SORT IT_RSEG BY BELNR GJAHR EBELN.
      ENDIF.
    ENDIF.
    LOOP AT ITAB.
      READ TABLE IT_BKPF INTO WA_BKPF WITH KEY BELNR =   ITAB-BELNR
                GJAHR =   ITAB-GJAHR BINARY SEARCH.
      IF SY-SUBRC EQ 0.
        READ TABLE IT_RSEG INTO WA_RSEG WITH KEY BELNR =   ITAB-BELNR
                   GJAHR =   ITAB-GJAHR BINARY SEARCH.
* BELNR = WA_BKPF-AWKEY(10) BINARY SEARCH.
                      "I am getting error-Cant pass BELNR two times
        IF SY-SUBRC EQ 0.
          IF NOT WA_RSEG-EBELN IS INITIAL.
            ITAB-EBELN =   WA_RSEG-EBELN.
            MODIFY ITAB.
          ENDIF.
        ELSE.
          DELETE ITAB INDEX SY-TABIX.
        ENDIF.
      ELSE.
        DELETE ITAB INDEX SY-TABIX.
      ENDIF.
    ENDLOOP.
  ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any suggestions. Please help. Examples are really appreciated.Also let me know if there is any other way to achieve this. I have not worked with Field symbols earlier. If using FIELD SYMBOLS solves the issues please let me know how do I need to use them in my case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Rob Burbank on Nov 10, 2011 10:17 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Nov 2011 00:58:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355452#M1640541</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-09T00:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355453#M1640542</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;oops, well that is what I meant to say, may be it was not clear, enough! Though what is clear is when you look to optimize your query you should not use '*' as the best thing you can do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reetesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Nov 2011 04:21:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355453#M1640542</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-09T04:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355454#M1640543</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, Nani, you have made many mistakes in your report.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) When selecting data from BSIK and BSAK you get over 400.000 entries. If you look at them, they are "item-based". So for each accounting document you'll have many items in your ITAB.&lt;/P&gt;&lt;P&gt;Your next SELECT statement from BKPF is using only document header.&lt;/P&gt;&lt;P&gt;So, this is your first mistake.&lt;/P&gt;&lt;P&gt;So if you really need to prove the existence of the corresponding entries in BKPF, you should copy ITAB to another table with the same structure, sort it by BELNR, GJAHR and delete adjacent duplicates before making a select from BKPF:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;ITAB2[] = ITAB[],
sort ITAB2 by BELNR GJAHR.
delete adjacent duplicates from ITAB2 comparing BELNR GJAHR.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Why do you need a check against BKPF? If I understand the table structire correctly, BSIK and BSAK are "index" tables. From my understanding there will ALWAYS be an entry in BKPF for the corresponding enries in BSAK and BSIK. Otherwise it's a data inconsistency issue which needs to be resolved.&lt;/P&gt;&lt;P&gt;If my understanding is correct, the select from BKPF and subsequent READ TABLE inside the loop should be removed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3) Your select from RSEG is completely useless in a way you have it now.&lt;/P&gt;&lt;P&gt;I guess you need to change it like this (keep in mind that you need to implement point 1 before this select):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;      IF ITAB2[] IS NOT INITIAL.&lt;/P&gt;&lt;P&gt;      SELECT BELNR GJAHR EBELN INTO TABLE IT_RSEG   &lt;/P&gt;&lt;P&gt;                               FROM RSEG FOR ALL ENTRIES IN &lt;STRONG&gt;ITAB2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;                                WHERE BELNR EQ &lt;STRONG&gt;ITAB2-BELNR&lt;/STRONG&gt; AND&lt;/P&gt;&lt;P&gt;                                 GJAHR EQ &lt;STRONG&gt;ITAB2-GJAHR&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;                                 AND EBELN IN S_EBELN.&lt;/P&gt;&lt;P&gt;     ENDIF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;4) Use field symbols for ITAB and IT_RSEG.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Adjust your coding:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;field-symbols: &amp;lt;fs_itab&amp;gt; like line of itab,
             &amp;lt;fs_rseg&amp;gt; like line of it_rseg.

LOOP AT ITAB assigning &amp;lt;fs_itab&amp;gt;.
* Think about removing the below check completely! See my point 2.
      *READ TABLE IT_BKPF INTO WA_BKPF WITH KEY BELNR =   ITAB-BELNR*
                *GJAHR =   ITAB-GJAHR BINARY SEARCH.*
      IF SY-SUBRC EQ 0.
        READ TABLE IT_RSEG ASSIGNING &amp;lt;FS_RSEG&amp;gt; WITH KEY BELNR = &amp;lt;FS_ITAB&amp;gt;-BELNR
                   GJAHR =  &amp;lt;FS_ITAB&amp;gt;-GJAHR BINARY SEARCH.
        IF SY-SUBRC EQ 0.
          IF NOT &amp;lt;FS_RSEG&amp;gt;-EBELN IS INITIAL.
            &amp;lt;FS_ITAB&amp;gt;-EBELN = &amp;lt;FS_RSEG&amp;gt;-EBELN.
*            MODIFY ITAB. "not needed anymore
          ENDIF.
        ELSE.
          DELETE ITAB. "index specification not required in loop
        ENDIF.
      ELSE.
        DELETE ITAB. "index specification not required in loop
      ENDIF.
    ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;  Yuri&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Nov 2011 08:38:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355454#M1640543</guid>
      <dc:creator>yuri_ziryukin</dc:creator>
      <dc:date>2011-11-09T08:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355455#M1640544</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Quite strange that nobody pointed out some of the performance standards: Use SORTED tables with unique keys (Removes any SORT command). DO NOT use statement DELETE inside your LOOP. Instead use a DELETE WHERE outside...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kr,&lt;/P&gt;&lt;P&gt;Manu.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2011 09:25:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355455#M1640544</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-10T09:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355456#M1640545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;gt; &lt;/P&gt;&lt;P&gt;1) Use SORTED tables with unique keys (Removes any SORT command).&lt;/P&gt;&lt;P&gt;2) DO NOT use statement DELETE inside your LOOP. Instead use a DELETE WHERE outside...&lt;/P&gt;&lt;P&gt;&amp;gt; &lt;/P&gt;&lt;P&gt;&amp;gt; Kr,&lt;/P&gt;&lt;P&gt;&amp;gt; Manu.&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;1) Yes, sorted table would be an option, but since when using sorted tables and moreover UNIQUE keys became a STANDARD? And I would like to see a proof link.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Really? Why? And please show me the coding sample to delete all entries from ITAB1 where the corresponding entry does not exist in ITAB2 using DELETE WHERE outside of the loop.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2011 09:42:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355456#M1640545</guid>
      <dc:creator>yuri_ziryukin</dc:creator>
      <dc:date>2011-11-10T09:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: LOOP....ENDLOOP and SELECT Statement Issue</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355457#M1640546</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;1) Sorted table would prevent the use of an inefficient SORT command on such big table, which is quite enough to avoid using standard one&lt;/P&gt;&lt;P&gt;2) Well, I think this is quite obvious that 1 DELETE is more efficient that many nested DELETE... &lt;SPAN __jive_emoticon_name="wink"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;This could be done easily by maintaining a flag on itab instead of statement DELETE in your example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;here you are:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
LOOP AT ITAB assigning &amp;lt;fs_itab&amp;gt;.
* Think about removing the below check completely! See my point 2.
      *READ TABLE IT_BKPF INTO WA_BKPF WITH KEY BELNR =   ITAB-BELNR*
                *GJAHR =   ITAB-GJAHR BINARY SEARCH.*
      IF SY-SUBRC EQ 0.
        READ TABLE IT_RSEG ASSIGNING &amp;lt;FS_RSEG&amp;gt; WITH KEY BELNR = &amp;lt;FS_ITAB&amp;gt;-BELNR
                   GJAHR =  &amp;lt;FS_ITAB&amp;gt;-GJAHR BINARY SEARCH.
        IF SY-SUBRC EQ 0.
          IF NOT &amp;lt;FS_RSEG&amp;gt;-EBELN IS INITIAL.
            &amp;lt;FS_ITAB&amp;gt;-EBELN = &amp;lt;FS_RSEG&amp;gt;-EBELN.
*            MODIFY ITAB. "not needed anymore
          ENDIF.
        ELSE.
          "DELETE ITAB. "index specification not required in loop
          &amp;lt;fs_itab&amp;gt;-del = 'X'.
        ENDIF.
      ELSE.
        "DELETE ITAB. "index specification not required in loop
         &amp;lt;fs_itab&amp;gt;-del = 'X'.
      ENDIF.
ENDLOOP.
DELETE itab WHERE del = 'X'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kr,&lt;/P&gt;&lt;P&gt;Manu.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Manu D'Haeyer on Nov 10, 2011 10:58 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2011 09:58:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-endloop-and-select-statement-issue/m-p/8355457#M1640546</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-11-10T09:58:33Z</dc:date>
    </item>
  </channel>
</rss>

