sql-info.de

3. PL/PgSQL

3.1. ELSEIF

Affects: PostgreSQL <= 7.4

The keyword ELSEIF appears to be valid syntax. However it does not seem to be acted upon, as demonstrated by this function:

CREATE OR REPLACE FUNCTION iftest(INT)
  RETURNS VARCHAR
  LANGUAGE 'plpgsql'
  AS 
'
  DECLARE
    nr ALIAS FOR $1;
  BEGIN
    IF nr < 0
      THEN RETURN ''subzero'';
    ELSEIF nr >= 0
      THEN RETURN ''zero or larger'';
    ELSE
      RETURN ''something strange has happened'';
    END IF;
  END;
';

Logically this should report whether the parameter is less than zero or not. However:

test=> SELECT iftest(1);
             iftest
--------------------------------
 something strange has happened
(1 row)

The correct keyword is ELSIF (note the missing E). The relevant documentation is at: http://www.postgresql.org/docs/7.4/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS.

See these threads: http://archives.postgresql.org/pgsql-bugs/2004-11/msg00297.php and http://archives.postgresql.org/pgsql-general/2004-12/msg00789.php for some background on why ELSEIF doesn't work.

This problem has been fixed in PostgreSQL 8.0.