Mysql事务提交和回滚示例

时间:2015-01-26 13:16:42

标签: mysql

以下mysql查询给出以下错误。

QUERY -

    set @rollback = 0; 
    start transaction;    
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @rollback = 1;
    INSERT INTO `tablea` (`date`) VALUES (NOW());
    INSERT INTO `tableb` (`date`) VALUES (NOW());
    INSERT INTO `tablec` (`date`) VALUES (NOW());
    IF @rollback THEN
        ROLLBACK;
    ELSE
        COMMIT;
    END IF;

错误:

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 'DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @rollback = 1' at line 1 

1 个答案:

答案 0 :(得分:0)

DECLARE只能在BEGIN和END语句之间使用,它只能出现在存储过程,函数,触发器或事件定义中。

请参阅:http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-compound-statements.html