我目前正在学习很多 PostgreSQL,尤其PLPGSQL是在处理函数中的查询结果方面很挣扎。我想围绕用户表创建一个包装器,稍后使用结果然后返回它。在我的情况下,用户和帐户是两个不同的表,我想一次性创建它。
我的第一个也是天真的方法是构建以下内容:
CREATE OR REPLACE FUNCTION schema.create_user_with_login (IN email varchar, IN password varchar, IN firstname varchar DEFAULT NULL, IN surname varchar DEFAULT NULL)
RETURNS schema.user
LANGUAGE plpgsql
VOLATILE
RETURNS NULL ON NULL INPUT
AS
$$
declare
created_user schema."user";
begin
INSERT INTO schema."user" ("firstname", "surname", "email")
VALUES (firstname, surname, email)
RETURNING * INTO created_user;
// [...] create accounts and other data using e.g. created_user.id
// the query should return the initially created user
RETURN created_user
end;
$$;
Run Code Online (Sandbox Code Playgroud)
这种方法不起作用,因为 …