3

I'm writing one postgresql function that executes some update query. I need to make sure that any rows affected or not and return success/failure message accordingly.

If(rows affected)? then 'update success' else 'update failed'

How to RAISE NOTICE the row count??

-- Get count from UPDATE
WITH rows AS (
    UPDATE distributors
    SET dname = 'JKL Widgets'
    WHERE did <= 10
    RETURNING 1
)
SELECT count(*) FROM rows;
2

1 Answer 1

4

use get diagnostics, e.g:

t=# create table so17(i int);
CREATE TABLE
t=# insert into so17 values(0);
INSERT 0 1
t=# do $$
declare d int;
begin
update so17 set i = 0 where i=0;
get diagnostics d = row_count;
raise info 'updated: % rows', d;
update so17 set i = 0 where i=1;
get diagnostics d = row_count;
raise info 'updated: % rows', d;
end;
$$;
INFO:  updated: 1 rows
INFO:  updated: 0 rows
DO
Sign up to request clarification or add additional context in comments.

2 Comments

I'm executing update query followed by a select query. Its always returning row count as 1 even update query is failed.
you should share the code. put get diagnostics STRAIGHT after update, otherwise you get other rows count

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.