473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with WHILE statement in MySQL

I can't get the WHILE statement to work in MySQL.

The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.

Here is the relevant section from the manual:

20.1.9.7 WHILE Statement

[begin_label:] WHILE search_conditio n DO
statement_list
END WHILE [end_label]

The statement or statements within a WHILE statement are repeated as
long as the search_conditio n is true.

begin_label and end_label must be the same, if both are specified.

For example:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO
....
SET v1 = v1 - 1;
END WHILE;
END

- - - - - - - - -

I tried to execute the example code - slightly modified:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

SET v1 = v1 - 1;
END WHILE;
END

I got the following error message:

"You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL version for the right syntax to use near
‘PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."

Then I tried it again with @ in front of the variables:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;

WHILE @v1 > 0 DO

SET @v1 = @v1 - 1;
END WHILE;
END

I got the same error message.

- - - - - - - - -

Apart from the above, I have tried numerous other variations including
table searches.

What am I doing wrong?

Thanks much for your help.
Jul 20 '05 #1
4 3274
James E Koehler wrote:
I can't get the WHILE statement to work in MySQL.

The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.

Here is the relevant section from the manual:

20.1.9.7 WHILE Statement

[begin_label:] WHILE search_conditio n DO
statement_list
END WHILE [end_label]

The statement or statements within a WHILE statement are repeated as
long as the search_conditio n is true.

begin_label and end_label must be the same, if both are specified.

For example:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END

- - - - - - - - -

I tried to execute the example code - slightly modified:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

SET v1 = v1 - 1;
END WHILE;
END

I got the following error message:

"You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL version for the right syntax to use near
‘PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."

Then I tried it again with @ in front of the variables:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;

WHILE @v1 > 0 DO

SET @v1 = @v1 - 1;
END WHILE;
END

I got the same error message.

- - - - - - - - -

Apart from the above, I have tried numerous other variations including
table searches.

What am I doing wrong?

Thanks much for your help.


Stored procedures aren't supported in MySQL until 5.x which is currently in
alpha development. You are using 4.0.6. This is why you are getting these
errors.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #2
Many thanks to Chris Hope for his help.

I am working on my own and I don't have quick access to any local
MySQL gurus. So I appreciate very much any help that participants of
this newsgroup may supply.

I went to http://dev.mysql.com/downloads/mysql/5.0.html and downloaded
MySQL version 5.0.0a-alpha.

I assume that version 5 is not intended for general use as it only
seems to handle one database called "test".

I set up the demo example:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;

WHILE @v1 > 0 DO

SET @v1 = @v1 - 1;
END WHILE;
END

but it still won't work - yielding the same unhelpful error message.

Am I right in thinking tat version 5 won't be available for general
use for quite a while - the info on http://www.mysql.com/ indicate hat
version 4.1 has just become production-ready.

- - - - - - - - - -

What I want to with a WHILE statement can very likely be done with one
or two MySQL statements.

What I want to do is fill in the zeroed values with copies of the
non-zero values resulting in the modified version of the table as
shown in VERSION 2 below.

In other words, I want to replicate entries following the entries as
originally supplied.

CREATE TABLE `table1` (
`record_number` int(8) NOT NULL auto_increment,
`field_A` int(11) default NULL,
PRIMARY KEY (`record_number `),
UNIQUE KEY `record_number` (`record_number `)
) TYPE=MyISAM

VERSION 1
1 0
2 0
3 0
4 99
5 0
6 0
7 0
8 0
9 33
10 0
11 0
12 0

VERSION 2
1 0
2 0
3 0
4 99
5 99
6 99
7 99
8 99
9 33
10 33
11 33
12 33

This would be trivial with a WHILE statement. However I cant figure
out how to do this in MySQL without one.

Your help would be much appreciated.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Chris Hope <bl*******@elec trictoolbox.com > wrote in message news:<10******* *******@216.128 .74.129>...
James E Koehler wrote:
I can't get the WHILE statement to work in MySQL.

The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.

Here is the relevant section from the manual:

20.1.9.7 WHILE Statement

[begin label:] WHILE search condition DO
statement list
END WHILE [end label]

The statement or statements within a WHILE statement are repeated as
long as the search condition is true.

begin label and end label must be the same, if both are specified.

