代码之家  ›  专栏  ›  技术社区  ›  Matt Sidesinger

MySQL语法错误

  •  0
  • Matt Sidesinger  · 技术社区  · 17 年前

    执行时:

    BEGIN
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
            ROLLBACK;
            SELECT 1;
        END;
    
        DECLARE EXIT HANDLER FOR SQLWARNING
        BEGIN
            ROLLBACK;
            SELECT 1;
        END;
    
        -- delete all users in the main profile table that are in the MaineU18 by email address
        DELETE FROM ap_form_1 WHERE element_5 IN (SELECT email FROM MaineU18);
    
        -- delete all users from the MaineU18 table
        DELETE from MaineU18;
    
        COMMIT;
    END;
    

    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 'e1:
                    DECLARE EXIT HANDLER FOR SQLEXCEPTION
                    BEGIN
                            ROLLBACK' at line 2
    

    有什么想法吗?谢谢。

    更新2:

    I have tried putting the script into a PROCEDURE:
    
    DELIMITER |
    DROP PROCEDURE IF EXISTS temp_clapro|
    CREATE PROCEDURE temp_clapro()
    BEGIN
        DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING ROLLBACK;
    
        SET AUTOCOMMIT=0;
    
        -- delete all users in the main profile table that are in the MaineU18 by email address
        DELETE FROM ap_form_1 WHERE element_5 IN (SELECT email FROM MaineU18);
    
        -- delete all users from the MaineU18 table
        DELETE from MaineU18;
    
        COMMIT;
        SET AUTOCOMMIT=1;
    END
    |
    DELIMITER ;
    CALL temp_clapro();
    

    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (2.40 sec)
    
    Query OK, 0 rows affected (2.40 sec)
    
    Query OK, 0 rows affected (2.40 sec)
    
    Query OK, 0 rows affected (2.40 sec)
    
    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 'END;
    |
    DELIMITER ;
    CALL temp_clapro()' at line 1
    

    更新3:

    2 回复  |  直到 17 年前
        1
  •  2
  •   Bill Karwin    17 年前

    你好像在用 BEGIN 就像在Oracle中一样,就像打开一个特殊语句块一样。

    DECLARE 仅在存储过程、存储函数或触发器的主体中。

    http://dev.mysql.com/doc/refman/5.1/en/declare.html

    宣布 仅允许在 BEGIN ... END 声明。

    http://dev.mysql.com/doc/refman/5.1/en/begin-end.html :

    开始。。。结束语法用于 出现在存储程序中。


    回复你的评论和更新的问题:我不知道为什么会失败。我自己试过了,效果很好。我在我的Macbook上使用MySQL 5.0.75。你在用什么版本的MySQL?