I have a weird problem with a trigger in HSQLDB. I’ve checked the syntax several times but I haven’t found any mistakes. I now think that it may be impossible to create a subquery inside a trigger.
This is the code:
create trigger activate_member after update on member
REFERENCING OLD AS old NEW AS new
for each row
begin atomic
IF new.active = FALSE AND old.active = TRUE then
insert into member_inactive
(
member,
since,
until
)
values (
new.id,
(select since from member_active where
member = new.id AND since = (select max(since) from member_active where
member = new.id group by member)),
curdate()
);
ELSEIF new.active = TRUE AND old.active = FALSE then
insert into member_active (member, since) values
(new.id, curdate());
end if;
end;
I think I’ve made everything correct: I have semicolons on every query inside the IF and ELSIEF statements, and trailing semicolon after end if.
But I still get this error message:
SEVERE SQL Error at 'database.sql' line 125:
"create trigger activate_member after update on member
REFERENCING OLD AS old NEW AS new
for each row
begin atomic
IF new.active = FALSE AND old.active = TRUE then
insert into member_inactive
(
member,
since,
until
)
values (
new.id,
(select since from member_active where member = new.id AND since = (select max(since) from member_active where member = new.id group by member)),
curdate()
)"
unexpected end of statement: required: ; : line: 17
org.hsqldb.cmdline.SqlTool$SqlToolException
But where I should add a semicolon? I think the problem is the subquery inside the insert, but that makes no sense.
Probably the begin atomic is problematic. But I see no other way to have an if-else statement inside a trigger without it!
i found now whats caused this problem: hsqldb only has
begin atomicblocks inside a trigger.Inside a
begin atomicblock i cannot useinsertstatements…i now changed it to two triggers with
where (statement)clauses, this works also