We help IT Professionals succeed at work.

Will my insert statement here work in Mysql?

motioneye
motioneye asked
on
289 Views
Last Modified: 2010-03-19
I would like to know whether this statement work in Mysql or not

DECLARE @i INT
SET @i=2      
WHILE @i<=20000
BEGIN
INSERT budget (SpendID,Amount,Currency,Country) SELECT @i,'Eur1000','EurDutch',Germany
SET @i=@i+1
END
GO

and how can I run it as batch file?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
What do you mean by 'run it as a batch'?

You do realize that you'll be inserting the same data 19999 times, yes?  Only `SpendID` will be different.  If `SpendID` is an auto-number field, you don't even need it in the query.

CREATE PROCEDURE `new_proc`()
BEGIN
  SET @i=2;
  WHILE @i<=20000 DO
      INSERT INTO table2 (spendid,amount,currency,country) VALUES(@i,'Eur1000','EurDutch','Germany');
      SET @i=@i+1;
  END WHILE;
END

Open in new window

Author

Commented:
Yes I know that same data will be inserted many, this is just my db test server which currently I'm doing my practice with Mysql, Ok how do I save this as a text file and run it from mysql>

Author

Commented:
Oh btw, How do I execute this stored procedure in Mysql> ?
CERTIFIED EXPERT
Top Expert 2004

Commented:
To use it as a stored procedure, execute this SQL:

CALL new_proc();  /* or whatever you named it */

If you want to run it on a schedule, create the procedure, and use cron or the Windows equivalent to call the mysql client.  Since you specifically asked about the possibility, you could put the meat of the procedure in a text file, and run that from the command line instead.  Here's a guide for you:

http://dev.mysql.com/doc/refman/5.1/en/mysql.html

Author

Commented:
I ran the procedure but it returned me error as following


mysql>
mysql> CREATE PROCEDURE `InsertTable`()
    -> BEGIN
    ->   SET @i=2;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
mysql>   WHILE @i<=20000 DO
    ->       INSERT INTO table2 (spendid,amount,currency,country) VALUES(@i,'Eur1000','EurDutch','Germany');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE @i<=20000 DO
      INSERT INTO table2 (spendid,amount,currency,country) VA' at line 1
mysql>       SET @i=@i+1;
Query OK, 0 rows affected (0.00 sec)

mysql>   END WHILE;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END WHILE' at line 1
mysql> END
CERTIFIED EXPERT
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.