For example:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END

- - - - - - - - -

I tried to execute the example code - slightly modified:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

SET v1 = v1 - 1;
END WHILE;
END

I got the following error message:

"You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL version for the right syntax to use near
PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."

Then I tried it again with @ in front of the variables:

CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;

WHILE @v1 > 0 DO

SET @v1 = @v1 - 1;
END WHILE;
END

I got the same error message.

- - - - - - - - -

Apart from the above, I have tried numerous other variations includin

g
table searches.

What am I doing wrong?

Thanks much for your help.


Stored procedures aren't supported in MySQL until 5.x which is currentl
y in
alpha development. You are using 4.0.6. This is why you are getting the
se
errors.

Jul 20 '05 #3
:) I see why you want a while loop

SELECT * FROM table1 WHERE field_A>0

And then with the result array you want to fill the record_numers between
result 0 and 1 that have value 0 for field_A with the value of field_A of
result 0.
And then the same for the record_nubers between result 1 and 2 etc.

:)
Maybe use PHP for that loop?

Wouter

"James E Koehler" <ko*****@btinte rnet.com> wrote in message
news:d7******** *************** ***@posting.goo gle.com...
: Many thanks to Chris Hope for his help.
:
: I am working on my own and I don't have quick access to any local
: MySQL gurus. So I appreciate very much any help that participants of
: this newsgroup may supply.
:
: I went to http://dev.mysql.com/downloads/mysql/5.0.html and downloaded
: MySQL version 5.0.0a-alpha.
:
: I assume that version 5 is not intended for general use as it only
: seems to handle one database called "test".
:
: I set up the demo example:
: CREATE PROCEDURE dowhile()
: BEGIN
: DECLARE @v1 INT DEFAULT 5;
:
: WHILE @v1 > 0 DO
:
: SET @v1 = @v1 - 1;
: END WHILE;
: END
:
: but it still won't work - yielding the same unhelpful error message.
:
: Am I right in thinking tat version 5 won't be available for general
: use for quite a while - the info on http://www.mysql.com/ indicate hat
: version 4.1 has just become production-ready.
:
: - - - - - - - - - -
:
: What I want to with a WHILE statement can very likely be done with one
: or two MySQL statements.
:
: What I want to do is fill in the zeroed values with copies of the
: non-zero values resulting in the modified version of the table as
: shown in VERSION 2 below.
:
: In other words, I want to replicate entries following the entries as
: originally supplied.
:
: CREATE TABLE `table1` (
: `record_number` int(8) NOT NULL auto_increment,
: `field_A` int(11) default NULL,
: PRIMARY KEY (`record_number `),
: UNIQUE KEY `record_number` (`record_number `)
: ) TYPE=MyISAM
:
: VERSION 1
: 1 0
: 2 0
: 3 0
: 4 99
: 5 0
: 6 0
: 7 0
: 8 0
: 9 33
: 10 0
: 11 0
: 12 0
:
: VERSION 2
: 1 0
: 2 0
: 3 0
: 4 99
: 5 99
: 6 99
: 7 99
: 8 99
: 9 33
: 10 33
: 11 33
: 12 33
:
: This would be trivial with a WHILE statement. However I cant figure
: out how to do this in MySQL without one.
:
: Your help would be much appreciated.
:
: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:
: Chris Hope <bl*******@elec trictoolbox.com > wrote in message
news:<10******* *******@216.128 .74.129>...
: > James E Koehler wrote:
: >
: > > I can't get the WHILE statement to work in MySQL.
: > >
: > > The version of MySQL that I am using is:
: > > Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
: > > running on Windows MX.
: > >
: > > Here is the relevant section from the manual:
: > >
: > > 20.1.9.7 WHILE Statement
: > >
: > > [begin label:] WHILE search condition DO
: > > statement list
: > > END WHILE [end label]
: > >
: > > The statement or statements within a WHILE statement are repeated as
: > > long as the search condition is true.
: > >
: > > begin label and end label must be the same, if both are specified.
: > >
: > > For example:
: > >
: > > CREATE PROCEDURE dowhile()
: > > BEGIN
: > > DECLARE v1 INT DEFAULT 5;
: > >
: > > WHILE v1 > 0 DO
: > > ...
: > > SET v1 = v1 - 1;
: > > END WHILE;
: > > END
: > >
: > > - - - - - - - - -
: > >
: > > I tried to execute the example code - slightly modified:
: > >
: > > CREATE PROCEDURE dowhile()
: > > BEGIN
: > > DECLARE v1 INT DEFAULT 5;
: > >
: > > WHILE v1 > 0 DO
: > >
: > > SET v1 = v1 - 1;
: > > END WHILE;
: > > END
: > >
: > > I got the following error message:
: > >
: > > "You have an error in your SQL syntax. Check the manual that
: > > corresponds to your MySQL version for the right syntax to use near
: > > PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."
: > >
: > > Then I tried it again with @ in front of the variables:
: > >
: > > CREATE PROCEDURE dowhile()
: > > BEGIN
: > > DECLARE @v1 INT DEFAULT 5;
: > >
: > > WHILE @v1 > 0 DO
: > >
: > > SET @v1 = @v1 - 1;
: > > END WHILE;
: > > END
: > >
: > > I got the same error message.
: > >
: > > - - - - - - - - -
: > >
: > > Apart from the above, I have tried numerous other variations includin
: > g
: > > table searches.
: > >
: > > What am I doing wrong?
: > >
: > > Thanks much for your help.
: >
: > Stored procedures aren't supported in MySQL until 5.x which is currentl
: > y in
: > alpha development. You are using 4.0.6. This is why you are getting the
: > se
: > errors.
Jul 20 '05 #4
James E Koehler wrote:
I set up the demo example:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;

