Syntax error in mysql Loop. MariaDB - mysql

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 $$

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;

Get Columns Single Record in Stored Procedure Mysql

I have my procedure and is working, but my question is the following,
with the cursor is working correctly, but before the cursor I need a Single Record with several columns, I donĀ“t know if I need another cursor just for one record.
Which would be the right way to get the columns of that single row without a cursor.
The query to execute is:
'SELECT id,anio,fec_iniciointeres FROM mytable WHERE id=3 '
DELIMITER $$
DROP PROCEDURE IF EXISTS db.cal_intereses$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `cal_intereses`()
BEGIN
DECLARE factura_id INT UNSIGNED;
DECLARE val_avaluo DECIMAL(16,2);
DECLARE fec_actual DATE;
DECLARE done INT;
DECLARE cur CURSOR FOR SELECT fac_facturas.id AS factura_id, fac_facturas.val_avaluo FROM fac_facturas WHERE fac_facturas.vigencia_id<=26 AND fac_facturas.estado=1 AND fac_facturas.val_avaluo>0 LIMIT 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
SET fec_actual=(SELECT CURDATE());
SET done = 0;
OPEN cur;
ciclo: LOOP
FETCH cur INTO factura_id,val_avaluo;
IF done=1 THEN LEAVE ciclo; END IF;
DELETE FROM val_interesaux;
IF fec_actual>='2006-07-29' THEN
INSERT INTO val_interesaux(factura_id,fec_inicio) VALUES(factura_id,fec_actual);
END IF;
END LOOP ciclo;
CLOSE cur;
END$$
DELIMITER ;

Mysql loop errors

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.

change mysql stored procedure to accept mysql field?

I found the below procedure and have edited it a bit - trying to figure out how i can set variable str to a query result 'tags' --(Select tags from Post where id > 3) so I can create the temp table from its contents... nothing I do seems to help.
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;
insert into Tmp values (str);
END LOOP simple_loop;
END
Select Into statement will do that.
BEGIN
DECLARE a INT Default 0 ;
DECLARE str VARCHAR(255);
simple_loop: LOOP
SET a=a+1;
Select tags into str from Post where id > 3;
SET str=SPLIT_STR(str,",",a);
IF str='' THEN
LEAVE simple_loop;
END IF;
insert into Tmp values (str);
END LOOP simple_loop;
END