Solved

Retrieve the last 5 records added in MS SQL Database

Posted on 2004-08-10
5
260 Views
Last Modified: 2008-03-17
Hello,

Probably a simple question, but how could I retrieve the last 5 records added to a table? (assuming there is no id field to work with)

I know MS SQL supports a 'TOP' function to return the top # of results you specify.. so is there anyway to return the last 5 records efficiently?

Thanks!
0
Comment
Question by:jacowhee
5 Comments
 
LVL 11

Expert Comment

by:sciber_dude
ID: 11767583
Is there a data/time field that you could sort on?

If you do, sort it by desc and pick up the top 5. Otherwise, I am not familiar with any other way.

:) SD
0
 
LVL 23

Accepted Solution

by:
adilkhan earned 125 total points
ID: 11767605
Can You Do a Order By on a Field?

Order By [Field_Name] Desc
0
 

Author Comment

by:jacowhee
ID: 11767636
Sorry, I am trying to do this based on not using any field for sorting, just trying to collect the last five.

There is a dateadded field that I would have liked to use, but it doesn't have updated data (need to find a bug preventing it from updating) I was just looking for a quick fix.

Thanks for the comments.
0
 
LVL 6

Expert Comment

by:casstd
ID: 11768963
Hi,

          As adilkhan mention you can use your Primarykey and DESC order to find last five records.

SQL = "Select * from table ORDER BY PrimaryKeyID DESC;"

SET Rs = Conn.Execute(SQL)

if not rs.eof then
        do until rs.eof
               Count = Count + 1
                  'Display your last five records information here.
                 if Cint(Count) =  5 then
                            exit do
                 end if
           rs.MoveNext
       LOOP
End if

Hope this would be helpful.
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 11777717
Just a thought! Whats the difference between ...

> Order By [Field_Name] Desc

and

>Is there a data/time field that you could sort on?
> If you do, sort it by desc and pick up the top 5.

Anyway, I am glad it all worked out.
:) SD
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now