Solved

Retrieve the last 5 records added in MS SQL Database

Posted on 2004-08-10
5
248 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

23 Experts available now in Live!

Get 1:1 Help Now