MySQL存储过程INSERT问题

时间:2014-06-26 09:27:14

标签: mysql sql sql-server stored-procedures

以下情况适用:

CREATE TEMPORARY TABLE IF NOT EXISTS `smth_table` (
  `login` VARCHAR(20),
  `password` VARCHAR(20),
  `type` INT(11),
  `account_state` DECIMAL(12,4)
);

PREPARE Selection FROM
   "INSERT INTO `smth_table`
    (SELECT ta.`login`, ta.`password`, ta.`type`, ta.`account_state`
    FROM tableA ta
    INNER JOIN tableB tb ON tb.id_client = ta.id_client
    WHERE tb.id_lot = ? AND ta.`type` MOD 2 = 0
    AND ta.first_use = '0000-00-00 00:00:00'
    AND ta.account_state = 0
    LIMIT ?)";
SET @WHERE = var1;
SET @LIMIT = var2;
EXECUTE Selection USING @WHERE, @LIMIT;
DEALLOCATE PREPARE Selection;

DECLARE curs CURSOR FOR
  SELECT `password` FROM `smth_table`;

DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;


OPEN pin_curs;

get_pass: LOOP
  FETCH curs INTO pass;
  IF v_finished = 1 THEN
     LEAVE get_pass;
  END IF;

  UPDATE tableA ta INNER JOIN tableB tb
  ON tb.id_client = ta.id_client
  SET `type` = `type` | 1,
  `account_state` = `account_state` + 5
  WHERE tb.id_lot = var1
  AND `password` = pass;


END LOOP get_pass;

 CLOSE curs;

END

为什么在运行此存储过程时,临时表是否填充了超出限制的值?请记住,我将LIMIT设置为通过该过程传递的IN变量,顺便提一下,它是10。但是当我运行该程序时,它会在临时表中插入更多的100行,而且我不明白为什么,它应该只插入10行。

1 个答案:

答案 0 :(得分:1)

<强>解决!

问题转发的事实是我在重新创建表时没有删除表格,因此一遍又一遍地插入相同的值......

DROP TABLE IF EXISTS `smth_table`; 

在创建它之前插入,并且查询运行顺利: - )