Move/fetch cursor in loop (PostgreSQL) -
i need create trigger check if new inserted element's id isn't repeated. problem in loop statement, console spit out error:
context: sql statement in pl/pgsql function "foo" near line 7 line 1: move forward 1 $1
here function:
create function foo() returns trigger as' declare xyz cursor select id accounts; begin loop if new.id = xyz raise notice ''id used!''; else move forward 1 xyz; end if; end loop; return new; close xyz; end; ' language 'plpgsql'; create trigger foo before insert on accounts each row execute procedure foo();
your example has no sense (you can't compare scalar value cursor). cursor self pointer without value.
if new.id = xyx
why don't do
begin if exists(select * accounts a.id = new.id) raise notice ''id used''; -- better raise exception end if; return new; end;
second nonsense
return new; close xyz; -- no statement executed after return
Comments
Post a Comment