MySQL触发器:检查变量是否存在时出错

时间:2013-02-07 10:59:31

标签: mysql triggers

我有一个触发器,它引用一个来自删除查询的全局变量来设置触发器:

DELIMITER ;;

CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW 
BEGIN

    IF (SELECT @userID IS NULL) 
    THEN @userID := 0
    END IF;

END
;;

DELIMITER ;

出于某种原因,我收到了SQL错误:

SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@userID := 0

我不明白这一点,这是一个非常基本的IF声明,为什么不起作用?

更新:我完全糊涂了,我试过这个

IF TRUE
THEN TRUE
END IF;

它仍然会引发错误......说真的?

更新2:已解决这实际上有效,但很奇怪

IF (SELECT @userID IS NULL) 
THEN SET userID = 0;
END IF;

显然你在IF语句中需要一个分号?

1 个答案:

答案 0 :(得分:1)

CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW 
BEGIN
  IF (SELECT @userID IS NULL) 
  THEN 
    SET @userID = 0;
END IF;
END;