Mysql loop errors - mysql

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.

Related

I want the min number of the column

I don't know why i have a syntax error.
I need to get the minim number of the column salario from empleados.
I comment line per line and the error appears on the while
Error syntax image
Please help, thanks
DELIMITER $$
DROP FUNCTION IF EXISTS `empresa`.`Minim` $$
CREATE FUNCTION `empresa`.`Minim` () RETURNS INT
BEGIN
declare numregs int;
declare i int default 0;
declare v_minim int default 0;
declare v_salario int default 0;
declare cminim cursor for select salario from empleados;
select count(*) into numregs from empleados;
open cminim;
while i<numregs do
fetch cminim into v_salario;
if i=0 then
v_minim=v_salario;
end if;
if i>0 and v_salario<v_minim then
v_minim=v_salario;
end if;
i=i+1;
end while;
return v_minim;
close cminim;
END $$
DELIMITER ;
The error has show itself clearly,the reason is that when you assign value of the variable ,you missing the keywork set.
You need to use set keyword to assign value
while i<numregs do
fetch cminim into v_salario;
if i=0 then
set v_minim=v_salario; -- use set
end if;
if i>0 and v_salario<v_minim then
set v_minim=v_salario;
end if;
set i=i+1;
end while;

MySQL Syntax Error - END IF line

I try to create a stored procedure with an if statement within.
I copied from: https://dev.mysql.com/doc/refman/5.7/en/local-variable-scope.html
But I get the following error exact on the END IF; near '':
DROP PROCEDURE IF EXISTS `myProc`;
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;
Has anyone an idea where I make a mistake?
Exact error message:
SQL 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 '' at line 19 SQL Error
MySQL Version:
5.5.46
Thanks for help!
I found the solution.
I have to set DELIMITER $$ at first statement and at the end DELIMITER ;
DELIMITER $$;
DROP PROCEDURE IF EXISTS `myProc`; $$
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;$$
DELIMITER ;
It is important to set the keyword on the first position in line. If there is a blank on the first position, the error above will be thrown.
Well you are missing the loop label while ending loop. Change it to below
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
SELECT tmp_courseId, in_userId;

Syntax error in mysql Loop. MariaDB

I am getting a syntax error at simple_loop: Loop statement saying
Unexpected character near ':'
in the line simple_loop: LOOP
Please help
DELIMITER $$
CREATE PROCEDURE getTable(fullstr VARCHAR(555))
BEGIN
DECLARE a INT Default 0
DECLARE str VARCHAR(255)
simple_loop: LOOP
SET a=a+1
SET str=SPLIT_STR(fullstr,",",a)
IF str='' THEN
LEAVE simple_loop
END IF
#Do Inserts into temp table here with str going into the row
insert into my_temp_table values (str)
END LOOP simple_loop
END $$
It is a very annoying MySQL syntax related issue: There is a TAB somewhere in you procedure leading to this parse issue.
Note: Separate ;
DELIMITER $$
CREATE PROCEDURE getTable(fullstr VARCHAR(555))
BEGIN
DECLARE a INT Default 0 ;
DECLARE str VARCHAR(255);
simple_loop: LOOP
SET a=a+1;
SET str=SPLIT_STR(fullstr,",",a);
IF str='' THEN
LEAVE simple_loop;
END IF;
#Do Inserts into temp table here with str going into the row
insert into my_temp_table values (str);
END LOOP simple_loop;
END $$

Nested Loop not working in Mysql

I'm trying to implement a nested loop in Mysql without cursors.
DELIMITER $$
CREATE FUNCTION abc()
RETURNS INT
BEGIN DECLARE val INT;
DECLARE row,col INT DEFAULT 1;
SET val=1;
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
RETURN val;
END LOOP row_loop;
END;
$$
The output should be 64 rather but I'm getting 8 as the inner loop is not running after 1st iteration.
Your logic is fine, except the return statement is inside the loop:
row_loop: LOOP
SET col=1;
col_loop: LOOP
SET col=col+1;
SET val=val+1;
IF col>8 THEN
LEAVE col_loop;
END IF;
END LOOP col_loop;
SET row=row+1;
IF row>8 THEN
LEAVE row_loop;
END IF;
END LOOP row_loop;
RETURN val;

Printing out multiple rows in stored procedure

I want to pass in an int value and get back a list of short passwords that are less than this int. I have had trouble modifying the MySQL cursors example: http://dev.mysql.com/doc/refman/5.0/en/cursors.html. I want to print out the rows with short passwords but any time the result has more than one row PhpMyAdmin just displays "Processing request" indefinitely. What am I doing wrong? Its ruining my Saturday productivity!
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE b VARCHAR(255);
DECLARE cur1 CURSOR FOR SELECT UID, Password
FROM authentication WHERE CHARACTER_length(Password) < passwordLength;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a, b;
IF done THEN
LEAVE read_loop;
END IF;
SELECT b;
END LOOP;
CLOSE cur1;
END
$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
SELECT UID, Password
FROM authentication WHERE CHARACTER_length(Password) > passwordLength;
END
$$
DELIMITER ;
You will get rows UID, password directly. as you get from a query when you
call ShortPasswd;
I suggest to modify these lines:
...
DECLARE FINISHED BOOLEAN DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
...
IF done THEN
CLOSE cur1;
LEAVE read_loop;
END IF;
...
END LOOP read_loop;
I use the 0/1 instead of FALSE/TRUE, and close the cursor before leave the LOOP.