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

320
Visualizações
Postgresql - testing RECORD types (for fields or null)

I am having problems dealing with plpgsql and RECORD types.

Does anyone know of a way to simply test a returned RECORD type to know if it was set or not?

Basic Pattern:

A function returns a RECORD and where it may be unset (i.e. select into returned no records) or it is set to a single record.

create or replace function blah()
returns RECORD as $$
declare
  rec RECORD;
begin
   rec := row(null); --Do not want to hear about it not being assigned
   select *, status_field into rec ... ;
   if found then
       raise notice '%', rec.status_field is not null   --prints 't'
   end if

   if not found then
       raise notice '%', rec.status_field is not null   --Throws Error (I just want it to say 'f'
   end if; 
end; $$

create or replace function callblah()
returns text as $$
declare
  rtText text;
  blahRec RECORD;
begin
 blahRed := blah(...);

 -- what test can I do to determine if record is set or not.
 -- Try: if blah is not null then -- nope always returns false for is null and is not null
 -- Try: if blah.status is not null then -- nope throws error on field not existing
 -- What test condition do I need to add here to simply test if record is either (NOT FOUND) or (FOUND where field will exist).
 ...
 end if
end; $$
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Ah.. is null operator on RECORD is (all fields null), and not is null (all fields not null). As I have some fields null I need to do

if blah is null then
   -- No record found
else
   if blah.status is not null then
   -- Record found with field I want
   end if;
end if;
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