Ares Draguna
Ares Draguna

Reputation: 1661

MySQL stored procedure INSERT issue

The following scenario applies:

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

Why, when I run this stored procedure, does the temp table populates with more then the limit? Keep in mind that I set the LIMIT with an IN variable passed through the procedure, and it's 10, incidentally. But when I run the procedure it inserts in the temp table more the 100 rows, and I don't understand why, when it should insert only 10.

Upvotes: 0

Views: 65

Answers (1)

Ares Draguna
Ares Draguna

Reputation: 1661

SOLVED!

The issue relayed on the fact that I was not deleting the table upon creating it again, thus inserting same values over and over again...

DROP TABLE IF EXISTS `smth_table`; 

this inserted before creating it and the query's run smooth :-)

Upvotes: 1

Related Questions