Search code examples
postgresqlsql-functionsql-returning

ERROR: syntax error at or near "RETURN" , PostgreSQL function


I am trying to write a function which returns a json data from my table by adding person id . I am using pgAdmin 4 for it.

CREATE FUNCTION public.getuserinfo(IN userid bigint DEFAULT 00000, OUT uinfo json)
RETURNS SETOF json
LANGUAGE 'sql'
VOLATILE 
AS $BODY$
SELECT info into uinfo FROM public.users Where uid=userid;
RETURN uinfo;
$BODY$;
ALTER FUNCTION public.getuserinfo(bigint)
 OWNER TO sun;
GRANT EXECUTE ON FUNCTION public.getuserinfo(bigint) TO sun 
WITH GRANT OPTION;
REVOKE ALL ON FUNCTION public.getuserinfo(bigint) FROM 
PUBLIC;
COMMENT ON FUNCTION public.getuserinfo(bigint)
  IS 'gets user info by id';

but i am getting this error.error image i searched on tutorials and another previous post this but got no help. thanks in advance.


Solution

  • You’re not telling the function there’s a sequence of things, so it expects only one statement to be there and there’s two. Use BEGIN and END:

    CREATE FUNCTION public.getuserinfo(IN userid bigint DEFAULT 00000, OUT uinfo json)
    RETURNS SETOF json
    VOLATILE 
    AS $BODY$
    BEGIN
      SELECT info into uinfo FROM public.users Where uid=userid;
      RETURN uinfo;
    END;
    $BODY$ LANGUAGE plpgsql;
    

    Also the language probably should be PL/PGSQL