PostgreSQL 9.3 中的@@ROWCOUNT

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

@@ROWCOUNT in PostgreSQL 9.3

postgresqlpostgresql-9.3

提问by MAK

I want to returns the number of rows affected by the last statement.

我想返回受最后一条语句影响的行数。

Using Microsoft SQL Server 2008 R2 I do it this way:

使用 Microsoft SQL Server 2008 R2 我这样做:

SELECT * FROM Test_table;

SELECT @@ROWCOUNT AS [Number Of Rows Affected];

Will gives:

将给出:

Number Of Rows Affected
-----------------------
10

How about in PostgreSQL 9.3?

在 PostgreSQL 9.3 中怎么样?

采纳答案by Eelke

AFAIK there is no such construct in postgresql however the number of rows is part of the result you get from postgresql.

AFAIK postgresql 中没有这样的构造,但是行数是您从 postgresql 获得的结果的一部分。

CORRECTION: as a_horse_with_no_name states in his comment there is something similar which can be used within PL/pgSQL. Also see example in answer posted by Achilles Ram Nakirekanti

更正:正如 a_horse_with_no_name 在他的评论中所说的,有一些类似的东西可以在 PL/pgSQL 中使用。另请参阅 Achilles Ram Nakirekanti 发布的答案中的示例

From within programs however my original suggestion is in most cases easier then having to resort to the usage of PL/pgSQL.

然而,在程序内部,我最初的建议在大多数情况下比使用 PL/pgSQL 更容易。

When using libpq: On the result of a select you can use PQntuples to determine the number of rows returned. For update, insert and delete you can use PQcmdTuples with the result to get the number of rows affected.

使用 libpq 时:在选择的结果上,您可以使用 PQntuples 来确定返回的行数。对于更新、插入和删除,您可以使用 PQcmdTuples 和结果来获取受影响的行数。

Other client libraries often have similar functionality.

其他客户端库通常具有类似的功能。

回答by Achilles Ram Nakirekanti

DO $$
DECLARE
     total_rows integer;
BEGIN
  UPDATE emp_salary
   SET salary = salary+1;
   IF NOT FOUND THEN
      RAISE NOTICE'Now rows found %';
   ELSIF FOUND THEN
   GET DIAGNOSTICS total_rows := ROW_COUNT;
      -- the above line used to get row_count
      RAISE NOTICE'Rows Found : total_rows: %', total_rows;
   END IF; 
END $$;