plpgsql: Is ELSE IF supported or not?

Поиск
Список
Период
Сортировка
Искать

plpgsql: Is ELSE IF supported or not?

От:
"Marko Kreen" <markokr@gmail.com>
Дата:
Docs seems to say it is, but following function fails to compile:

create function err_else() returns void as $$
begin   if 1 = 1 then   else if 1 = 2 then   end if;
end;
$$ language plpgsql;

ERROR:  syntax error at or near ";"
LINE 6: end;

Version 8.3.3.

-- 
marko

Re: plpgsql: Is ELSE IF supported or not?

От:
"Pavel Stehule" <pavel.stehule@gmail.com>
Дата:
hello

2008/6/26 Marko Kreen :
> Docs seems to say it is, but following function fails to compile:
>
> create function err_else() returns void as $$
> begin
>    if 1 = 1 then
>    else if 1 = 2 then
>    end if;
> end;
> $$ language plpgsql;
>
> ERROR:  syntax error at or near ";"
> LINE 6: end;

use elseif or elsif :)

Pavel

>
> Version 8.3.3.
>
> --
> marko
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>

Re: plpgsql: Is ELSE IF supported or not?

От:
"Marko Kreen" <markokr@gmail.com>
Дата:
On 6/26/08, Pavel Stehule  wrote:
>  2008/6/26 Marko Kreen :
> > Docs seems to say it is, but following function fails to compile:
>  >
>  > create function err_else() returns void as $$
>  > begin
>  >    if 1 = 1 then
>  >    else if 1 = 2 then
>  >    end if;
>  > end;
>  > $$ language plpgsql;
>  >
>  > ERROR:  syntax error at or near ";"
>  > LINE 6: end;
>
> use elseif or elsif :)

Yeah, I know.  Just the docs say this is one "form" of the IF statement:
 IF ... THEN ... ELSE IF

Although now that i read it more, the actual "form" is:
 ELSE    IF THEN    END IF END IF;

That is - the ELSE starts new block unconditionally and ignores any IF
that follows.  Later the IF can be part of new block as usual.  Huh.

This is confusing.  I suggest removing the "ELSE IF" as one of the "forms"
because it is not.

-- 
marko

Re: plpgsql: Is ELSE IF supported or not?

От:
"Pavel Stehule" <pavel.stehule@gmail.com>
Дата:
2008/6/26 Marko Kreen :
> On 6/26/08, Pavel Stehule  wrote:
>>  2008/6/26 Marko Kreen :
>> > Docs seems to say it is, but following function fails to compile:
>>  >
>>  > create function err_else() returns void as $$
>>  > begin
>>  >    if 1 = 1 then
>>  >    else if 1 = 2 then
>>  >    end if;
>>  > end;
>>  > $$ language plpgsql;
>>  >
>>  > ERROR:  syntax error at or near ";"
>>  > LINE 6: end;
>>
>> use elseif or elsif :)
>
> Yeah, I know.  Just the docs say this is one "form" of the IF statement:
>
>  IF ... THEN ... ELSE IF
>
> Although now that i read it more, the actual "form" is:
>
>  ELSE
>     IF THEN
>     END IF
>  END IF;
>
> That is - the ELSE starts new block unconditionally and ignores any IF
> that follows.  Later the IF can be part of new block as usual.  Huh.
>
> This is confusing.  I suggest removing the "ELSE IF" as one of the "forms"
> because it is not.
>

this is same in all procedural languages

Pavel

> --
> marko
>

Re: plpgsql: Is ELSE IF supported or not?

От:
Mark Mielke <mark@mark.mielke.cc>
Дата:
 Pavel Stehule wrote: 
2008/6/26 Marko Kreen <markokr@gmail.com>: 
Although now that i read it more, the actual "form" is:
ELSE   IF THEN   END IFEND IF;

That is - the ELSE starts new block unconditionally and ignores any IF
that follows.  Later the IF can be part of new block as usual.  Huh.

This is confusing.  I suggest removing the "ELSE IF" as one of the "forms"
because it is not.   
this is same in all procedural languages 

I don't agree with this statement. In "all procedural languages", or probably most, they usually make "ELSE IF" special, in that you don't need to close the block twice as per above. The ELSE IF is not actually special in PL/SQL, so it is not a special form. The "ELSE" can contain a block, which contain any statement, including a nested IF statement. Why not describe ELSE WHILE as well based upon the logic that ELSE IF is valid? :-)

Now, if it were to say "an alternative form of ELSEIF is to nest IF statement like so:" ...

Cheers,
mark

-- 
Mark Mielke <mark@mielke.cc>

Re: plpgsql: Is ELSE IF supported or not?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Mark Mielke  writes:
> I don't agree with this statement. In "all procedural languages", or 
> probably most, they usually make "ELSE IF" special, in that you don't 
> need to close the block twice as per above. The ELSE IF is not actually 
> special in PL/SQL, so it is not a special form. The "ELSE" can contain a 
> block, which contain any statement, including a nested IF statement. Why 
> not describe ELSE WHILE as well based upon the logic that ELSE IF is 
> valid? :-)

> Now, if it were to say "an alternative form of ELSEIF is to nest IF 
> statement like so:" ...

Yeah, that might be better.  I think the reason the text looks the way
it does is that we didn't have ELSEIF/ELSIF to start out with, and what
is now section 38.6.2.3 was originally an example of what you had to do
to work around that lack.  I agree that the current presentation is more
confusing than anything else.  ISTM documenting ELSEIF and ELSIF as
"separate forms" of IF is a bit over-the-top too.
		regards, tom lane

FAQ