Re: PL/pgSQL 'i = i + 1' Syntax

From: David Wheeler <david(at)kineticode(dot)com>
To: Mark Dilger <pgsql(at)markdilger(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/pgSQL 'i = i + 1' Syntax
Date: 2006-05-16 23:48:12
Message-ID: 6570E265-A68F-499E-9019-2F28DDC7464D@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On May 16, 2006, at 16:42, Mark Dilger wrote:

> So I don't know why it works for you. I wrote the following, and
> it also
> increments the variable:
>
> CREATE OR REPLACE FUNCTION weird () RETURNS SETOF INT AS $$
> DECLARE
> i integer;
> BEGIN
> i := 0;
> return next i;
> i = i + 1;
> return next i;
> i = i + 1;
> return next i;
> return;
> END;
> $$ LANGUAGE plpgsql;
>
> So I don't think it has anything to do with loop variables,
> specifically.

Indeed. It appears that, contrary to what I previously thought, :=
also works:

CREATE OR REPLACE FUNCTION inc_by_two(
upfrom int,
upto int
) RETURNS SETOF INT AS $$
BEGIN
FOR i IN upfrom..upto LOOP
RETURN NEXT i;
i := i + 1;
END LOOP;
END;
$$ LANGUAGE 'plpgsql';

try=# select * from inc_by_two(1,11);
inc_by_two
------------
1
3
5
7
9
11
(6 rows)

Best,

David

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Wheeler 2006-05-16 23:58:12 Re: PL/pgSQL 'i = i + 1' Syntax
Previous Message Mark Dilger 2006-05-16 23:42:45 Re: PL/pgSQL 'i = i + 1' Syntax