SQL 如何限制 Sybase 中返回的结果数量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1587135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:02:42  来源:igfitidea点击:

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

sqllimitsybase

提问by Ryan

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.

我需要查询一个包含大量数据的 Sybase 数据库,并想设置一个限制,以便数据库在 10 个结果后停止查询。

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

关键是性能,所以如果它搜索所有结果然后返回最后10个结果是没有用的。

Thanks in advance

提前致谢

采纳答案by Alex Martelli

I believe you can do a SET ROWCOUNT 10first, then all queries in this session until a further SET ROWCOUNTwill return no more than 10 rows. As a comment points out, this affects allfollowing 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 LIMITclause of other engines, which is inherently per-query, but, I don't think you can do anything about that.

我相信您可以先做SET ROWCOUNT 10一次,然后在此会话中执行所有查询,直到进一步SET ROWCOUNT返回不超过 10 行。正如评论指出的那样,这会影响会话中的所有后续查询(不仅仅是SELECTs!),直到关闭(通过设置为 0)或设置不同 - 这种“全局”效果使其不如LIMIT其他引擎的典型条款方便,这本质上是每个查询,但是,我认为您对此无能为力。

回答by brianegge

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.

对于 Sybase 12.5 及更高版本,您可以在 select 语句中使用 top 谓词。这是 MSSQL 已经有一段时间的非 ANSI 功能。

select top 10 * from people

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

您不能在子查询、更新或删除中使用 top,并且没有相应的“bottom”子句。

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.

top 的优点是您不必担心重置它。如果您使用数据库连接池,这一点尤其重要。

回答by Vadim Cote

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.

我碰巧偶然发现了这个问题,并使用 TOP 和 START AT 从 sybase doc 替换了 MySQL LIMIT 找到了答案。您需要使用 ORDER BY 否则您将获得不可预测的结果。

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

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

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

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