Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgres function syntax near at IF

I am writing a Postgres function as below

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$
;

I am getting syntax error near at IF, I am not able to find any problem here. Can someone please help me to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think it should be like

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading