简体   繁体   English

SQL触发器创建-如何检查NULL或为空?

[英]SQL Trigger Creation - How do I check if NULL or empty?

I'm having some issues with my SQL syntax it seems. 我的SQL语法似乎有问题。 I'm trying to create a trigger and then check if data is NULL or empty. 我正在尝试创建一个触发器,然后检查数据是否为NULL或为空。

This is the code now thanks to Bill & Gordon: 感谢Bill&Gordon,现在这是代码:

DROP TRIGGER IF EXISTS `tablename_OnInsert`;

    DELIMITER $$

    CREATE TRIGGER `tablename_OnInsert` BEFORE INSERT ON `users`
    FOR EACH ROW
    BEGIN
        IF NEW.`swid` IS NULL OR NEW.`swid` = '' THEN
            SET NEW.`swid` = CONCAT('{', uuid(), '}');
        END IF
    END$$
    ;

The server still responds with an 1064: 服务器仍然响应1064:

/* 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 'END' at line 7 */

I've been looking around to see what I am doing wrong here, but I just don't get it. 我一直在四处看看我在做什么错,但我只是不明白。

Well: 好:

DROP TRIGGER IF EXISTS `tablename_OnInsert`;

DELIMITER $$

CREATE TRIGGER `tablename_OnInsert` BEFORE INSERT ON `users`
FOR EACH ROW
BEGIN
    IF NEW.`swid` IS NULL OR NEW.`swid` = '' THEN
        SET NEW.`swid` = CONCAT('{', uuid(), '}');
    END IF;
END$$

Your problem appears to be the extra ) in the IF . 您的问题似乎是IF的额外问题) However, I recommend BEGIN / END and setting the delimiter. 但是,我建议BEGIN / END并设置定界符。

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

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