Cast Const Integer for Bigint in Postgres

I get the following error when running below script. My goal is to create a function in Postgres to return 1 as bigint. Help me please!

hashtagpostgresnoobie

ERROR: the type of the result of the function must be bigint due to OUT parameters

CREATE OR REPLACE FUNCTION GetNumberOne(
    OUT numberone bigint)
  RETURNS SETOF record AS
$BODY$

    SELECT CAST(1 AS BIGINT) AS "NUMBERONE";

$BODY$
  LANGUAGE sql VOLATILE;
+5
source share
1 answer

You unexpectedly came across this function) Recording requires two or more fields. Therefore, when you have only one out variable, the result should be scalar.

So you can just do what the compilers ask)

CREATE OR REPLACE FUNCTION GetNumberOne(
        OUT numberone bigint)
      RETURNS bigint AS
    $BODY$

        SELECT CAST(1 AS BIGINT) AS "NUMBERONE";

    $BODY$
      LANGUAGE sql VOLATILE;

Plpgsql example:

CREATE OR REPLACE FUNCTION NumberOne()
      RETURNS bigint AS
    $BODY$
      DECLARE num bigint;
      BEGIN
        num := 1;
        RETURN num;
      END
    $BODY$
      LANGUAGE plpgsql VOLATILE;
      select * from NumberOne()
+9
source

All Articles