11

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;

1 Answer 1

22

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()
Sign up to request clarification or add additional context in comments.

4 Comments

I apologize for my slowness, but could you please be more specific? I had a hard time following that. I've been using Postgres for all of about 10 hours now. :/
I'll provide exact snippet after lunch )
It turned out to be simpler. Take a look.
Holy fantastic answers, Batman! I'd upvote more than once if I could.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.