触发器中的语法错误

发布于 2024-11-03 07:49:41 字数 1019 浏览 0 评论 0原文

我在创建 MySQL (5.1) 触发器时遇到了麻烦,我真的不明白我在这里做错了什么。这是代码:

DELIMITER //
CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
    -- Uppercase the first letter
    SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));

    -- If price isn't round
    IF NEW.price != TRUNCATE(NEW.price) THEN
        IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN
            -- Sets it to xx.49
            SET NEW.price = TRUNCATE(NEW.price) + 0.49;
        ELSIF (NEW.price - TRUNCATE(NEW.price)) <= 0.99 THEN
            -- Sets it to xx.99
            SET NEW.price = TRUNCATE(NEW.price) + 0.99;
        END IF;
    END IF;
END;
//
DELIMITER ;

以及 MySQL 向我抛出的内容:

ERROR 1064 (42000): 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 ') THEN
        IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN    
    ' at line 7

非常感谢您查看它!

I'm having trouble when creating a MySQL (5.1) Trigger and I really don't see what I'm doing wrong here. Here's the code :

DELIMITER //
CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
    -- Uppercase the first letter
    SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));

    -- If price isn't round
    IF NEW.price != TRUNCATE(NEW.price) THEN
        IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN
            -- Sets it to xx.49
            SET NEW.price = TRUNCATE(NEW.price) + 0.49;
        ELSIF (NEW.price - TRUNCATE(NEW.price)) <= 0.99 THEN
            -- Sets it to xx.99
            SET NEW.price = TRUNCATE(NEW.price) + 0.99;
        END IF;
    END IF;
END;
//
DELIMITER ;

and what MySQL throws at my face :

ERROR 1064 (42000): 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 ') THEN
        IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN    
    ' at line 7

Thanks a lot for looking at it !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-11-10 07:49:41
DELIMITER //
    CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
    FOR EACH ROW BEGIN
        -- Uppercase the first letter
        SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));

        -- If price isn't round
        IF NEW.price != TRUNCATE(NEW.price,2) THEN
            IF NEW.price - TRUNCATE(NEW.price,2) <= 0.49 THEN
                -- Sets it to xx.49
                SET NEW.price = TRUNCATE(NEW.price,2) + 0.49;
            ELSIF (NEW.price - TRUNCATE(NEW.price,2)) <= 0.99 THEN
                -- Sets it to xx.99
                SET NEW.price = TRUNCATE(NEW.price,2) + 0.99;
            END IF;
        END IF;
    END;
    //

DELIMITER ;

截断函数使用两个参数!

DELIMITER //
    CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
    FOR EACH ROW BEGIN
        -- Uppercase the first letter
        SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));

        -- If price isn't round
        IF NEW.price != TRUNCATE(NEW.price,2) THEN
            IF NEW.price - TRUNCATE(NEW.price,2) <= 0.49 THEN
                -- Sets it to xx.49
                SET NEW.price = TRUNCATE(NEW.price,2) + 0.49;
            ELSIF (NEW.price - TRUNCATE(NEW.price,2)) <= 0.99 THEN
                -- Sets it to xx.99
                SET NEW.price = TRUNCATE(NEW.price,2) + 0.99;
            END IF;
        END IF;
    END;
    //

DELIMITER ;

truncate function use two parameters!

指尖凝香 2024-11-10 07:49:41
DELIMITER # 

CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
    SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));
    IF NEW.price != TRUNCATE(NEW.price) THEN
      ...
    END IF;
END # -- end of create trigger

DELIMITER ;

编辑 - 差异:

...
END; <----
# -- end of create trigger


DELIMITER ;
DELIMITER # 

CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
    SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));
    IF NEW.price != TRUNCATE(NEW.price) THEN
      ...
    END IF;
END # -- end of create trigger

DELIMITER ;

EDIT - the difference:

...
END; <----
# -- end of create trigger


DELIMITER ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文