1

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

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 语句中使用分号?

4

1 回答 1

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;
于 2013-02-07T11:12:17.500 回答