Postgres 函数语法 near at IF

Postgres function syntax near at IF

提问人:Niranjan 提问时间:5/19/2023 最后编辑:marc_sNiranjan 更新时间:5/19/2023 访问量:17

问:

我正在编写一个Postgres函数,如下所示

CREATE OR REPLACE FUNCTION dbo.get_managing_owner(p_imo integer, p_charter_id integer)
RETURNS TABLE(managing_owner character varying) 
LANGUAGE plpgsql
AS $function$
BEGIN
 RETURN QUERY
IF p_charter_id = 0
THEN
    SELECT managing_owner
    from dbo.vessel
    where imo=p_imo
    union
    SELECT managing_owner
    from dbo.charter c
    where imo=p_imo;
ELSE 
    SELECT managing_owner
    from dbo.vessel v
    inner join dbo.charter c2
    on v.imo =c2.imo
    where c2.id =p_charter_id
    union
    SELECT managing_owner
    from dbo.charter c
    where id=p_charter_id;
END IF;
end;
$function$
;

我在 IF 附近遇到语法错误,我在这里找不到任何问题。有人可以帮我解决这个问题吗?

PostgreSQL 函数

评论


答:

0赞 Shakirov Ramil 5/19/2023 #1

我认为它应该是这样的

BEGIN
 
IF p_charter_id = 0
THEN
    RETURN QUERY SELECT managing_owner
    from dbo.vessel
    where imo=p_imo
    union
    SELECT managing_owner
    from dbo.charter c
    where imo=p_imo;
ELSE 
    RETURN QUERY SELECT managing_owner
    from dbo.vessel v
    inner join dbo.charter c2
    on v.imo =c2.imo
    where c2.id =p_charter_id
    union
    SELECT managing_owner
    from dbo.charter c
    where id=p_charter_id;
END IF;