I want the min number of the column - mysql

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;

Related

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

MySQL stored procedure causes error

Can someone kindly tell me where I'm wrong ?
This procedure returns an error near
#recuperato = #recuperato - saldofattura;
I mistake to update the variable #recuperato?
Thanks to all
DELIMITER //
DROP PROCEDURE IF EXISTS fatture_lettere_retail//
CREATE PROCEDURE fatture_lettere_retail (idcontratto INT(11))
BEGIN
DECLARE finito INT default 0;
DECLARE idfattura INT default 0;
DECLARE saldofattura DECIMAL(10,2);
DECLARE cur1 CURSOR FOR SELECT idfattura,saldofattura FROM fatture_lettere_isa;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
SET finito = 1;
SET #recuperato=(SELECT SUM(valorea)-SUM(valorer) FROM ImportiContratto WHERE idcontratto=idcontratto AND idimporto=1);
ciclo: LOOP
SET finito = 0;
FETCH cur1 INTO idfattura,saldofattura;
IF finito THEN
LEAVE ciclo;
END IF;
IF (#recuperato-saldofattura>=0) THEN
#recuperato = #recuperato-saldofattura;
DELETE FROM fatture_lettere_isa WHERE idfattura=idfattura;
ELSE
UPDATE fatture_lettere_isa SET saldofattura=saldofattura-#recuperato;
LEAVE ciclo;
END IF;
END LOOP ciclo;
CLOSE cur1;
END; //
DELIMITER;
Add the SET keyword at the beginning of the row in question:
SET #recuperato = #recuperato-saldofattura;
You have to write SET keyword to assign value to variable "#recuperato" as follow:
SET #recuperato = #recuperato-saldofattura;

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.

Mysql syntax error on stored procedure

I run this code as a sql script from command line, and I get "you have an error in your SQL syntax" almost in all lines! Any ideas what is wrong here?
CREATE PROCEDURE updatemandate()
BEGIN
DECLARE _mandate_id BIGINT(20);
DECLARE _has_succesful_payment tinyint(1);
DECLARE done INT DEFAULT 0;
DECLARE cnt INT;
DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN mandateCursor;
allmandates: LOOP
Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
IF done THEN LEAVE allmandates;
END IF;
Select COUNT(*) FROM payments WHERE mandate_id=_mandate_id AND status='OK' into cnt;
IF cnt>0 THEN
SET _has_succesful_payment=1;
END IF;
END LOOP allmandates;
CLOSE mandateCursor;
END
The ; character is the default delimiter, so when MySQL sees the first ; it thinks you are done. When you create a sproc, you need to declare a different delimiter character, like so:
DELIMITER $$
CREATE PROCEDURE updatemandate()
READS SQL DATA
BEGIN
DECLARE _mandate_id BIGINT(20);
DECLARE _has_succesful_payment tinyint(1);
DECLARE done INT DEFAULT 0;
DECLARE cnt INT;
DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN mandateCursor;
allmandates: LOOP
Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
IF done THEN LEAVE allmandates;
END IF;
Select COUNT(*) FROM payment WHERE mandate_id=_mandate_id AND status='OK' into cnt;
IF cnt>0 THEN
SET _has_succesful_payment=1;
END IF;
END LOOP allmandates;
CLOSE mandateCursor;
END$$
DELIMITER ;
Also a good idea to add the READS SQL DATA to the sproc definition in case you need to support binary logging.
Have you tried putting delimiter // before the CREATE statement and change the last line with END //?