URGENT(HOW TO LOCATE LAST RECORD IN DATABASE)

D

Daniel Ng

Thanks for the 2 guys how hep to to solve to count the number of recount
in forum. I'm done wif that. Got 1 more question..

Example
Record 1: ID 2 VALUE 2
Record 2: ID 3 VALUE 3
Record 3: ID 2 VALUE 1

How can i retrieve the VALUE of the last record of ID 2 using ASP?? In
this case, the asnwer given to me should be Record 3.
 
A

Aaron [SQL Server MVP]

What makes "record 3" the "last record"?

You realize that a table is an unordered set of rows, right? In order to
have a *consistent* result for the "last" row you need to tell the database
how to determine last (e.g. using an ORDER BY or MAX/MIN aggregate).
 
B

Bob Barrows [MVP]

Daniel said:
Thanks for the 2 guys how hep to to solve to count the number of
recount in forum. I'm done wif that. Got 1 more question..

Example
Record 1: ID 2 VALUE 2
Record 2: ID 3 VALUE 3
Record 3: ID 2 VALUE 1

How can i retrieve the VALUE of the last record of ID 2 using ASP?? In
this case, the asnwer given to me should be Record 3.

You need to rid yourself of the idea that there is such a thing as a "last"
record in a table. A table in a relational database is defined as an
unordered set of rows and columns. The only way to impose a reliable and
consistent order on a set of rows is to use an ORDER BY clause in a sql
statement. Having said that, I assume that you are defining the "last"
record in your table as the record containing hte highest ID value. There
are a couple ways to return this record, which may or may not work depending
on the type and version of database you are using (please, always tell us
this information):

SELECT Top 1 <column_list>
FROM <table_name>
ORDER BY ID DESC

SELECT <column_list>
FROM <table_name>
WHERE ID =
(SELECT Max(ID) FROM <table_name>)

There are other techniques, but this should get you going.

Bob Barrows
 
R

Ray Costanzo [MVP]

Your posts are no more urgent than anyone else's. Please stop SHOUTING in
your subject lines and trying to be more important than everyone else. Your
question will not get answered any sooner by doing this. If anything, it
will have the opposite effect.

Ray at work
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top