<?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: COUNT(*) - FOR ALL ENTRIES IN using Native SQL in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103843#M1508843</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jelena&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Table XYZ (MAST) is transparent.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried the COUNT( tnlpr ) in the Open SQL but this produced a syntax error.  In the Native SQL (MaxDB) the count(*) works like count(tlnpr) in that is does an index only access path.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I looked at the explain plan for joining the 2 tables (INNER JOIN) and it doesn't look very efficient (insert grumble about MaxDb and joins).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The old code was (psuedo code):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
select * into lt_stpo from stpo where idnrk = p_matnr.
if sy-subrc EQ 0.
    loop at lt_stpo
        select * from mast where stlnr = lt_stpo-stlnr and werks = p_werks.
        if sy-subrc EQ 0
            set true flag
            exit
        else
            set false flag
        endif
    endloop
else
    set false flag
endif.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;new code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
select stlnr into lt_stpo from stpo where idnrk = p_matnr.
if sy-subrc EQ 0
    select count(*) into (counter) from mast
        for all entries in lt_stpo
        where stlnr = lt_stpo-stlnr
        and werks = p_werks
    if counter &amp;gt; 0.
        set true flag
    else
        set false flag
    endif
else
    set false flag
endif
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;faster code:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;please insert here&lt;/EM&gt;&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;Doug&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 30 Jun 2010 23:14:37 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2010-06-30T23:14:37Z</dc:date>
    <item>
      <title>COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103841#M1508841</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;I'm trying to tune an ABAP by using a count(*).  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I do a trace (explain plan) on the Query I see that the DBI retrieves the complete row from the database and then counts the result.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I would like to do is have the rdbms count the records and return the result to the ABAP as it doesn't access the table, only the index, which is must faster, especially when it's being done several thousand times.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem I have is that the DBI (Open SQL) uses the FOR ALL ENTRIES IN  and I'm not sure how I can easily convert this query to the Native SQL (EXEC SQL).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The query is something like this:&lt;/P&gt;&lt;P&gt;     SELECT COUNT(*) INTO (counter) FROM xyz &lt;/P&gt;&lt;P&gt;              FOR ALL ENTRIES  IN itab &lt;/P&gt;&lt;P&gt;              WHERE field = itab-field &lt;/P&gt;&lt;P&gt;              AND field2 = p_field2 etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Doug&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Jun 2010 05:49:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103841#M1508841</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-06-29T05:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103842#M1508842</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is XYZ a transparent table? Have you tried COUNT (column) instead of COUNT(*)? Also using FOR ALL ENTRIES is not always justified. See if it can be replaced by a JOIN. Subquery is sometimes an option, although not necessarily a better one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also you might get better replies in the Performance forum. Ask moderators to move the post.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jun 2010 20:32:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103842#M1508842</guid>
      <dc:creator>Jelena_Perfiljeva</dc:creator>
      <dc:date>2010-06-30T20:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103843#M1508843</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jelena&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Table XYZ (MAST) is transparent.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried the COUNT( tnlpr ) in the Open SQL but this produced a syntax error.  In the Native SQL (MaxDB) the count(*) works like count(tlnpr) in that is does an index only access path.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I looked at the explain plan for joining the 2 tables (INNER JOIN) and it doesn't look very efficient (insert grumble about MaxDb and joins).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The old code was (psuedo code):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
select * into lt_stpo from stpo where idnrk = p_matnr.
if sy-subrc EQ 0.
    loop at lt_stpo
        select * from mast where stlnr = lt_stpo-stlnr and werks = p_werks.
        if sy-subrc EQ 0
            set true flag
            exit
        else
            set false flag
        endif
    endloop
else
    set false flag
endif.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;new code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
select stlnr into lt_stpo from stpo where idnrk = p_matnr.
if sy-subrc EQ 0
    select count(*) into (counter) from mast
        for all entries in lt_stpo
        where stlnr = lt_stpo-stlnr
        and werks = p_werks
    if counter &amp;gt; 0.
        set true flag
    else
        set false flag
    endif
else
    set false flag
endif
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;faster code:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;please insert here&lt;/EM&gt;&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;Doug&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jun 2010 23:14:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103843#M1508843</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-06-30T23:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103844#M1508844</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Doug,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why you are wasting time to write this code ? You only want to check entry exit or not (i.e. True or false)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use join statement which will reduce the data transfer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try the following way&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
  select single * from stpo INNER JOIN mast on  mast~stlnr = stpo~stlnr
    where werks = p_werks
        and idnrk = p_matnr. 
  if sy-subrc eq 0.
     set true flag.
  else.
     set false flag.
  endif.    

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rgds&lt;/P&gt;&lt;P&gt;Ravi Lanjewar&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Ravishankar Lanjewar on Jul 1, 2010 10:00 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Jul 2010 04:29:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103844#M1508844</guid>
      <dc:creator>ravi_lanjewar</dc:creator>
      <dc:date>2010-07-01T04:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103845#M1508845</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ravi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Short and sweet, much more elegant than my code but as I mentioned in my reply to Jelena, the MaxDB query plan for the inner join doesn't look very efficient.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the generated access plan (explain plan)&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
TABLE            INDEX                            STRATEGY                                                              PAGECOUNT
STPO              STPO~M                        INDEX SCAN                                                            6592
                                                            ONLY INDEX ACCESSED                     
MAST                                                   JOIN VIA KEY RANGE                                              312
                                                            TABLE HASHED                                                                                
STLNR        (USED COLUMN)                                                                                
RESULT IS COPIED   , COSTVALUE IS                      9604707

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I use individual queries on each table I can get both of them to perform index only queries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I try using hints and see if that gets me any further.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Doug&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Jul 2010 05:48:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103845#M1508845</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-01T05:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103846#M1508846</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;what is the original problem?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;COUNT(*) does not work with FOR ALL ENTRIES, because of the blockwise processing and because of the removal of the duplicates in the final result set.&lt;/P&gt;&lt;P&gt;=&amp;gt; use describe table&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, if you need only to check whether at least one record fulfills the WHERE-clause,&lt;/P&gt;&lt;P&gt;i.e.    if counter &amp;gt; 0&lt;/P&gt;&lt;P&gt;then read only one record, not with SELECT SINGLE but SELECT UP TO 1 ROWS.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally, in most times a join can be better than the FOR ALL ENTRIES ... but this was said a thousand times.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Jul 2010 07:31:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103846#M1508846</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-01T07:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: COUNT(*) - FOR ALL ENTRIES IN using Native SQL</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103847#M1508847</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt; to perform index only queries.&lt;/P&gt;&lt;P&gt;don't be confused, the index only is not so important and you will not gain so much in MaxDB and MSSQL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Jul 2010 07:33:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/count-for-all-entries-in-using-native-sql/m-p/7103847#M1508847</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-01T07:33:09Z</dc:date>
    </item>
  </channel>
</rss>

