[英]How to use WITH in a Postgres function?
How to write with as in the function? 在函数中如何用as编写? I want to write a recursive function to calculate the weight of a car. 我想编写一个递归函数来计算汽车的重量。 It gave me error like : ERROR: syntax error at or near "WITH" 它给了我这样的错误:错误:“ WITH”或附近的语法错误
create or replace function aggregateWeight(p integer)
returns int as
$$
begin
if p in (select p.pid
from parts p) then
return (select p.weight
from parts p)
else
return
WITH RECURSIVE included_parts(pid, sid, quantity) AS (
SELECT ps.pid, ps.sid, ps.quantity
FROM partSubPart ps
UNION ALL
SELECT pr.pid, ps.sid, pr.quantity*ps.quantity
FROM included_parts pr, partSubPart ps
WHERE ps.pid = pr.sid
)
SELECT sum(pr.quantity*p.weight)
FROM included_parts pr, parts p
where pr.sid in (select p.pid from parts p)
GROUP BY pid;
end;
$$ language sql;
Try to surround the query with parentheses. 尝试用括号将查询括起来。
You are also missing an END IF;
您还缺少END IF;
after the ELSE
. 在ELSE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.