472,993 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 3221
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.