Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
Postgresql function return integer or null

I have a function like this, which checks for appropriate account type:

CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS integer AS $$
  BEGIN
    RETURN (SELECT 1 FROM account WHERE account_no = acc_no AND account_type = acc_type);
  END;
$$ LANGUAGE plpgsql;

Return type is integer, and if no record found its going to be NULL instead. Does it make sense at all? Or i need to somehow explicitly declare that return type is going to be integer or null?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You could use EXISTS logic here:

CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS boolean AS $$
    BEGIN
        RETURN EXISTS (SELECT FROM account WHERE account_no = acc_no AND account_type = acc_type);
    END;
$$ LANGUAGE plpgsql;

This would return either true or false, depending on whether or not a matching record can be found.

over 4 years ago · Santiago Trujillo Denunciar

0

The SQL standard says:

Every data type includes a special value, called the null value, sometimes denoted by the keyword NULL.

So it goes without saying that you can return NULL as an integer result, because NULL is a valid value for any data type.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda