簡體   English   中英

如何在Postgres函數中使用WITH?

[英]How to use WITH in a Postgres function?

在函數中如何用as編寫? 我想編寫一個遞歸函數來計算汽車的重量。 它給了我這樣的錯誤:錯誤:“ 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;

嘗試用括號將查詢括起來。

您還缺少END IF; ELSE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM