By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

get last record from database without looping

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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/
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.