I am trying to figure out how to update a postgres table using an array of object. I want each object in the array to correspond to a new row, with each key corresponding to a column, and each value is the data to be inserted into the column.
Also would like to know how to call that procedure in c#?
Here the the data format:
[
{ col1: a, col2: 5, col3: 1, col4: one},
{ col1: b, col2: 6, col3: 2, col4: two},
{ col1: c, col2: 7, col3: 3, col4: three},
{ col1: d, col2: 8, col3: 4, col4: four},
]
Here is my expected output:
col1 (varchar)| col2 (integer) | col3 (integer) | col4 (varchar)
-----------------+----------------+--------------------+------------------
a | 5 | 1 | one
b | 6 | 2 | two
c | 7 | 3 | three
d | 8 | 4 | four
I am passing the data format as array in stored procedure.
But want to know, how to cal the SP from c#?
The stored procedure I have written is:
CREATE OR REPLACE FUNCTION dbo.sp_insertorupdatereadings(d dbo.reading[])
RETURNS boolean AS
$BODY$
DECLARE
begin
--Update min values
update dbo.reading set
p7_1_5_1_0_first =subquery.p7_1_5_1_0_first,
p7_1_5_1_0_last =subquery.p7_1_5_1_0_last,
p7_1_5_2_0_first=subquery.p7_1_5_2_0_first,
p7_1_5_2_0_last=subquery.p7_1_5_2_0_last
From (select * from unnest(d)) as subquery
where dbo.reading.p7_1_5_1_0_first= subquery.p7_1_5_1_0_first;
-- insert new records
insert into dbo.reading
select * from unnest(d) as inserd where (id) not in (select id from dbo.reading);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION dbo.reading(dbo.reading[])
OWNER TO postgres;