1

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.

1 Answer 1

1

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

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

8 Comments

i tried that before and it gives ERROR: syntax error at or near "SELECT" LINE 7: SELECT info into uinfo FROM public.users Where uid=userid; ^ error message
@Axel and you didn’t have semicolon after BEGIN?
CREATE FUNCTION public.getuserinfo(IN userid bigint DEFAULT 00000, OUT uinfo json) RETURNS json LANGUAGE 'sql' AS $BODY$ BEGIN SELECT info into uinfo FROM public.users Where uid=userid; RETURN uinfo; END; $BODY$; this is my code now
@Axel Also only now realized you have language as sql, it probably should be plpgsql since it’s not just sql statements
thank you very much @Sami Kuhmonen. i changed the language and removed the return statement and it is working fie.
|

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.