I am facing a very strange problem with while loop insert on MySQL
Here's the background information of my test:
CREATE TABLE test_table (
`token` varchar(16) NOT NULL,
`val1` varchar(256) DEFAULT NULL,
PRIMARY KEY (`token`)
);
DELIMITER $$
CREATE PROCEDURE sp_test(
IN token VARCHAR(16),
IN token1 VARCHAR(16),
IN token2 VARCHAR(16),
IN token3 VARCHAR(16),
IN token4 VARCHAR(16),
IN val1 VARCHAR(256)
)
BEGIN
DECLARE tokenUsed VARCHAR(16);
DECLARE trial INT DEFAULT 0;
DECLARE tmpTok VARCHAR(16);
WHILE (tokenUsed IS NULL AND trial < 5)
DO
SET tmpTok = CASE trial WHEN 0 THEN token WHEN 1 THEN token1 WHEN 2 THEN token2
WHEN 3 THEN token3 WHEN 4 THEN token4 END;
IF NOT EXISTS(SELECT 1 FROM test_table WHERE token = tmpTok)
THEN
SET tokenUsed = tmpTok;
INSERT INTO test_table (token) VALUES(tmpTok);
END IF;
SET trial = trial + 1;
END WHILE;
IF tokenUsed IS NOT NULL
THEN
UPDATE test_table SET val1 = val1 WHERE token = tokenUsed;
END IF;
SELECT * FROM test_table WHERE token = tokenUsed;
END$$
DELIMITER ;
Then I would call the stored proc servral times like this
CALL sp_test ('test1', 'test2', 'test3', 'test4', 'test5', 'happy');
What I am trying to achieve
Here are the symptom
Error Code: 1062. Duplicate entry 'test2' for key 'PRIMARY'
The while loop did move to token test2 and inserted it to test_table but went no further, update also failed after test2. The stored proc is quite straight forward and I have no idea why this is happening. Any thoughts?
UPDATE test_table SET val1 = val1 WHERE token = tokenUsed;
Both val1
in it are local variable defined in SP parameters. The same about token
. Must be
UPDATE test_table SET test_table.val1 = val1 WHERE test_table.token = tokenUsed;
Also check your code for the same issue in another statements.