I came into a problem in postgres sql function, I need to return all records when the array of integer is empty.
For now I am using dynamic sql to achieve it with if statement checking if the array is empty so don't concat in statement in the query.
I want to know if there is another way to achieve it without dynamic sql?
eg using dynamic sql:
CREATE OR REPLACE FUNCTION testme() RETURNS TABLE(Title text) LANGUAGE plpgsql AS $$
DECLARE
one int[];
BEGIN
one:= '{}';
RAISE NOTICE 'value:%', array_length(one,1);
if array_length(one,1) > 0 THEN
RETURN QUERY EXECUTE 'SELECT "Title" from "Tickets" where "TicketID"
IN(SELECT(UNNEST($1)));' USING one;
ELSE
RETURN QUERY EXECUTE 'SELECT "Title" from "Tickets"';
END IF;
END
$$;
SELECT testme();