@@ROWCOUNT in PostgreSQL 9.3

时光毁灭记忆、已成空白 提交于 2019-12-01 16:27:03

问题


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

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

SELECT * FROM Test_table;

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

Will gives:

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

How about in PostgreSQL 9.3?


回答1:


AFAIK there is no such construct in postgresql however the number of rows is part of the result you get from 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

From within programs however my original suggestion is in most cases easier then having to resort to the usage of 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.

Other client libraries often have similar functionality.




回答2:


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 $$;


来源:https://stackoverflow.com/questions/28109074/rowcount-in-postgresql-9-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!