简体   繁体   English

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

[英]MySQL create trigger AFTER Update syntax error

I am working on my first from scratch application, I am trying to set up a trigger that will execute every time a user activates their account and move user data from PRE_REG table to USERS and PERSONAL TABLE(S). 我正在开发我的第一个从头开始的应用程序,我试图设置一个触发器,该触发器将在用户每次激活其帐户并将用户数据从PRE_REG表移至USERS和PERSONAL TABLE(S)时执行。

I got this to work in SQL on my machine : 我让它可以在我的机器上的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

I have attempted to recreate it in MySQL syntax however it keep giving me errors and I can't figure out out: 我试图用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 : 错误:

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 

If anyone can help me it will be greatly appreciated thank you! 如果有人可以帮助我,将非常感谢您! Even if there is a better way to do this I am willing to listen and learn thank you! 即使有更好的方法,我也愿意倾听并学习谢谢!

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM