我试图在存储过程中运行insert语句后返回一个字符串id。
这是我的存储过程代码:
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;
表架构:
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)
这是我尝试从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...
我有点困惑,因为我没有传入一个列而只是第一个参数的字符串id
。我做错了什么?
我做了下面提到的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.
由于
答案 0 :(得分:1)
您的代码中有几处错误:
'qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1'
代替"qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1"
。您的函数返回标量而不是集合。你不应该从FROM
条款中调用它。
SELECT create_quote('qw2e3r4t5y6u7i8o9i8u7y6t5r4e3w2q1', 'name', 'website', 'phone', 'email', 'msg', now()::timestamp, NULL, 0, now()::timestamp);
您的功能没有返回值。您可以将 UPSERT 的结果分配给变量并返回该变量。或者更容易返回输入参数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;