get last record from database without looping

Discussion in 'ASP General' started by Matt, Jan 10, 2004.

  1. Matt

    Matt Guest

    how to get the last record from database without looping?

    Whenever the user need to insert a new record to the database, it just
    increment the id field by one from the last record.

    I tried objRS.MoveLast, but it wont work.
    Matt, Jan 10, 2004
    #1
    1. Advertising

  2. > how to get the last record from database without looping?

    There isn't really any concept of "last." If you have a column that
    indicates order without ambiguity, you can use one of these:

    SELECT MAX(column) FROM table

    SELECT TOP 1 * FROM table ORDER BY column DESC

    Also see http://www.aspfaq.com/2499

    > Whenever the user need to insert a new record to the database, it just
    > increment the id field by one from the last record.


    You're killing scalability and concurrency if you're expecting to select the
    max(id), add 1, and use that as part of your insert. IDENTITY (SQL Server)
    and AUTOINCREMENT (Access) are much better suited for this than rolling your
    own.

    On the other hand, if you're using IDENTITY or AUTOINCREMENT and are just
    trying to retrieve the last value inserted, see http://www.aspfaq.com/2174

    > I tried objRS.MoveLast, but it wont work.


    So wait, you're adding a record, and using objRS? Please read
    http://www.aspfaq.com/2191

    In the future, please tell us what database and version you're using,
    include code samples as well as a more explicit description of the problem.
    "wont work" doesn't give us much to go on. Do you get an error message? If
    so, what is it? Or, how is it behaving that you think is wrong? How do you
    think it should behave?

    (I've read your message five times and I still have no idea if you're adding
    rows or retrieving rows.)

    --
    Aaron Bertrand
    SQL Server MVP
    http://www.aspfaq.com/
    Aaron Bertrand [MVP], Jan 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    300
  2. Jim in Arizona
    Replies:
    2
    Views:
    4,687
    Jim in Arizona
    Oct 23, 2008
  3. Daniel Ng
    Replies:
    3
    Views:
    103
    Ray Costanzo [MVP]
    Oct 1, 2004
  4. Replies:
    0
    Views:
    117
  5. Replies:
    5
    Views:
    263
Loading...

Share This Page