Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create triggers with RAISE

I'm trying to limit a table to only one record and disable all attempt to add more. I have created this trigger: CREATE TRIGGER abort_insert_to_my_tbl BEFORE INSERT ON my_tbl BEGIN RAISE(ABORT,"You can't add records to my_tbl") END;

But I keep getting this error:

Error: near line 3080: near "RAISE": syntax error  

What am I doing wrong?

like image 357
user1908466 Avatar asked Mar 05 '14 14:03

user1908466


1 Answers

As the documentation shows, RAISE is a function, not a statement, so it cannot be used directly in the trigger body.

To use a function in a statement, use, for example, a SELECT statement:

CREATE TRIGGER abort_insert_to_my_tbl
BEFORE INSERT ON my_tbl
BEGIN
    SELECT RAISE(ABORT, 'You can''t add records to my_tbl');
END;
like image 125
CL. Avatar answered Oct 15 '22 00:10

CL.