Chevy Mark Sunderland
Chevy Mark Sunderland

Reputation: 435

Insert with While Loop in MySQL database table

I'm trying to insert a bunch of records in my MySQL database table.

I am not expert to create a stored procedure.

Here is my code:

BEGIN

 DECLARE var INT; 
 SET var = 0; 
 WHILE var < 100000 DO 
  INSERT INTO stored_copy (total, active, stored) VALUES (var, 1, 1);
  SET var = var + 1; 
 END WHILE; 

END;

Here is the error:

enter image description here

Can anyone check what I am doing wrong?

Upvotes: 0

Views: 964

Answers (2)

Chris
Chris

Reputation: 51

Update the name of "stored" column in stored_copy table. stored is a reserved word.

Upvotes: 1

nbk
nbk

Reputation: 49373

stored is a reserved word you have to use backticks

see also When to use single quotes, double quotes, and backticks in MySQL

BEGIN

 DECLARE var INT; 
 SET var = 0; 
 WHILE var < 100000 DO 
  INSERT INTO stored_copy (total, active, `stored`) VALUES (var, 1, 1);
  SET var = var + 1; 
 END WHILE; 

END;

Upvotes: 1

Related Questions