Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Postgres sql in return all when array empty without dynamic sql

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();
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

select  "Title"
from "Tickets"
where
    "TicketID" = any (one)
    or
    cardinality(one) = 0;

cardinality returns 0 for an empty array whereas array_length returns null

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!