I have a table which maintains records of the transactions carried out within the enterprise.
Now I am writing a function that returns the transaction IDs that were found in the table. Following is the code of the function :
CREATE OR REPLACE FUNCTION searchList(inputList varchar) RETURNS character varying AS $BODY$
DECLARE
newList varchar;
rowcount integer;
splitted varchar[];
BEGIN
splitted = regexp_split_to_array(inputList,','); --splits the string by comma as a delimiter and produces an array as a result
select incidents.transaction_id into newList from incidents where transaction_id IN(select unnest(splitted)); --unnest function expands the array and is replaced by all the values of array
GET DIAGNOSTICS rowcount = ROW_COUNT; -- ROW_COUNT is the literal which is gonna provide the number of rows returned by previous query
IF rowcount = 0 THEN
return 'Match does not exist';
ELSE
return newList;
END IF;
END;
$BODY$ LANGUAGE plpgsql;
I am providing a string as an input to the function and then splits that string to form an array and then I am running a select query that is intended to store the transaction id of all such records into a variable whose transaction id was present within the array and finally return the variable
However, when I execute this function I only get one of the transaction_id as output even if all the transaction_id provided are a match.
I am using select into to store the result into variable however I have a feeling it only stores one of the extreme values of the column(first/last)
Is there any way we can store the result of a single column which may contain multiple entries into the variable.
I know there are multiple threads already here related to select into, however none of them helped. I have recently begin using pgsql so have little knowledge of the subject