<?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: Finding last Record from a table in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895193#M54103</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Srinivas.  The highest key is not necessarily the last one inserted.  An example is an employee table keyed by SSN.  Just because an employee has the highest SSN number does not mean that he/she was the last one inserted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am somewhat confused by what you mean by "automatically sorted by its key fields"?  My understanding is that you cannot count on the order of the data returned from the data base manager unless you specify an order.  Do you have a different understanding?&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="--------------------------------------------" /&gt;&lt;P&gt;Charlie...&amp;lt;a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=jvmiygdzb%2fyqmtolryqkmq%3d%3d"&amp;gt;[BC]&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 20 Mar 2005 17:09:55 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2005-03-20T17:09:55Z</dc:date>
    <item>
      <title>Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895187#M54097</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi , &lt;/P&gt;&lt;P&gt;  What is the method to find the last inserted  record from a database table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;AArun&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Mar 2005 02:33:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895187#M54097</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-19T02:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895188#M54098</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;

data: index type i.
Describe table itab lines index.
read table itab index index.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Mar 2005 03:23:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895188#M54098</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2005-03-19T03:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895189#M54099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Rich's post is in regards to an internal table, not a database table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Couple of ideas for a database table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) Some tables record the user, date, time in the data.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Create change documents or change pointers for the table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3) Use table logging.  Check the documentation at&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.sap.com/saphelp_470/helpdata/en/7e/c81ed552c511d182c50000e829fbfe/frameset.htm" target="test_blank"&gt;http://help.sap.com/saphelp_470/helpdata/en/7e/c81ed552c511d182c50000e829fbfe/frameset.htm&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Mar 2005 03:58:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895189#M54099</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-19T03:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895190#M54100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;oops, looks like I need to pay more attention.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Mar 2005 13:48:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895190#M54100</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2005-03-19T13:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895191#M54101</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;Rich's example also applies for only internal tables of  type "standard". And if during the execution there is any sorting mechanism for the internal table, it might not work properly either. So, handling this problem needs some work-around regarding to what is really needed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyways, since the issue is about something different I jump to the relevant topic. Charles has signed to possible solutions.&lt;/P&gt;&lt;P&gt;For this, your table should contain some purpose-specific fields (e.g. timestamp). Timestamping may be needed twice for the first creation and for the last change.&lt;/P&gt;&lt;P&gt;Or if your table has a key field which takes its value from a number range object, you can get te record with the greatest number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your table has not either of these and is a standard table. Try coding into an exit to store a record in another customer table containing the key of the standard table record and the timestamp or an order number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Considering SAP's backup-restore strategy, it seems at deeper infrastructure they store something more about this. However, they pertain to the technical layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this much helps...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*--Serdar &amp;lt;a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d"&amp;gt;[ BC ]&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Mar 2005 21:10:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895191#M54101</guid>
      <dc:creator>ssimsekler</dc:creator>
      <dc:date>2005-03-19T21:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895192#M54102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Any database table is automatically sorted by its key fields. So if you do a select of table in the descending order of the key fields up to 1 rows, then you will get the last record.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 20 Mar 2005 16:32:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895192#M54102</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-20T16:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895193#M54103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Srinivas.  The highest key is not necessarily the last one inserted.  An example is an employee table keyed by SSN.  Just because an employee has the highest SSN number does not mean that he/she was the last one inserted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am somewhat confused by what you mean by "automatically sorted by its key fields"?  My understanding is that you cannot count on the order of the data returned from the data base manager unless you specify an order.  Do you have a different understanding?&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="--------------------------------------------" /&gt;&lt;P&gt;Charlie...&amp;lt;a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=jvmiygdzb%2fyqmtolryqkmq%3d%3d"&amp;gt;[BC]&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 20 Mar 2005 17:09:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895193#M54103</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-20T17:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895194#M54104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi , &lt;/P&gt;&lt;P&gt; Thanks all for your replys.&lt;/P&gt;&lt;P&gt;My actual requirement is that i have to access the most recent record from table MARDH , for a user entered range of material number and plant.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One method is to get all the records for this range into an internal table ,ordered by date in descending order and then access the first record for each material-plant combination and store it into another internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any better way of doing it as in this case unnecessary processing is required.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regrads&lt;/P&gt;&lt;P&gt;Arun&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Mar 2005 04:20:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895194#M54104</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-21T04:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895195#M54105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are two ways to find the last record from the table and to determine the best way is, for you to analyse where the processing needs to be done: &lt;/P&gt;&lt;P&gt;a) Database&lt;/P&gt;&lt;P&gt;b) Application layer&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Application layer, Rich and Charles have already mentioned. In case of database layer, just to minimize few lines of code, you can do something similar to this :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data: lv_lfgja type mardh-lfgja.
data: lv_werks type werks_d.

select max( LFGJA )
  into lv_lfgja
  from mardh
 where werks = '3210'.

if sy-subrc = 0.
  write lv_lfgja.
else.
  write 'unsuccessful'.
endif.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the above code, you will only get LFGJA or the fiscal period. In order to get other details, you would have to do something similar to this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data: lv_lfgja type mardh-lfgja.
data: lv_werks type werks_d.

select single werks max( LFGJA )
         into (lv_werks, lv_lfgja)
         from mardh
        where werks = '3210'
        group by werks.

if sy-subrc = 0.
  write : lv_werks, lv_lfgja.
else.
  write 'unsuccessful'.
endif.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Subramanian V.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Mar 2005 04:58:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895195#M54105</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-21T04:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895196#M54106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Arun,&lt;/P&gt;&lt;P&gt;       Another possible solution is to check for the table CDHDR with the transaction name and Change object as Material or use the function module CHANGEDOCUMENT_READ_HEADERS to get the list of recently changed records. Maybe this wud help u out Arun.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Abhishek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Mar 2005 05:05:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895196#M54106</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-21T05:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895197#M54107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi if you are using oracle database then try this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i am using emp table which has following table&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EMPNO                  &lt;/P&gt;&lt;P&gt;ENAME                  &lt;/P&gt;&lt;P&gt;JOB                    &lt;/P&gt;&lt;P&gt;MGR                    &lt;/P&gt;&lt;P&gt;HIREDATE               &lt;/P&gt;&lt;P&gt;SAL                    &lt;/P&gt;&lt;P&gt;COMM                   &lt;/P&gt;&lt;P&gt;DEPTNO                 &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select * from emp&lt;/P&gt;&lt;P&gt;where empno=(select empno from emp&lt;/P&gt;&lt;P&gt;having max(rownum)=(select count(rownum) from emp)&lt;/P&gt;&lt;P&gt;group by empno);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rai Zeeshan Zaffar Khan.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Mar 2005 08:47:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895197#M54107</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-21T08:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: Finding last Record from a table</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895198#M54108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Charles,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I see what you mean. Yes, you are correct in saying that the last one entered need not be at the end of the table. I did a small test table with 3 key fields and one non-key field. Even though I entered records into the table in random order of the key fields, when I looked in SE16, they were all sorted by the key field. So that my first record entered ended up as the last record of the table(since its key was the highest for the sort). This is the behaviour that I wanted to present by saying 'automatically sorted', which is to indicate that no matter how you enter the records, system is going to sort the entries in ascending order of the table key, before saving them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry for the confusion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Srinivas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Mar 2005 16:17:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/finding-last-record-from-a-table/m-p/895198#M54108</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2005-03-21T16:17:55Z</dc:date>
    </item>
  </channel>
</rss>