WHILE @v1 > 0 DO

SET @v1 = @v1 - 1;
END WHILE;
END

but it still won't work - yielding the same unhelpful error message.
You're using what appears to be Microsoft SQL Server syntax.
Did you try reading the docs on the MySQL stored procedure language?

There's an example of a WHILE loop here:
http://dev.mysql.com/doc/mysql/en/WHILE_Statement.html
Am I right in thinking tat version 5 won't be available for general
use for quite a while - the info on http://www.mysql.com/ indicate hat
version 4.1 has just become production-ready.


Correct, 5.0 is in a state of "alpha" according to MySQL.com.
Features are no doubt subject to change, and the release date is not stated.

Regards,
Bill K.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4792
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element in the for loop. I am including the portion of the code : #Get the connection to the database my $dbh = &getConnection(); my @acodes;
16
3191
by: Andie | last post by:
Hello All, I have this sql statement thats works in MSSQL when I call a function function showNewProd(dispNum) 'Declare some variables dim mySQL, rsTemp, tempStr, count 'Read Database
1
3642
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is produced due to the unknown GCC compiler option "+DAportable". For the Build using CC, the preprocessor error is produced due to the recursive declration of macro "PerlIO" in perlio.h file.
0
1715
by: nrip | last post by:
Dear All, I am facing a very peculiar problem. I am reading a CSV file from my JSP code and trying to insert them into MYSQL database. the program first reads a line and then splits it into words using the split function and then inserts them into different fields of the database. But the problem is that the JSP ; token=line.split(" "); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
10
1849
by: sssk28 | last post by:
i have installed fedora core 6 and i am getting following error: type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
9
1694
by: Dave | last post by:
Hi guys, I have just set up a duplicate server running: apache 2.54, mysql 5.04 and php 5.04 This is the same setup as as the server we are using now, apart from the hardware inside. I have copied across the database and website, with exact same permissions as the first server. The problem is that part of the php code is executing but others
3
3827
by: codeninja | last post by:
Hello, I'm trying to write a jsp page that calls a java class, which accesses a MYSQL database, and for some reason i get "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" while trying to establish the connection. I tried putting the connector .jar in the WEB-INF\lib, I tried setting the classpath variable to the connector's location, and nothing seems to fix it. Am I using the wrong class for the driver, or is it something else. Here...
2
2882
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
3
1245
by: pavani1 | last post by:
Hi, After signin in my home page one button is there.i,e update profile.for that i wrote the below code but it will not work.plz tell that what's the problem in my code. <?php // Connect database. $host="localhost"; // Host name. $db_user="root"; // MySQL username. $db_password="sreeni"; // MySQL password. $database="my_db1"; // Database name. mysql_connect($host,$db_user,$db_password);
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.