Re: create function problem - Mailing list pgsql-sql

From Pavel Stehule
Subject Re: create function problem
Date
Msg-id AANLkTikSn8RNVuMrwajxThtNk6c5UyHkYw4hXWr=tLC9@mail.gmail.com
Whole thread Raw
In response to create function problem  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
Responses Re: create function problem
List pgsql-sql
Hello

you badly use a IF statement. It's not C. Every IF must to finish with END IF

this is

IF .. THEN
ELSEIF .. THEN ..
ELSE
END IF

Regards

Pavel Stehule

2010/12/30 Gary Stainburn <gary.stainburn@ringways.co.uk>:
> Hi folks,
>
> I'm writing my first plpsql function in ages and I'm going blind trying to see
> why it won't create. The error message and the code are below. I'm guessing
> it's something blindingly obvious, but can someone please point it out to me.
>
> ta
>
> Gary
>
> The actual line number is the LANGUAGE line at the end of the file.
>
> goole=# \i project_delivery_date.sql
> psql:project_delivery_date.sql:42: ERROR:  syntax error at or near ";"
> LINE 37: END;
>            ^
> goole=# \q
> [root@stan t-cards]# cat project_delivery_date.sql
> -- vim: ft=sql et ai ic
> --
> -- project_delivery_date() - project delivery date from existing dates
>
> CREATE FUNCTION project_delivery_date(date,date,date,date) RETURNS date AS $$
> DECLARE
>  eta_dealer ALIAS FOR $1;
>  eta_customer ALIAS FOR $2;
>  req_date ALIAS FOR $3;
>  act_date ALIAS FOR $4;
>  eta date;
>
> BEGIN
>  IF act_date IS NOT NULL THEN
>    return act_date;
>  END IF;
>  IF eta_dealer IS NOT NULL AND eta_customer IS NULL THEN
>    eta := eta_dealer;
>  ELSE IF eta_dealer IS NULL AND eta_customer IS NOT NULL THEN
>    eta := eta_customer;
>  ELSE IF eta_dealer IS NULL AND eta_customer IS NULL THEN
>    eta := NULL;
>  ELSE IF eta_dealer > eta_customer THEN
>    eta := eta_dealer;
>  ELSE
>    eta := eta_customer;
>  END IF;
>  IF eta IS NOT NULL AND req_date IS NULL THEN
>    RETURN eta;
>  END IF;
>  IF eta IS NULL AND req_date IS NOT NULL THEN
>    RETURN req_date;
>  END IF;
>  IF eta IS NULL AND req_date IS NULL THEN
>    RETURN NULL;
>  END IF;
>  IF eta > req_date THEN
>    RETURN eta;
>  END IF;
>  RETURN req_date;
> END;
> $$ LANGUAGE 'plpgsql';
>
> [root@stan t-cards]#
>
> --
> Gary Stainburn
> I.T. Manager
> Ringways Garages
> http://www.ringways.co.uk
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>


pgsql-sql by date:

Previous
From: Gary Stainburn
Date:
Subject: create function problem
Next
From: Adrian Klaver
Date:
Subject: Re: create function problem