2

Hello i am trying insert 100-200 new rows to user_acc table , but for some reason my current code do nothing .

DELIMITER //
DECLARE x INT DEFAULT 0;

 WHILE x < 200 DO
INSERT INTO `user_acc`(`playRegion`, `firsttimelogin`) VALUES
(RAND() * (6)+1,1) ;
SET x = x +1;
 END WHILE;
DELIMITER ;

so i am adding playRegion and firsttimelogin for each of row , playRegion is random 1-5 and firstimelogin is walways 1. once i try to execute query nothing happens no error nothing .

3 Answers 3

5

Here is an alternative with a stored proc that uses Loop:

DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
BEGIN
  DECLARE int_val INT DEFAULT 0;
  test_loop : LOOP
      IF (int_val = 10) THEN
      LEAVE test_loop;
    END IF;
   INSERT INTO `user_acc`(`playRegion`, `firsttimelogin`) VALUES
   (RAND() * (6)+1,1) ;
    SET int_val = int_val +1;
  END LOOP; 
END;

call proc_loop_test;

select * from `user_acc`;

I limited to 10 just for testing

Functional Example that you can play around with

Credit for this solution

Sign up to request clarification or add additional context in comments.

2 Comments

@EgiDijus elaborate, check the link to rextester, works fine
you can save stored proc using WorkBench but otherwise use delimiters if running from shell. You can't run anonymous block, need stored proc
0

DECLARE Syntax

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

1 Comment

Please add some explanation to your answer such that others can learn from it. How does that excerpt from the documentation resolve the given problem?
0

It looks like you are trying to execute an anonymous code block, which MySQL does not support.

You could create a named stored procedure to execute that code.

Rather than implement your own stored procedure, my suggestion is to use the mysqlslap utility, which makes it easy to generate test data.

This command should work for you:

mysqlslap --iterations 200 \
  --create-schema=test \
  --query="INSERT INTO user_acc(playRegion, firsttimelogin) VALUES (RAND() * (6)+1,1) ;"

3 Comments

syntax error at line 1 1064 , and i am using 5.6 if thats have to do anything with it
You need to run mysqlslap in your shell, not at the mysql command line.
trying to run shell but none of my passwords are right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.