简体   繁体   中英

Mysql transaction commit and rollback example

Following mysql query is giving the following error.

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;

error :

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 

DECLARE can only be used between BEGIN and END statement which can only appear within a Stored Procedure, Function, Trigger, or Event definition.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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