@@ROWCOUNT in PostgreSQL 9.3

丶灬走出姿态 提交于 2019-12-01 17:08:27

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.

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