1

I am new to MySQL (have just been using SQL Server) and I am wondering how to create multiple rows easily!

I am looking to create about 300 rows, so the methods I have found online just don't seem practical enough..

When using SQL Server, you can just use a GO statement, and then enter a number afterwards and it will run the command that many times, such as : GO 50

Is there anything as simple as this on MySQL? I have read about using a loop statement, but I cannot find any info on it?

Any help will be really appreciated!

Thanks,

-Liam.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate [link](https://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – ernesthm Apr 19 '18 at 16:00

3 Answers3

0

You could create a Repeat statement:

DELIMITER //

CREATE FUNCTION CalcularSueldo ( starting_value INT )
RETURNS INT

BEGIN

   DECLARE sueldo INT;

   SET sueldo = 0;

   label1: REPEAT
     SET sueldo = sueldo + starting_value;
   UNTIL sueldo >= 10000000 
   END REPEAT label1;

   RETURN sueldo;

END; //

DELIMITER ;

It's basically a loop, where you put the condition using UNTIL. Here it is used in a function but you can adapt it to your needs.

Nick
  • 1,032
  • 16
  • 27
  • Hi Nick, so where would I insert the code to insert my data? I believe that my code is along the lines of: INSERT INTO mysqltest.noderedtest (Password) VALUES "Test" - Something along those lines. – Liam Broughton Apr 20 '18 at 08:00
0

You may prepare you data with Excel, export as CSV and import to DB by common Database Tools like DBeaver and Sql Squirrel

https://github.com/dbeaver/dbeaver/issues/722

Jimmy Wong
  • 492
  • 4
  • 5
0

Without using loop:

CREATE TABLE noderedtest(Password TEXT, Email TEXT);

INSERT INTO noderedtest (Password, Email)  -- storing password in clear text
                                           -- is very bad idea
WITH RECURSIVE cte AS
(
   SELECT 1 AS i
   UNION ALL
   SELECT i+1
   FROM cte
   WHERE i < 300
)
SELECT 'Test', 'email@email.com'
FROM cte;

SELECT * FROM noderedtest;

DBFiddle Demo MySQL 8.0+

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • Hi there, I tried this code but it is giving me an error at the "WITH" part of the code. The error is - "Syntax Error: unexpected 'WITH' (with). Any ideas to why this is happening? Thanks. – Liam Broughton Apr 24 '18 at 15:44
  • @LiamBroughton `SELECT @@version` and my code is for MySQL 8.0 and above – Lukasz Szozda Apr 24 '18 at 15:46