MySQL创建触发器AFTER Update语法错误

时间:2015-01-09 00:31:48

标签: mysql

我正在处理我的第一个从头开始的应用程序,我正在尝试设置一个触发器,该触发器将在每次用户激活其帐户并将用户数据从PRE_REG表移动到USERS和PERSONAL TABLE(S)时执行。

我在机器上的SQL中使用它

CREATE TRIGGER new_active_user ON pre_reg FOR UPDATE
AS
DECLARE @current_user int 
DECLARE @banned int
DECLARE @user_type smallint
SET @user_type = 3
SET @banned = 0
SELECT @current_user = id FROM pre_reg WHERE active = 1
    BEGIN
        INSERT INTO users (id, username, password, banned, user_since) SELECT @current_user, username, password, @banned, date  FROM pre_reg WHERE active = 1
        INSERT INTO personal(id, email, user_type) SELECT @current_user, email, @user_type FROM pre_reg WHERE id = @current_user
        DELETE FROM pre_reg WHERE id = @current_user
    END
GO

我试图在MySQL语法中重新创建它,但它一直给我错误,我无法弄清楚:

CREATE TRIGGER new_active_user AFTER UPDATE ON `pre_reg`
FOR EACH ROW
BEGIN
DECLARE current_user INT;
DECLARE banned INT;
DECLARE user_type INT;
SET user_type = 3;
SET banned = 0;
SELECT current_user = `id` FROM `pre_reg` WHERE `active` = 1
INSERT INTO `users` (`id`, `username`, `password`,`banned`, `user_since`) SELECT current_user, `username`, `password`, banned, date  FROM `pre_reg` WHERE `active` = 1
INSERT INTO` personal` (`id`, `email`,`user_type`) SELECT current_user, `email`, `user_type` FROM `pre_reg` WHERE `id` = current_user
DELETE FROM `pre_reg` WHERE `id` = current_user
END;
GO

错误:

Error
SQL query:

CREATE TRIGGER new_active_user AFTER UPDATE ON  `pre_reg` FOR EACH ROW BEGIN DECLARE current_user INT;


MySQL said: Documentation

#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 'current_user INT' at line 4 

如果有人能帮助我,将不胜感激,谢谢! 即使有更好的方法,我愿意倾听并学习谢谢!

1 个答案:

答案 0 :(得分:0)

Delimiter //

CREATE TRIGGER new_active_user AFTER UPDATE ON `pre_reg`
FOR EACH ROW
BEGIN
    DECLARE currentUser INT;
    DECLARE banned INT;
    DECLARE user_type INT;
    SET @user_type = 3;
    SET @banned = 0;
    SET @currentUser := (select `id` FROM `pre_reg` WHERE `active` = 1);
    INSERT INTO `users` (`id`, `username`, `password`,`banned`, `user_since`) SELECT current_user, `username`, `password`, banned, date  FROM `pre_reg` WHERE `active` = 1;
    INSERT INTO` personal` (`id`, `email`,`user_type`) SELECT current_user, `email`, `user_type` FROM `pre_reg` WHERE `id` = current_user;
    DELETE FROM `pre_reg` WHERE `id` = current_user;
END//

Delimiter ;