The number of rows affected by the PostgreSQL fetch operation

Source: Internet
Author: User


How to get the number of rows affected by the normal operation, PostgreSQL inside has a built-in variable diagnostics and Row_count can do this.

First, the environment:
Db:9.4beta3

Second, prepare:
postgres=# create table test(id int);
CREATE TABLE
postgres=# insert into test select generate_series(1,20);
INSERT 0 20
postgres=# select * from test;
 id 
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
(20 rows)
Third, create the function:
CREATE OR REPLACE FUNCTION fun_affect_rows ()
   RETURNS text AS
$ BODY $

declare
v_count int; begin

insert into test values (99), (98);
GET DIAGNOSTICS v_count = ROW_COUNT;
raise notice ‘The amount of data inserted this time%’, v_count;

delete from test where id <15;
GET DIAGNOSTICS v_count = ROW_COUNT;
raise notice ‘the amount of data deleted this time%’, v_count;

update test set id = 100 where id> 90;
GET DIAGNOSTICS v_count = ROW_COUNT;
raise notice ‘the amount of data this update%’, v_count;

return ‘Test completed’;
end;
$ BODY $
   LANGUAGE plpgsql VOLATILE
   COST 100;
ALTER FUNCTION fun_affect_rows ()
   OWNER TO postgres;
Four, operation situation:
postgres = # select * from fun_affect_rows ();
NOTICE: The amount of data inserted this time 2
NOTICE: The amount of data deleted this time 14
NOTICE: The amount of data updated this time 2
  fun_affect_rows
-----------------
  Test finished
(1 row) 
If you want to return the results of DML, then use returning.

v. References
Http://www.postgresql.org/docs/9.0/static/plpgsql-statements.html


The number of rows affected by the PostgreSQL fetch operation


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.