[英]how to return string id from insert stored procedure in postgresql
I am trying to return a string id after the insert statement runs in a stored procedure. 我试图在存储过程中运行插入语句后返回一个字符串ID。
Here is my stored procedure code: 这是我的存储过程代码:
CREATE OR REPLACE FUNCTION create_quote(
p_id character varying(40),
p_name character varying(200),
p_website character varying(200),
p_phone character varying(100),
p_email character varying(200),
p_msg text,
p_created_on timestamp without time zone,
p_created_ip cidr,
p_contacted boolean,
p_contacted_on timestamp without time zone
)
RETURNS character varying(40) AS $body$
BEGIN
INSERT INTO quote VALUES (p_id, p_name, p_website, p_phone, p_email, p_msg, p_created_on, p_created_ip, p_contacted, p_contacted_on)
ON CONFLICT (id)
DO UPDATE SET name = p_name, website = p_website, phone = p_phone, email = p_email, msg = p_msg, created_on = p_created_on, created_ip = p_created_ip, contacted
= p_contacted, contacted_on = p_contacted_on RETURNING id;
END;
$body$ LANGUAGE plpgsql;
The table schema: 表架构:
Table "public.quote"
Column | Type | Modifiers
--------------+-----------------------------+--------------------------------------
id | character varying(40) | not null
name | character varying(200) | not null
website | character varying(200) | not null
phone | character varying(100) | not null
email | character varying(200) | not null
msg | text |
created_on | timestamp without time zone | default timezone('utc'::text, now())
created_ip | cidr |
contacted | boolean |
contacted_on | timestamp without time zone |
Indexes:
"quote_pkey" PRIMARY KEY, btree (id)
"quote_email_key" UNIQUE CONSTRAINT, btree (email)
Here is the error I get when I try to call the procedure from the psql client. 这是我尝试从psql客户端调用过程时遇到的错误。
select * from create_quote("qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1", "name", "website", "phone", "email", "msg", now()::timestamp, NULL, 0, now()::timestamp);
ERROR: column "qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1" does not exist
LINE 1: select * from create_quote("qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q...
I am a bit confused as I am not passing in a column and just a string id
for the first parameter. 我有点困惑,因为我没有传递列,而只是第一个参数的字符串id
。 What am I doing wrong? 我究竟做错了什么?
I made the changes that clemens mentioned below but now i am getting this error: 我做了下面clemens提到的更改,但是现在出现了此错误:
select create_quote('qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1', 'name', 'website', 'phone', 'email', 'msg', now()::timestamp, NULL, 0, now()::timestamp);
ERROR: function create_quote(unknown, unknown, unknown, unknown, unknown, unknown, timestamp without time zone, unknown, integer, timestamp without time zone) does not exist
LINE 1: select create_quote('qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1', 'na...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Thanks 谢谢
You have several errors in your code: 您的代码中有几个错误:
'qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1'
instead of "qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1"
for instance. 例如,使用'qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1'
代替"qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1"
。 Your function is returning a scalar and not a set. 您的函数返回的是标量而不是集合。 You shouldn't call it from the FROM
clause. 您不应该从FROM
子句中调用它。
SELECT create_quote('qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1', 'name', 'website', 'phone', 'email', 'msg', now()::timestamp, NULL, 0, now()::timestamp);
Your function doesn't return a value. 您的函数没有返回值。 You can assign the result from the UPSERT to a variable and returning this. 您可以将结果从UPSERT分配给变量,然后将其返回。 Or much easier returning the input parameter p_id
, because your UPSERT will always return this value. 或者更容易返回输入参数p_id
,因为您的UPSERT将始终返回该值。
CREATE OR REPLACE FUNCTION create_quote( p_id character varying(40), p_name character varying(200), p_website character varying(200), p_phone character varying(100), p_email character varying(200), p_msg text, p_created_on timestamp without time zone, p_created_ip cidr, p_contacted boolean, p_contacted_on timestamp without time zone ) RETURNS character varying(40) AS $body$ BEGIN INSERT INTO quote VALUES (p_id, p_name, p_website, p_phone, p_email, p_msg, p_created_on, p_created_ip, p_contacted, p_contacted_on) ON CONFLICT (id) DO UPDATE SET name = p_name, website = p_website, phone = p_phone, email = p_email, msg = p_msg, created_on = p_created_on, created_ip = p_created_ip, contacted = p_contacted, contacted_on = p_contacted_on; RETURN p_id; END; $body$ LANGUAGE plpgsql;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.