<?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 The Select Query in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774098#M646491</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;  SELECT matnr FROM mara INTO TABLE gt_mara&lt;/P&gt;&lt;P&gt;                               WHERE matnr IN s_matnr&lt;/P&gt;&lt;P&gt;                               AND   mtart = p_mtart.&lt;/P&gt;&lt;P&gt;  IF NOT gt_mara[] IS INITIAL.&lt;/P&gt;&lt;P&gt;    SORT gt_mara BY matnr.&lt;/P&gt;&lt;P&gt;    SELECT aufnr objnr aufpl FROM caufv INTO TABLE gt_caufv&lt;/P&gt;&lt;P&gt;                             FOR ALL ENTRIES IN gt_mara&lt;/P&gt;&lt;P&gt;                             WHERE aufnr IN s_aufnr&lt;/P&gt;&lt;P&gt;                             AND auart = p_auart&lt;/P&gt;&lt;P&gt;                             AND werks = p_werks&lt;/P&gt;&lt;P&gt;                             AND plnbez = gt_mara-matnr.&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have replaced the above for all entries query by the below inner join. Found in the debugg both have same number of records in the (DEV server). Performance wise also this one is better. Let me know both are exactly same or not.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  SELECT a~matnr&lt;/P&gt;&lt;P&gt;         b~aufnr&lt;/P&gt;&lt;P&gt;         b~objnr&lt;/P&gt;&lt;P&gt;         b~aufpl&lt;/P&gt;&lt;P&gt;         INTO TABLE gt_caufv FROM mara AS a INNER JOIN caufv AS b&lt;/P&gt;&lt;P&gt;         ON a&lt;SUB&gt;matnr = b&lt;/SUB&gt;plnbez WHERE matnr IN s_matnr&lt;/P&gt;&lt;P&gt;                                     AND mtart =&amp;lt;b&amp;gt; p_mtart&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;                                     AND aufnr IN s_aufnr&lt;/P&gt;&lt;P&gt;                                     AND auart = p_auart&lt;/P&gt;&lt;P&gt;                                     AND werks = p_werks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My concern with the join is &amp;lt;b&amp;gt;a&lt;SUB&gt;matnr = b&lt;/SUB&gt;plnbez&amp;lt;/b&amp;gt;  is whether this will give me unique records or duplicate records. It seems to be good and unique in the DEV but not sure about the PROD server .  Kindly Clarify.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 14 Sep 2007 15:27:05 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2007-09-14T15:27:05Z</dc:date>
    <item>
      <title>The Select Query</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774098#M646491</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;  SELECT matnr FROM mara INTO TABLE gt_mara&lt;/P&gt;&lt;P&gt;                               WHERE matnr IN s_matnr&lt;/P&gt;&lt;P&gt;                               AND   mtart = p_mtart.&lt;/P&gt;&lt;P&gt;  IF NOT gt_mara[] IS INITIAL.&lt;/P&gt;&lt;P&gt;    SORT gt_mara BY matnr.&lt;/P&gt;&lt;P&gt;    SELECT aufnr objnr aufpl FROM caufv INTO TABLE gt_caufv&lt;/P&gt;&lt;P&gt;                             FOR ALL ENTRIES IN gt_mara&lt;/P&gt;&lt;P&gt;                             WHERE aufnr IN s_aufnr&lt;/P&gt;&lt;P&gt;                             AND auart = p_auart&lt;/P&gt;&lt;P&gt;                             AND werks = p_werks&lt;/P&gt;&lt;P&gt;                             AND plnbez = gt_mara-matnr.&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have replaced the above for all entries query by the below inner join. Found in the debugg both have same number of records in the (DEV server). Performance wise also this one is better. Let me know both are exactly same or not.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  SELECT a~matnr&lt;/P&gt;&lt;P&gt;         b~aufnr&lt;/P&gt;&lt;P&gt;         b~objnr&lt;/P&gt;&lt;P&gt;         b~aufpl&lt;/P&gt;&lt;P&gt;         INTO TABLE gt_caufv FROM mara AS a INNER JOIN caufv AS b&lt;/P&gt;&lt;P&gt;         ON a&lt;SUB&gt;matnr = b&lt;/SUB&gt;plnbez WHERE matnr IN s_matnr&lt;/P&gt;&lt;P&gt;                                     AND mtart =&amp;lt;b&amp;gt; p_mtart&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;                                     AND aufnr IN s_aufnr&lt;/P&gt;&lt;P&gt;                                     AND auart = p_auart&lt;/P&gt;&lt;P&gt;                                     AND werks = p_werks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My concern with the join is &amp;lt;b&amp;gt;a&lt;SUB&gt;matnr = b&lt;/SUB&gt;plnbez&amp;lt;/b&amp;gt;  is whether this will give me unique records or duplicate records. It seems to be good and unique in the DEV but not sure about the PROD server .  Kindly Clarify.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Sep 2007 15:27:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774098#M646491</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-09-14T15:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: The Select Query</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774099#M646492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;    You can carry on with the second query as it will create no problem and performance wise also it is more efficient.&lt;/P&gt;&lt;P&gt;Since u r selecting from mara based on only matnr and all the other conditions in the second itab is based on input parameters ,always the record will be unique.&lt;/P&gt;&lt;P&gt;Also remember to change the query like this.&lt;/P&gt;&lt;P&gt;SELECT a~matnr&lt;/P&gt;&lt;P&gt;b~aufnr&lt;/P&gt;&lt;P&gt;b~objnr&lt;/P&gt;&lt;P&gt;b~aufpl&lt;/P&gt;&lt;P&gt;INTO TABLE gt_caufv FROM mara AS a INNER JOIN caufv AS b&lt;/P&gt;&lt;P&gt;ON a&lt;SUB&gt;matnr = b&lt;/SUB&gt;plnbez WHERE &amp;lt;b&amp;gt;a~&amp;lt;/b&amp;gt;matnr IN s_matnr&lt;/P&gt;&lt;P&gt;AND &amp;lt;b&amp;gt;a~&amp;lt;/b&amp;gt;mtart = p_mtart&lt;/P&gt;&lt;P&gt;AND &amp;lt;b&amp;gt;b~&amp;lt;/b&amp;gt;aufnr IN s_aufnr&lt;/P&gt;&lt;P&gt;AND &amp;lt;b&amp;gt;b~&amp;lt;/b&amp;gt;auart = p_auart&lt;/P&gt;&lt;P&gt;AND &amp;lt;b&amp;gt;b~&amp;lt;/b&amp;gt;werks = p_werks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Sep 2007 15:46:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774099#M646492</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-09-14T15:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: The Select Query</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774100#M646493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The join by itself will allow duplicate records if they exist, since the same material could exist in more than one order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the FOR ALL ENTRIES addition de-dups the result set; this is a feature of FOR ALL ENTRIES.   This forces the original to be distinct apart from any other consideration.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to guarantee the records are unique, why not just add DISTINCT?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;      SELECT DISTINCT
             a~matnr
             b~aufnr
             b~objnr
             b~aufpl
             INTO TABLE gt_caufv
             FROM mara AS a
             INNER JOIN caufv AS b
             ON a~matnr = b~plnbez
             WHERE matnr IN s_matnr
               AND mtart = p_mtart
               AND aufnr IN s_aufnr
               AND auart = p_auart
               AND werks = p_werks.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively, you could SORT the resulting itab and DELETE ADJACENT DUPLICATES.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the case of DISTINCT, the sorting and deleting of duplicates occurs on the database server; in the case of explicit SORT and DELETE ADJACENT DUPLICATES, it occurs on the application server.   This may be a consideration for performance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lastly, the join and FOR ALL ENTRIES are different in one other way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the itab gt_mara in FOR ALL ENTRIES is empty, the WHERE clause is ignored and the whole table is loaded.   This is a feature of FOR ALL ENTRIES, which you can confirm in SAP HELP.   If this is not the behavior that you expect, you need to check for it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Brian&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited for spelling, wording&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: &lt;/P&gt;&lt;P&gt;        Brian Sammond&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Sep 2007 15:50:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774100#M646493</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-09-14T15:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: The Select Query</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774101#M646494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sounds Good.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;S&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Sep 2007 15:57:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774101#M646494</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-09-14T15:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: The Select Query</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774102#M646495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Anju's answer is incorrect, since it does not take into account the possibility of duplicates in the join due to the material appearing on different orders in CAUFV, which will NOT occur with FOR ALL ENTRIES.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;They are NOT always the same.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Brian&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;null&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Sep 2007 16:09:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/the-select-query/m-p/2774102#M646495</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-09-14T16:09:08Z</dc:date>
    </item>
  </channel>
</rss>

