Forum Moderators: phranque
I'm trying to write a query that does an insert statement multiple times until the while loop returns false. I tried executing the below code but MySQL server returns the following error. Oh also I'm running MySQL 3.23.37. I tried to check and see if the problem was due to the version I have but was unable to locate anything on MySQL's web site. For all I know maybe I'm going about this query the wrong way, never done a dowhile loop before in SQL. Any ideas to solve the problem would be greatly appreciated.
ERROR 1064: You have an error in your SQL syntax near 'PROCEDURE dowhile()
ERROR 1064: You have an error in your SQL syntax near 'WHILE v1 <= 41675 DO INSERT INTO PostalCodeReps(Postal_Code_Info_ID, Company' at line 1
ERROR 1064: You have an error in your SQL syntax near 'END WHILE' at line 1
ERROR 1064: You have an error in your SQL syntax near 'END' at line 1
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 41190;WHILE v1 <= 41675 DO
INSERT INTO PostalCodeReps(Postal_Code_Info_ID, Company_ID_Unitary, Company_ID_Applied) VALUES(v1, 1332,1332)
SET v1 = v1 + 1;
END WHILE;
END
[dev.mysql.com...]
<?php
// Connection:
$db_server = 'localhost';
$db_user = 'userid';
$db_pwd = 'password';
$db_name = 'test';
$db_link = mysql_connect($db_server, $db_user, $db_pwd)
or exit('Could not connect (' . mysql_errno() . '): '
. mysql_error()); # Test mode only
$db = mysql_select_db($db_name, $db_link)
or exit('Could not select database (' . mysql_errno() . '): '
. mysql_error()); # Test mode only
// Process:
for ($v1 = 41190; $v1 <= 41675; $v1++) {
mysql_query("INSERT INTO PostalCodeReps
(Postal_Code_Info_ID, Company_ID_Unitary, Company_ID_Applied)
VALUES($v1, 1332, 1332)");
}
?>