How do I limit the amount of results returned in Sybase?

Ryan picture Ryan · Oct 19, 2009 · Viewed 66.9k times · Source

I need to query a Sybase database which has a lot of data in it, and would like to set a limit so the DB stops the query after 10 results.

The key thing is performance, so it would be no use if it searched all results and then then returned the last 10 results.

Thanks in advance

Answer

brianegge picture brianegge · Oct 20, 2009

With Sybase 12.5 and later you can use the top predicate in your select statement. This is a non-ANSI feature which MSSQL has had for quite a while.

select top 10 * from people

You can't use top in subqueries, updates, or deletes, and there is no corresponding 'bottom' clause.

The advantage of top is you don't have to worry about resetting it. This is especially important if you are using a database connection pool.