0

I get the following error when I try to create this trigger in SQLite

create trigger timeslot_check1 after insert on section

for each row
when(new.time_slot_id not in(select time_slot_id
                            from time_slot))
begin
    rollback
end;

ERROR : near "rollback": syntax error:

2
  • Shouldn't this have a BEGIN TRANSACTION ? Or are you missing a semicolon after ROLLBACK ? Commented May 4, 2017 at 16:40
  • @RamMehta none of these worked Commented May 4, 2017 at 16:47

1 Answer 1

4

As shown in the documentation, the only SQL commands allowed in a trigger body are UPDATE, INSERT, DELETE, and SELECT.

To raise an error, you must use the RAISE() function from inside a query:

CREATE TRIGGER timeslot_check1
BEFORE INSERT ON section
FOR EACH ROW
WHEN NEW.time_slot_id NOT IN (SELECT time_slot_id FROM time_slot)
BEGIN
    SELECT RAISE(FAIL, "invalid timeslot");
END;

Anyway, this check can be done much easier with a foreign key.

Sign up to request clarification or add additional context in comments.

1 Comment

your code gives the ERROR : near "RAISE": syntax error:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.