简体   繁体   English

MySQL需要帮助设置触发器

[英]MySQL need help setting up trigger

I am trying to set up a TRIGGER to clear empty strings before INSERT . 我试图在INSERT之前设置一个TRIGGER来清除空字符串。 Not rocket science but I can't find the error in my syntax! 不是火箭科学,但我在语法中找不到错误!

Here is the TRIGGER itself 这是TRIGGER本身

USE `ga_abtest_logging`;
DELIMITER $$
CREATE TRIGGER avoid_empty
    BEFORE INSERT ON daily_analytics
        FOR EACH ROW
        BEGIN
            IF profileID = '' 
                THEN SET profileID = NULL
            END IF;
        END$$

Here is what workbench is showing: 这是工作台显示的内容:

DELIMITER

On hover over the END IF it reads syntax error, unexpected END, expecting ';' 悬停在END IF它会读取syntax error, unexpected END, expecting ';'

Could I have a problem with the settings on my DB? 我的数据库设置有问题吗? I have gone through the MySQL docs and I think the trigger looks right! 我已经浏览了MySQL文档,我认为触发器看起来正确! Does anyone see anything obviously wrong? 有没有人看到任何明显的错误?

You should make a few changes: 你应该做一些改变:

  • Use the NEW. 使用NEW. prefix when referencing a column value 引用列值时的前缀
  • Add a semi-colon at the end of the line where you set the value 在您设置值的行的末尾添加分号

For example: 例如:

DELIMITER $$

CREATE TRIGGER tr_b_ins_daily_analytics BEFORE INSERT ON daily_analytics FOR EACH ROW BEGIN
  IF (NEW.profileID = '')
  THEN
    SET NEW.profileID = NULL;
  END IF;
END $$

DELIMITER ;

The code below should work. 下面的代码应该有效。

DELIMITER $$  
CREATE TRIGGER avoid_empty  
    BEFORE INSERT ON daily_analytics  
        FOR EACH ROW  
        BEGIN  
            IF NEW.profileID = ''  
                THEN SET NEW.profileID = NULL;  
            END IF;  
        END;$$  
DELIMITER ;  

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

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