473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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_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 including
table searches.

What am I doing wrong?

Thanks much for your help.
Jul 20 '05 #1
4 3249
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 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*******@electrictoolbox.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*****@btinternet.com> wrote in message
news:d7**************************@posting.google.c om...
: 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*******@electrictoolbox.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
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...
16
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
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...
0
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...
10
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...
9
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...
3
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...
2
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...
3
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.