1

I'm trying to create a function, like so:

CREATE FUNCTION RETURNONE(DATE)
BEGIN
  RETURN 1;
END

However, when I run this in psql 9.5 I get the following error:

ERROR:  syntax error at or near "BEGIN"
LINE 2: BEGIN
        ^
END

I did see this other StackOverflow thread with a reminiscent problem. Per the second answer, I re-encoded my code in UTF 8, which did nothing. This is my first ever SQL function, so I'm sure I'm missing something painfully obvious. Let me know what!

3
  • 3
    Have you read the CREATE FUNCTION documentation? Commented Feb 27, 2019 at 1:20
  • @muistooshort Honestly, I read that exact page. I don't know why, but I just assumed all that LANGUAGE and $func$ stuff wasn't necessary. In retrospect, rereading it, it's obvious. Thanks! Commented Feb 27, 2019 at 1:32
  • 1
    About that "$func$ stuff": stackoverflow.com/a/12172353/939860 Commented Feb 27, 2019 at 2:19

1 Answer 1

1

You omitted some essential syntax elements:

CREATE FUNCTION returnone(date)
  RETURNS integer
  LANGUAGE plpgsql AS
$func$
BEGIN
  RETURN 1;
END
$func$;

The manual about CREATE FUNCTION.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, this did it. I read that exact document twice, don't know how I came out so wrong. Thanks a billion!
@seisvelas Unrelated, but: you don't need PL/pgSQL for something like that. A simple language sql function would be more efficient if you don't have any procedural code (if, while, or similar constructs) in your function
@a_horse_with_no_name Interesting! I'm a complete beginner, how would this work using language sql?
@seisvelas: essentially removing begin and end and replace return with select. See here: rextester.com/KITLW87371

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.