Adding transaction management to mysql's stored procedure statement does not take effect

The transaction of the stored procedure written as follows is not effective because START TRANSACTION; cannot be written before the DDL statement

CREATE DEFINER=`root`@`localhost` PROCEDURE `myProcedure`()
BEGIN
START TRANSACTION;
CREATE TABLE IF NOT EXISTS `aa`(
  `a` int(255) NOT NULL AUTO_INCREMENT,
  `b` varchar(255) DEFAULT NULL,
  `c` varchar(255) DEFAULT NULL,
  `ak` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`a`) USING BTREE,
  KEY `ak` (`ak`) USING BTREE COMMENT '外键'
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- 睡眠10秒
DELETE FROM aa WHERE a=2;
SELECT SLEEP(10)
COMMIT;
END

The transaction of the stored procedure written as follows is valid

CREATE DEFINER=`root`@`localhost` PROCEDURE `myProcedure`()
BEGIN
CREATE TABLE IF NOT EXISTS `aa`(
  `a` int(255) NOT NULL AUTO_INCREMENT,
  `b` varchar(255) DEFAULT NULL,
  `c` varchar(255) DEFAULT NULL,
  `ak` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`a`) USING BTREE,
  KEY `ak` (`ak`) USING BTREE COMMENT '外键'
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
START TRANSACTION;
DELETE FROM aa WHERE a=2;
-- 睡眠10秒
SELECT SLEEP(10)
COMMIT;
END

Guess you like

Origin blog.csdn.net/qq_19891197/article/details/132397983