<?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: READ TABLE with OR condition in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625479#M2026892</link>
    <description>&lt;P&gt;Did you consider a SELECT&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A title="Specifies an internal table itab, whose name must be prefixed with the @ character, as a data source of a query." href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;</description>
    <pubDate>Fri, 01 Mar 2024 16:14:10 GMT</pubDate>
    <dc:creator>RaymondGiuseppi</dc:creator>
    <dc:date>2024-03-01T16:14:10Z</dc:date>
    <item>
      <title>READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625452#M2026891</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;&lt;P&gt;I would like to read the most recent entry from an internal table &lt;STRONG&gt;itab&lt;/STRONG&gt; based on a condition.&lt;BR /&gt;Let's assume the table looks like this and is sorted by the field &lt;STRONG&gt;Timestamp&lt;/STRONG&gt;:&lt;/P&gt;&lt;TABLE border="1" width="97.87557055484338%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="50%"&gt;&lt;STRONG&gt;Flag&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="50%"&gt;&lt;STRONG&gt;Timestamp (DD-MM-YYYY HH:MM:SS)&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;A&lt;/TD&gt;&lt;TD width="50%"&gt;01-03-2024 12:00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;01-03-2024 11:00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;A&lt;/TD&gt;&lt;TD width="50%"&gt;29-02-2024 15:27:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;B&lt;/TD&gt;&lt;TD width="50%"&gt;29-02-2024 14:13:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;28-02-2024 10:00:00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;I would like to fetch the most recent entry for which &lt;STRONG&gt;Flag&lt;/STRONG&gt; is either B or C and I know that a possible solution would be to loop through the table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;LOOP AT itab into wa.
    IF wa-flag = 'B' or wa-flag = 'C'.
        EXIT.
    ENDIF.
ENDLOOP.

