I am trying to write a trigger in which I have to create a Json after getting data(multiple rows) from another table. I have two table task and data. data table contains multiple information about the task in key/value pair.
task schema
taskid
description
value
data table schema
id
taskid
key
value
Now in trigger I want to fetch all data corresponding to a task and prepare a JSON from that. I am using below
CREATE OR REPLACE FUNCTION event()
RETURNS trigger AS
$BODY$
DECLARE
_message json;
_data data;
BEGIN
SELECT * INTO _data FROM data WHERE data.taskid = New.taskid;
IF TG_OP = 'INSERT' THEN
_messag:=json_build_object('taskId',NEW.taskid,'description',NEW.description,'value',NEW.value,'clientis',_data);
END IF;
INSERT INTO events(message)
VALUES(_messag);
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
Here, I am using below query to get all data from data table corresponding to a taskid
SELECT * INTO _data FROM data WHERE data.taskid = New.taskid;
Here, the problem is that I am getting only one row. How can all rows will be fetched? If anyone has any idea?