I want to create a trigger that only inserts into table when the condition is met. I\'ve tried using various combinations of IF/BEGIN/END and WHERE, but Django returns me an SQ
@variables
are not DECLARED
.
Either:
DECLARE user_same BOOLEAN;
SELECT 1 INTO user_same WHERE ...
or
SELECT @user_same := 1 WHERE ...
Better yet, this avoids the need for the variable.
IF (EXISTS SELECT * FROM ...)
Also, don't use the construct IN ( SELECT ... )
; it is usually better to use WHERE EXISTS ( SELECT * FROM ... )
or JOIN ... ON ...
.
(There may be more problems after fixing those.)
Another easy way to do this.
You should add any symbols between ;
delimiters for the appropriate converting MySQL syntax to the raw SQL.
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF NEW.number <> 'anynumber' AND NEW.number <> 'anynumber'
THEN
SET NEW.number = 'anynumber'; --
END IF; --
END
This example works due to the dash symbols.