i need your help,
I have a query with n times loop operation of insert and update.
I decided to use 'UPSERT', since it doesn’t require loop operation.
ex:
WITH upsert AS
(UPDATE employee_table
SET rollno=input_rollno,
name=input_name
RETURNING *)
INSERT INTO employee_table (rollno, name)
SELECT 'input_rollno',input_name
from employee_table
WHERE NOT EXISTS (SELECT * FROM upsert);
The query is working fine and now the problem is, i want the affected row counts of individual insert and update..
i tried adding 'GET DIAGNOSTICS' after update but the query fails. i can take row count only after ending upsert.
When i added get diagnostics rowcount after update i got error: ex:
-- THIS IS WRONG. It's an EXAMPLE.
declre
num_rows INTEGER :=0;
num_rows1 INTEGER :=0;
WITH upsert AS
(UPDATE employee_table
SET rollno=input_rollno,
name=input_name
RETURNING *)
GET DIAGNOSTICS num_rows = ROW_COUNT
INSERT INTO employee_table (rollno, name)
SELECT 'input_rollno',input_name
from employee_table
WHERE NOT EXISTS (SELECT * FROM upsert);
GET DIAGNOSTICS num_rows1 = ROW_COUNT;
i need to know how many rows got effected while updating and how many while inserting...
Using PostgreSQL 9.3