Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

limit

sybase

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

like image 305
Ryan Avatar asked Oct 19 '09 05:10

Ryan


People also ask

How do you limit the number of records to return from a query?

The SQL SELECT LIMIT statement is used to retrieve records from one or more tables in a database and limit the number of records returned based on a limit value. TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results.

How do I limit the number of results returned in SQL?

The SQL LIMIT clause constrains the number of rows returned by a SELECT statement. For Microsoft databases like SQL Server or MSAccess, you can use the SELECT TOP statement to limit your results, which is Microsoft's proprietary equivalent to the SELECT LIMIT statement.

What is used to limit the result of a query?

The limit keyword is used to limit the number of rows returned in a query result.

Which method is used to set a limit on number of rows those will be returned?

Correct Option: B. setMaxRows(int i) method is used to limit the number of rows that the database returns from the query.


2 Answers

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.

like image 56
brianegge Avatar answered Sep 22 '22 16:09

brianegge


I believe you can do a SET ROWCOUNT 10 first, then all queries in this session until a further SET ROWCOUNT will return no more than 10 rows. As a comment points out, this affects all following queries in the session (not just SELECTs!) until turned off (by setting to 0) or set differently -- this "global" effect makes it less handy than the typical LIMIT clause of other engines, which is inherently per-query, but, I don't think you can do anything about that.

like image 25
Alex Martelli Avatar answered Sep 22 '22 16:09

Alex Martelli