Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Select a fixed number of records

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-06-03 : 13:03:02
I have a sql line that selects records:

select * from prodstatistics order by shift_started desc


I do not need to read the entire table and want to limit the read to the first 500 records. How would I embedd that in my current statement?

Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-03 : 13:07:05
SELECT TOP 500 *
FROM prodstatistics
ORDER BY shift_started DESC

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-06-03 : 16:06:42
Thank you, it worked...
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-06-03 : 21:23:15
If you are interested in taking sections of your data you can use this.


select Row_Number() over (Order by Shift_Started desc) as RowID, *
into #tmp
from prodstatistics

Select * from #tmp a where a.RowID between 1 and 500
Select * from #tmp a where a.RowID between 501 and 1000
Select * from #tmp a where a.RowID between 1001 and 1500

etc

Go to Top of Page
   

- Advertisement -