我是InnoDB的新手,从交易开始。我一直在24小时内努力使其正常工作。
我正在创建一个交换站点,确实需要进行交易。首先,进行选择并查找一些数据,然后根据给出的结果进行一些更新和插入。
我不会发布完整的查询,因为它可能很难阅读,因此我创建了一个新查询以指出令人困扰的问题。
表日志
CREATE TABLE `log` (
`num_rows` int(10) unsigned NOT NULL,
`new_value` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
储存程序
DROP PROCEDURE IF EXISTS `test`//
CREATE PROCEDURE `test` (IN var1 BIGINT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE result INT;
DECLARE num_rows INT;
DECLARE cur1 CURSOR FOR
SELECT @var1 := @var1 +1 AS result;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
START TRANSACTION;
OPEN cur1;
SELECT FOUND_ROWS() into num_rows;
INSERT INTO log (num_rows,new_value) VALUES (num_rows,var1);
read_loop:
LOOP
FETCH cur1 INTO result;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
CLOSE cur1;
COMMIT;
END//
当我尝试
CALL test(1);
我传递1作为var1参数。因此,在cur1中,该值应增加。然后,将具有新值的新行插入到日志中。看起来:=分配不起作用。
我真的变了
SELECT @var1 := @var1 +1 AS result;
为了这
SELECT var1 := var1 +1 AS result;
并在“:= var1 +1”上得到错误
最佳答案
我知道问题的代码是存储过程的实际代码的抽象,因此不太了解您需要做什么,但是,这样的代码可能会有所帮助。
/* Procedure structure for procedure `test` */
/*!50003 DROP PROCEDURE IF EXISTS `test` */;
DELIMITER $$
CREATE PROCEDURE `test`(IN `var1` BIGINT)
BEGIN
DECLARE `done` TINYINT(1) DEFAULT 0;
DECLARE `result` BIGINT;
DECLARE `_num_rows` INT;
DECLARE `cur1` CURSOR FOR
SELECT SQL_CALC_FOUND_ROWS @`var1` := `var1` + 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done` := 1;
START TRANSACTION;
OPEN `cur1`;
SET `var1` := @`var1`;
SELECT FOUND_ROWS() INTO `_num_rows`;
INSERT INTO `log` (`num_rows`, `new_value`) VALUES (`_num_rows`, `var1`);
`read_loop`: LOOP
FETCH `cur1` INTO `result`;
IF (`done`) THEN
LEAVE `read_loop`;
END IF;
END LOOP `read_loop`;
CLOSE `cur1`;
COMMIT;
END$$
DELIMITER ;
重要的是要指出9.4. User-Defined Variables和常规参数13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax之间的区别是不同的变量。
SQL Fiddle demo