Re: Function syntax ?

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Ruben Gouveia" <rubes7202(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Function syntax ?
Date: 2008-09-09 18:15:56
Message-ID: dcc563d10809091115j18f98e92g573fd403de255e80@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

That's not what I copied and pasted in. Leave out the
v_dt := p_dt;
> v_dt2 := p_dt2;
lines and turn the v into p in the rest of the function.

On Tue, Sep 9, 2008 at 12:11 PM, Ruben Gouveia <rubes7202(at)gmail(dot)com> wrote:
> When i tried that, i got the following error:
>
> create or replace function fcn_max_dt(p_dt timestamp without time zone,
> p_dt2 timestamp without time zone)
> returns timestamp without time zone as $$
>
> BEGIN
> v_dt := p_dt;
> v_dt2 := p_dt2;
>
> if v_dt >= v_dt2 then
> return v_dt;
> else
> return v_dt2;
> end if;
>
> END;
> $$ LANGUAGE 'plpgsql';
>
> ERROR: syntax error at or near "v_dt" at character 1
> QUERY: v_dt := $1
> CONTEXT: SQL statement in PL/PgSQL function "fcn_max_dt" near line 3
>
> ********** Error **********
>
> ERROR: syntax error at or near "v_dt"
> SQL state: 42601
> Context: SQL statement in PL/PgSQL function "fcn_max_dt" near line 3
>
>
>
>
>
>
>
>
> On Tue, Sep 9, 2008 at 11:07 AM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
> wrote:
>>
>> On Tue, Sep 9, 2008 at 11:55 AM, Ruben Gouveia <rubes7202(at)gmail(dot)com>
>> wrote:
>> > Does this syntax look correct? Can anyone think of a better way to write
>> > this?
>> >
>> > This function will accept two timestamp parameters and determine the
>> > highest
>> > of the two?
>> >
>> > create or replace function fcn_max_dt(p_dt timestamp without time zone,
>> > p_dt2 timestamp without time zone)
>> > returns timestamp without time zone as $$
>> > DECLARE
>> > v_dt timestamp without time zone;
>> > v_dt2 timestamp without time zone;
>> >
>> > BEGIN
>> > v_dt := p_dt;
>> > v_dt2 := p_dt2;
>> >
>> > if v_dt >= v_dt2 then
>> > return v_dt;
>> > else
>> > return v_dt2;
>> > end if;
>> >
>> > END;
>> > $$ LANGUAGE 'plpgsql';
>>
>> It certainly works, but there's no real need for the declarations.
>> This works just as well:
>>
>> create or replace function fcn_max_dt(p_dt timestamp without time zone,
>> p_dt2 timestamp without time zone)
>> returns timestamp without time zone as $$
>>
>> BEGIN
>> if p_dt >= p_dt2 then
>> return p_dt;
>> else
>> return p_dt2;
>> end if;
>>
>> END;
>> $$ LANGUAGE 'plpgsql';
>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ruben Gouveia 2008-09-09 18:16:02 Re: Function syntax ?
Previous Message Richard Huxton 2008-09-09 18:15:53 Re: Function syntax ?