Re: PL/pgSQL 'i = i + 1' Syntax - Mailing list pgsql-hackers

From Mark Dilger
Subject Re: PL/pgSQL 'i = i + 1' Syntax
Date
Msg-id 446A6375.1060401@markdilger.com
Whole thread Raw
In response to Re: PL/pgSQL 'i = i + 1' Syntax  (Mark Dilger <pgsql@markdilger.com>)
Responses Re: PL/pgSQL 'i = i + 1' Syntax
List pgsql-hackers
Mark Dilger wrote:
> David Wheeler wrote:
> 
>>Hellow PostgreSQL hackers,
>>
>>Quick question. Why does the 'i = i + 1' syntax work in this PL/pgSQL 
>>function?
>>
>>try=# CREATE OR REPLACE FUNCTION inc_by_two(
>>try(#    upfrom int,
>>try(#    upto   int
>>try(# ) RETURNS SETOF INT AS $$
>>try$# BEGIN
>>try$#     FOR i IN upfrom..upto LOOP
>>try$#         RETURN NEXT i;
>>try$#         i = i + 1;
>>try$#     END LOOP;
>>try$# END;
>>try$# $$ LANGUAGE 'plpgsql';
>>CREATE FUNCTION
>>try=# select * from inc_by_two(1,10);
>>inc_by_two
>>------------
>>          1
>>          3
>>          5
>>          7
>>          9
>>(5 rows)
>>
>>Someone posted a PL/pgSQL function in my blog with this syntax, which 
>>is how I know about it, but I couldn't find it documented anywhere.  Is
>>it a special exception for loop variables, perhaps?
>>
>>Thanks,
>>
>>David
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 2: Don't 'kill -9' the postmaster
>>
> 
> 
> The syntax for assignment is:
> 
>   i := i + 1
> 
> what you are doing is merely comparison.  Since you are not using the results of
> the comparison, it is a no-op.
> 
> mark

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.

mark


pgsql-hackers by date:

Previous
From: David Wheeler
Date:
Subject: Re: PL/pgSQL 'i = i + 1' Syntax
Next
From: David Wheeler
Date:
Subject: Re: PL/pgSQL 'i = i + 1' Syntax