Solved

Retrieve the last 5 records added in MS SQL Database

Posted on 2004-08-10
5
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
Saqib Khan 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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