I have a table with following prototype:
CREATE TABLE `T2` (
`ID` int(11) DEFAULT NULL,
`NAME` varchar(100) DEFAULT NULL
)
I want to create procedure to insert random strings with a specific length and I wrote the following:
DELIMITER ;;
CREATE PROCEDURE STRGEN ( IN ITER INT) BEGIN
DECLARE COUNTER INT DEFAULT 0;
DECLARE _STR VARCHAR(1000) DEFAULT NULL;
WHILE COUNTER < @ITER DO
SET _STR = CONCAT(_STR , CHAR(RAND()*100+65));
SET COUNTER = COUNTER + 1;
END WHILE;
INSERT INTO T2 VALUES(RAND()*100 , _STR);
END;;
And called it this way:
CALL STRGEN(100);
The problem is that it inserts NULL as the NAME field.
My suspicion is that changes made to _STR
goes out of scope when the while loop terminates.
I've also tried using a global variable like the following:
CREATE PROCEDURE STRGEN ( IN ITER INT) BEGIN
DECLARE COUNTER INT DEFAULT 0;
DECLARE _STR VARCHAR(1000) DEFAULT NULL;
SET @S = NULL;
WHILE (COUNTER < @ITER) DO
SET _STR = CONCAT(_STR , CHAR(RAND()*100+65));
SET COUNTER = COUNTER + 1;
SET @S = _STR;
END WHILE;
INSERT INTO T2 VALUES(RAND()*100 , @S);
END;;
And also declaring @S
outside of the procedure , but nothing special happened.
Any help is appreciated.
Best regards.