* Continue processing using wa&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, since I am already perfoming this opration in a nested loop, I would like to avoid adding another loop to the logic.&lt;/P&gt;&lt;P&gt;I also found solutions suggesting to use READ TABLE statements for each of the conditions, but they so not seem to be applicable in the situation described above:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://community.sap.com/t5/application-development-discussions/or-condition-in-key-while-reading-internal-table/m-p/4110595#M982823" target="_blank" rel="noopener"&gt;OR condition in key while reading internal table..&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://community.sap.com/t5/application-development-discussions/read-table-with-or-condition/m-p/4031071" target="_blank" rel="noopener"&gt;Read Table with OR condition&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are there any alternatives to looping through the entire table to fetch the most recent entry for which &lt;STRONG&gt;Flag&lt;/STRONG&gt; is either B or C? Would&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_blank" rel="noopener"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;for example be a valid and recommended approach in this situation?&lt;/P&gt;&lt;P&gt;I'd highly appreciate any thoughts and suggestions in this regard.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:10:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625452#M2026891</guid>
      <dc:creator>carljschmidt</dc:creator>
      <dc:date>2024-03-01T16:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625479#M2026892</link>
      <description>&lt;P&gt;Did you consider a SELECT&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A title="Specifies an internal table itab, whose name must be prefixed with the @ character, as a data source of a query." href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:14:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625479#M2026892</guid>
      <dc:creator>RaymondGiuseppi</dc:creator>
      <dc:date>2024-03-01T16:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625510#M2026894</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/185148"&gt;@RaymondGiuseppi&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;thank you very much for your reply!&lt;BR /&gt;I haven't yet used&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;and after reading&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;SPAN&gt;SELECT is from DATABASE and READ is for INTERNAL table.&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;in the first reply for &lt;A href="https://community.sap.com/t5/application-development-discussions/the-read-table-is-the-same-thing-that-a-select-single/td-p/1238410" target="_self"&gt;The read table is the same thing that a Select Single&lt;/A&gt;&amp;nbsp;I wasn't sure when to use&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;or when READ TABLE shoud be preferred.&lt;/P&gt;&lt;P&gt;But the post is from 2006, so I assume that&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;simply did not exist back then.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 01 Mar 2024 17:03:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625510#M2026894</guid>
      <dc:creator>carljschmidt</dc:creator>
      <dc:date>2024-03-01T17:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625578#M2026898</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;However, since I am already perfoming this opration in a nested loop, I would like to avoid adding another loop to the logic.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This is not a good argument: you are confusing the rule to avoid nested SELECT, with nested loops.&lt;/P&gt;&lt;P&gt;Keep your loop.&lt;/P&gt;&lt;P&gt;In case itab is not a small table, one alternative is to have an index on itab and use one read on A, one on B, instead of OR, for a better performance.&lt;/P&gt;&lt;P&gt;I guess SELECT FROM&amp;nbsp;@itab will have an additional overhead but I never tested/never saw a blog post about FROM @itab&amp;nbsp;performance.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 18:08:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625578#M2026898</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-03-01T18:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625755#M2026903</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/145194"&gt;@Sandra_Rossi&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I appreciate your answer, thank you a lot.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;However, since I am already perfoming this opration in a nested loop, I would like to avoid adding another loop to the logic.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This is not a good argument: you are confusing the rule to avoid nested SELECT, with nested loops.&lt;/P&gt;&lt;P&gt;Keep your loop.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Are you saying that nested SELECT statements should be avoided, but nested loops are fine?&lt;BR /&gt;I assumed that having a cascade of loops is just not a good idea in terms performance due to growing complexity, but please correct me, if I am wrong here.&lt;/P&gt;&lt;P&gt;But I'm happy to use&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm" target="_self" rel="noopener noreferrer"&gt;SELECT - FROM @itab&lt;/A&gt;&amp;nbsp;for the problem described above. Thanks again!&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 00:22:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625755#M2026903</guid>
      <dc:creator>carljschmidt</dc:creator>
      <dc:date>2024-03-02T00:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625919#M2026909</link>
      <description>&lt;P&gt;Yes, I'm saying&amp;nbsp;&lt;SPAN&gt;that nested SELECT statements should be avoided, but nested loops are fine.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Nested loops have never been a problem if you need them. Doing separate SELECT disables inherently any performance improvement by the database, it's why one big SELECT with JOIN is to be preferred (will be optimized by database), and you can also optimize your ABAP code to query the result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Are you happy to use SELECT - FROM @itab because YOU THINK it improves the performance, or you really did a test to compare performance?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;e.g. example of comparison I did -&amp;gt; &lt;A href="https://community.sap.com/t5/application-development-discussions/count-the-records-in-an-internal-table-without-a-loop/m-p/6466787/highlight/true#M1416540" target="_blank"&gt;Solved: Re: Count the Records in an internal table withou... - SAP Community&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 11:17:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13625919#M2026909</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-03-02T11:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626012#M2026910</link>
      <description>&lt;P&gt;Nested LOOPs are only acceptable from a performance point of view, if the inner loop's are using a sorted/indexed table or if the tables are very small (&lt;EM&gt;less than 20-50 records&lt;/EM&gt;), otherwise you will have a non-linear runtime behavior. The same non-linear runtime behavior &lt;SPAN class=""&gt;O&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;n&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;2&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&amp;nbsp;&lt;/SPAN&gt;will happen if you place READ TABLE ... WITH KEY (without binary search), MODIFY WHERE ..., DELETE WHERE ... into a loop when the inner table is not sorted or indexed. The runtime can be improved if the inner table can be sorted before entering the outer loop. Sorting a standard table within a LOOP will also result in an&amp;nbsp;&lt;SPAN class=""&gt;O&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;n&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;2&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;) non-linear runtime.&lt;BR /&gt;&lt;BR /&gt;I highly recommend the book &lt;A href="https://www.sap-press.com/abap-performance-tuning_2092/" target="_self"&gt;&lt;EM&gt;&lt;STRONG&gt;ABAP Performance Tuning&lt;/STRONG&gt;&lt;/EM&gt;&lt;/A&gt; by Hermann Gahm (&lt;EM&gt;SAP Press,&amp;nbsp;ISBN&amp;nbsp;&lt;/EM&gt;&lt;SPAN&gt;&lt;EM&gt;978-1-59229-555-5&lt;/EM&gt;) - see chapter 7 Processing of internal tables.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 18:11:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626012#M2026910</guid>
      <dc:creator>christoph_weyd</dc:creator>
      <dc:date>2024-03-02T18:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626013#M2026911</link>
      <description>&lt;P&gt;If the WHERE condition of the inner loop is hard coded (B or C) but sorted descending by timestamp, then there should be a way to build this table without the Flag = A in the first place. Once you achieve that you only need to read the first entry.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 18:01:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626013#M2026911</guid>
      <dc:creator>christoph_weyd</dc:creator>
      <dc:date>2024-03-02T18:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626737#M2026919</link>
      <description>&lt;P&gt;You could also perform two READ TABLEs, one for each value, and compare the timestamp to select the best record?&lt;/P&gt;&lt;P&gt;A nested LOOP could also be suitable, if you have an index (primary or &lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/abenitab_key_memory.htm" target="_self"&gt;secondary&lt;/A&gt;) to optimize it.&lt;/P&gt;&lt;P&gt;You need to run optimization tests if the size of your internal table is large.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 09:47:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626737#M2026919</guid>
      <dc:creator>RaymondGiuseppi</dc:creator>
      <dc:date>2024-03-04T09:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: READ TABLE with OR condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626754#M2026920</link>
      <description>&lt;P&gt;Nested loops are only a problem if, as the tables increase in size linearly, the time taken increases rather more rapidly. I.e. if with twice as many records it takes more than twice as long.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This can be avoid often by defining the tables as HASHED or SORTED with a good key.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 10:01:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/read-table-with-or-condition/m-p/13626754#M2026920</guid>
      <dc:creator>matt</dc:creator>
      <dc:date>2024-03-04T10:01:40Z</dc:date>
    </item>
  </channel>
</rss>

