Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > get last record from database without looping

Reply
Thread Tools

get last record from database without looping

 
 
Matt
Guest
Posts: n/a
 
      01-10-2004
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.





 
Reply With Quote
 
 
 
 
Aaron Bertrand [MVP]
Guest
Posts: n/a
 
      01-10-2004
> 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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
Looping Problem (Generating files - only the last record generates a file) vasilijepetkovic@yahoo.com Python 6 10-26-2005 08:10 PM
URGENT(HOW TO LOCATE LAST RECORD IN DATABASE) Daniel Ng ASP General 3 10-01-2004 02:35 PM



Advertisments