Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda