The official documentation of MySQL says that using label for LOOP statement is optional. But when I used LOOP statement in a stored procedure without a label, an error occurred. The SQL script file is as follows:
DELIMITER $$
DROP PROCEDURE IF EXISTS loop_demo $$
CREATE PROCEDURE loop_demo()
BEGIN
DECLARE i INT;
DECLARE sum INT;
SET i = 0;
SET sum = 0;
LOOP
IF i > 10 THEN LEAVE ;
END IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE ;
ELSE
SET sum = sum + i;
END IF;
END LOOP;
SELECT sum;
END $$
DELIMITER ;
CALL loop_demo();
This is a simple loop which finds the sum of even no.s less than or equal to 10. But when I run the program the following error message generated:
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 IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE ;
ELSE
' at line 8
What could have went wrong?
Your are basically missing a label for the loop. It should be declared when the loop starts, and referred to in the ITERATE, LEAVE and END LOOP statements. I would also recommend not naming your variable sum, since it conflicts with a SQL keyword.
Consider:
DELIMITER $$
CREATE PROCEDURE loop_demo()
BEGIN
DECLARE i INT;
DECLARE sm INT;
SET i = 0;
SET sm = 0;
lbl: LOOP
IF i > 10 THEN LEAVE lbl;
END IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE lbl;
ELSE
SET sm = sm + i;
END IF;
END LOOP lbl;
SELECT sm;
END $$
DELIMITER ;
Demo on DB Fiddle
Related
I am just trying mysql stored procedure but I am getting the following error:
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 ';
DECLARE cur CURSOR for
select birth_date from employees;
' at line 3
Code:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done;
DECLARE cur CURSOR for
select birth_date from employees;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
DECLARE continue handler for not found
set done = 1;
open cur;
while done = 0 do
fetch birthdate into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
You have some bugs in your code
DECLARE without a tyoe is nocht valid
SET are only possible after DECLARE
and your CORSOR is cur not birthdate
you must check the datatypes for minsofar,maxsofar,curage,done
stored procedure:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done VARCHAR(10);
DECLARE cur CURSOR for
select birth_date from employees;
DECLARE continue handler for not found
set done = 1;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
open cur;
while done = 0 do
fetch cur into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
Is there a reason you are not using a simple query?
select min(birth_date), max(birth_date)
from employee;
This could be incorporated into a stored procedure, but that seems superfluous. Using a cursor is just a really, really, really bad practice -- even if you are learning SQL. Cursors should be avoided, except in the situations where they are necessary -- such as calling a stored procedure or invoking dynamic SQL for each row.
Your code also seems to be mixing up "ages" and "dates". It is a bit unclear what you really want to accomplish.
I am inserting the records in mysql database using while loop.I want to chcek the check atleast one record is inserted or not. I tried below code but ROW_COUNT() give me success, if the record is not inserted.
DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE res varchar(255);
WHILE count < 10 DO
/**Sql statement**/
SET count = count + 1;
END WHILE;
IF ROW_COUNT() > 0 THEN
SET res = 'success';
ELSE
SET res = 'failure';
END IF;
SELECT res;
END$$
DELIMITER ;
I am trying to use a WHILE loop in MySQL v5.7 and keep getting a syntax error. I haven't been able to identify the problem. The syntax looks correct according to the documentation.
I found a thread here suggesting wrapping the statement in a DELIMITER, but this did not work either. The code is:
SET #counter = 1;
WHILE (#counter < 2) DO
SELECT #counter;
#counter = #counter + 1;
END WHILE
and the error message is:
ERROR 1064 (42000) at line 22: 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 (#counter < 2) DO
SELECT #counter' at line 1
What am I missing?
As far as I remember, you cannot use WHILE LOOP just like that. You have to put it inside a Stored Procedure like so:
CREATE PROCEDURE mysp()
BEGIN
DECLARE counter int DEFAULT 1;
WHILE counter < 2 DO
SET counter = counter + 1;
END WHILE;
END
Try the following code. It ran successfully on my MySQL 5.7
DELIMITER //
CREATE PROCEDURE mysp()
BEGIN
DECLARE counter INT;
SET counter = 1;
label1: WHILE counter < 2 DO
SELECT counter;
SET counter = counter + 1;
END WHILE label1;
END; //
DELIMITER ;
I have a syntax error with my code, which adds 1000 random records to a table.\
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
first syntax error is at default 1 saying it's missing a semicolon, then at my_loop and there are like 4 more.. Any help please? It seems good to go from what I've gooogled.
You need to change the delimiter before defining the statement:
DELIMITER $$
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
$$
Otherwise, the ; will end the entire CREATE PROCEDURE statement.
I am trying to create a simple Stored procedure that allows me to conduct mass inserts, However I am running into syntactical troubles and unable to figure out where what's going wrong, despite comparing my procedure syntax to existing examples, and it seems to be correct.
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END
Upon running, MYSQL states:
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
and highlits this guy:
CREATE PROCEDURE populateUserTable( ) BEGIN DECLARE counter INT( 10 ) ;
What's up here?
Have you used
DELIMITER $$
At the start?
Try
DELIMITER $$
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END $$
DELIMITER ;