How do I limit the amount of results returned in S

2019-02-16 04:34发布

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

标签: sql limit sybase
4条回答
家丑人穷心不美
2楼-- · 2019-02-16 05:01

You can try adding this clause "fetch first n rows only".

查看更多
We Are One
3楼-- · 2019-02-16 05:16

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.

查看更多
该账号已被封号
4楼-- · 2019-02-16 05:18

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.

查看更多
Luminary・发光体
5楼-- · 2019-02-16 05:21

I've happened to stumble on this problem and found answer using TOP and START AT from sybase doc replacing MySQL LIMIT. You need to use ORDER BY for or you will have unpredictable results.

http://dcx.sybase.com/1101/en/dbusage_en11/first-order-formatting.html

SELECT TOP 2 START AT 5 * FROM Employees ORDER BY Surname DESC;

查看更多
登录 后发表回答