plpgsql trigger parse error

From: Roland Roberts <roland(at)astrofoto(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: plpgsql trigger parse error
Date: 2002-09-11 14:46:11
Message-ID: m21y8037po.fsf@kuiper.rlent.pnet
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am having trouble getting a pl/PgSQL function to parse correctly and
my usual divide and conquer strategy is not working very well. The
full function appears at the bottom. When I simplified it to only the
first if (...) clause, it works. When I added the first else if (...)
clause, as shown immediately below, it fails.

create function deepsky_nodups() returns opaque as '
begin
if (NEW.suffix is NULL and NEW.component is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and suffix is null and component is null)) then
return NULL;
end if;
else if (NEW.component is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and suffix = NEW.suffix and component is null)) then
return NULL;
end if
end if;
return NEW;
end;
' language 'plpgsql';

create trigger ds_nodups before insert on deepsky
for each row execute procedure deepsky_nodups();

When it fires via the following insert

insert into deepsky (catalog, entry) values ('NGC', '1');

I get this error

NOTICE: plpgsql: ERROR during compile of deepsky_nodups near line 6
ERROR: parse error at or near "else"

I've stared at this so long I've run out of ideas on what might be
wrong. I started by cutting it down to the first if (...) and exists
clause. That works fine, i.e.,

create function deepsky_nodups() returns opaque as '
begin
if (NEW.suffix is NULL and NEW.component is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and suffix is null and component is null)) then
return NULL;
end if;
end if;
return NEW;
end;
' language 'plpgsql';

is fine. The full function gives me a different error,

NOTICE: plpgsql: ERROR during compile of deepsky_nodups near line 20
ERROR: parse error at or near ";"

Here is the full function:

create function deepsky_nodups() returns opaque as '
begin
if (NEW.suffix is NULL and NEW.component is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and suffix is null and component is null)) then
return NULL;
end if;
else if (NEW.component is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and suffix = NEW.suffix and component is null)) then
return NULL;
end if;
else if (NEW.suffix is NULL) then
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and component = NEW.component and suffix is null)) then
return NULL;
end if;
else
if (exists (select id from deepsky where catalog = NEW.catalog and entry = NEW.entry and component = NEW.component and suffix = NEW.suffix)) then
return NULL;
end if;
end if;
return NEW;
end;
' language 'plpgsql';

What am I missing?

roland
--
PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD RL Enterprises
roland(at)rlenter(dot)com 76-15 113th Street, Apt 3B
roland(at)astrofoto(dot)org Forest Hills, NY 11375

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Roland Roberts 2002-09-11 14:49:28 Re: Mailing lists archive search
Previous Message Douglas Blood 2002-09-11 14:35:12 parsing fields