SELECT @@ROWCOUNT Oracle 等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33020243/
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-19 03:02:17  来源:igfitidea点击:

SELECT @@ROWCOUNT Oracle equivalent

sqloraclerowcount

提问by hmartos

I have an application for query management. Previously I was using SQL Server database, and to get the number of affected rows by a query I used to do:

我有一个查询管理应用程序。以前我使用 SQL Server 数据库,并通过我以前执行的查询获取受影响的行数:

SELECT * FROM TABLE(or any other select query)

SELECT * FROM TABLE(或任何其他选择查询)

and then I do SELECT @@ROWCOUNTto get the number of rows affected by the last executed query.

然后我SELECT @@ROWCOUNT获取受上次执行的查询影响的行数。

I have read about SQL%ROWCOUNT, but I am not able to make it work in a SELECT statement

我已经阅读过SQL%ROWCOUNT,但我无法在 SELECT 语句中使用它

Is there any way to do this in a Oracle database?. Thank you!

有没有办法在 Oracle 数据库中做到这一点?谢谢!

Edited:

编辑:

I have solved this by doing SELECT COUNT(*) FROM (QUERY)to get the number of rows affected by the query, I discarted this approach because it was not working in SQL Server if the query contains an ORDER BYclause.

我通过SELECT COUNT(*) FROM (QUERY)获取受查询影响的行数解决了这个问题,我放弃了这种方法,因为如果查询包含ORDER BY子句,它在 SQL Server 中不起作用。

回答by sstan

I don't know of any exact equivalent in Oracle that you can use in pure SQL.

我不知道您可以在纯 SQL 中使用 Oracle 中的任何确切等效项。

An alternative that may work for you, depending on your specific need, is to add a count(*) over ()to your selectstatement to give you the total number of rows. It would at least save you from having to re-execute the query a 2nd time.

根据您的特定需要,另一种可能对您有用的替代方法是count(*) over ()在您的select语句中添加 a以提供总行数。它至少可以使您不必第二次重新执行查询。

select t.*,
       count(*) over () as num_rows
  from table t
 where ...

Or, if you can't change the original query, then you can wrap it like this:

或者,如果您无法更改原始查询,则可以将其包装如下:

select t.*,
       count(*) over () as num_rows
  from (query) t