Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast Const Integer to Bigint in Postgres

I'm getting the below error while running the below script. My goal is to create a function in Postgres to return 1 as a bigint. Help please!

hashtagpostgresnoobie

ERROR: function result type must be bigint because of 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;
like image 852
Nick Vaccaro Avatar asked Mar 20 '12 19:03

Nick Vaccaro


1 Answers

You've suddenly encountered the feature ) Record needs two and more fields. So when you have only one out variable, then result must be scalar.

So, you can simply do what 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()
like image 143
Timur Sadykov Avatar answered Oct 12 '22 08:10

Timur Sadykov