Last Record inthe datadase...

Results 1 to 2 of 2

Thread: Last Record inthe datadase...

  1. #1
    Join Date
    Dec 1969

    Default Last Record inthe datadase...

    hi please help me i need to get the last record in a database iv tryed different ways and nothing works. i this code below did work at one stage but now it doesnt and i dont know why.. please can someone help me URGENTLY.. thank you<BR><BR>&#060;%<BR>set Rs1= server.CreateObject("ADODB.Recordset")<BR> "select* from links","DSN=links;UID=sa;PWD=;"<BR>while not Rs1.eof<BR>lastid=Rs1("LinkID")<BR>Rs1.movenext<BR >wend<BR>%&#062; <BR>

  2. #2
    Join Date
    Dec 1969

    Default ORDER BY ... DESC

    First of all, database engines do *NOT* have a concept of record placement! There ISN&#039;T ANY "first record" or "last record" so far as the base engine is concerned.<BR><BR>If you delete a record, then a *good* db will put the next inserted record into the "hole" left by the deleted record, to save space! So what does it mean to be the "last" record??? The last record *physically* might be nowhere near the last record *chronologically*.<BR><BR>SO...<BR><BR>It is up to *you* to provide ordering to a database. Typically, you do this by putting in a "recordID" field [e.g., one that increments with each added Autonumber field in Access, IDENTITY in SQL Server] or a "timeAdded" field [that gets the Date/Time the record was added].<BR><BR>*IF* you have such a field, then you can do:<BR><BR>SELECT TOP 1 * FROM links ORDER BY recordID DESC<BR>or<BR>SELECT TOP 1 * FROM links ORDER BY timeAdded DESC<BR><BR>and that will get *one* record, the last one by record id or time added. [DESC is short for DESCending, meaning you want the record in reverse numerical, date, or alphabetic order by that field, as appropriate.]<BR><BR>The VERY WORST THING you could do is what you were trying. Nothing much you could do would be slower.<BR><BR>*** NOW ***<BR><BR>A bigger lecture. *IF* the purpose of this exercise is to find the last record added by this session, *this will not work*!!<BR><BR>With multiple users you are hosed!<BR><BR>User 1 adds record with recordID 371<BR>User 2 adds record with recordID 372<BR>User 1 gets "latest" record: 372<BR>User 2 gets "latest" record: 372<BR>User 2 modifies record 372<BR>User 1 modifies record 372<BR><BR>KABLOOEY!<BR><BR>If you are interested in getting an identifier of the *just-added* record, go look in the ASPFAQs for the discussion of "AddNew".<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts