我正在尝试将一些网络应用程序逻辑转移到 postgres 函数中。但是我在创建一个非常基本的插入函数时遇到了一些错误。
这是我正在尝试创建的功能;
CREATE OR REPLACE FUNCTION create_user(IN email EMAIL, password TEXT, thumb TEXT)
RETURNS text AS
$BODY$
BEGIN
insert into users (unqid, thumb, email, password)
values (gen_random_uuid(), thumb, email, password)
returning unqid ;
END;
$BODY$
LANGUAGE plpgsql
VOLATILE
Run Code Online (Sandbox Code Playgroud)
如果插入成功,我试图让函数返回项目的 uuid。我这样称呼它;
select * from create_user('newuser@mail.com', 'passpopcorn', 'thumbelinaurl');
Run Code Online (Sandbox Code Playgroud)
收到此错误;
SQL Error [42601]: ERROR: query has no destination for result data
Where: PL/pgSQL function create_user(email,text,text) line 3 at SQL statement
Run Code Online (Sandbox Code Playgroud)
从我的谷歌搜索看来,当查询中没有 return 语句时,似乎会出现此错误。但在我的插入查询中,我确实有一个返回语句。
额外问题;对于简单的插入语句(例如这里的这个语句,或者带有几个选择后跟一个插入的语句),函数或过程会是更好的选择吗?