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

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

              http://www.postgresql.org/docs/faq

Reply via email to