When running my query I am getting this:
error:
#1064 - 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
SQL:
DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();
You need to define a new delimiter
delimiter |
DROP PROCEDURE IF EXISTS insertPromos |
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|
delimiter ;
CALL insertPromos();
Otherwise the procedure definition would end at the first ; which would not be correct.
Related
DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END;
DELIMITER ;
The error
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 'DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
' at line 1
I'm trying to insert 100 rows inside the table using the loop but that does not work.
REPEAT is a reserved word in MySQL. If you want to use it for userland names, you should quote it or rather use another name.
Use $$ delimiter to properly mark the end of CREATE PROCEDURE statement.
The result:
DELIMITER $$
CREATE PROCEDURE `repeat`()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END$$
DELIMITER ;
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;
i'm getting an error while creating a stored procedure, where it says that i have a syntax error, but i can't find where it is...
MySql Error:
"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 '$$DELIMITER
CREATE PROCEDURE 'entradas_sai'(IN ID_VEICULO VARCHAR(45), OUT' at
line 1
Here is the code related to the topic:
$$DELIMITER
CREATE PROCEDURE 'entradas_sai'(
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE '_rollback' BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(CURDATE("yyyy-MM-dd"),CURTIME("hh:mm:ss))
UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;
Here is a screenshot of my MySqlWB:
EDIT:
Here is the log:
>Executing:
>USE `portaria`;
>DROP unknown IF EXISTS `unknown_SYNTAX_ERROR`;
>
>DELIMITER $$
>USE `portaria`$$
>DELIMITER $$
>
>CREATE PROCEDURE 'entradas_sai'(
>IN ID_VEICULO VARCHAR(45), OUT retcode INT)
>BEGIN
> DECLARE '_rollback' BOOL DEFAULT 0;
> DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
> START TRANSACTION;
> INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()))
> UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
> IF '_rollback' THEN
> SET retcode = 0;
> ROLLBACK;
> ELSE
> SET retcode = 1;
> COMMIT;
> END IF;
>END$$
>
>DELIMITER ;$$
>
>DELIMITER ;
>
>ERROR 1064: 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 >'unknown IF EXISTS `unknown_SYNTAX_ERROR`' at line 1
>SQL Statement:
>DROP unknown IF EXISTS `unknown_SYNTAX_ERROR`
Here i got a scenarion of what i want my procedure to do...
I have a table called "Entradas" and one called "Saidas". Both have
the column "data" and "hora". With that in mind, since the "data" and
"hora column of "entradas" mean that a car joined at that date and
time, and the values from the columns "data" and "hora" of "saidas"
are mean to be inserted as i click a button, inserting the current
date and time. I'm requesting a sql syntax that could insert the
values "data" and "hora" into the table "saidas" and update a value of
"entradas" called "sai" which is equal to 0 and i want it to change to
1 on button press... Any sugestion?
delimiter symbol ($$) come after DELIMITER keyword. also use date(now()) instead CURDATE("yyyy-MM-dd") and time(now()) instead CURTIME("hh:mm:ss"))
Try This
DELIMITER $$
CREATE PROCEDURE `entradas_sai`(IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE _rollback boolean DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()));
UPDATE ENTRADAS SET SAI = #SAI;
IF _rollback = 1 THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;
If i'm right, you mistyped $$DELIMITER, it should be DELIMITER $$
EDIT: after adding the screenshot to the question, i believe you shouldn't quote the procedure name. I have update the code-block, try it again please.
DELIMITER $$
CREATE PROCEDURE entradas_sai(
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE '_rollback' BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(CURDATE("yyyy-MM-dd"),CURTIME("hh:mm:ss"))
UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;
I figured what was wrong, and it wasn't only about "DELIMITER" but with a couple things more...
Here is the fixed code:
CREATE PROCEDURE entradas_sai (
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE _rollback BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()));
UPDATE ENTRADAS SET SAI=1;
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END $$
Steps: Removed the "Delimiter $$" and "Delimiter ;" and then restructured the UPDATE query. That was the main problem, because the syntax wasn't on the spot... Thanks all who tryied to help.
In this case the value of "1" wasn't the value i want... So, i changed it to #sai, which means that the value is given at a button click, which is a increment of the parameter #sai, being equal to 1,2,3,4 and so on.
I have an error in this stored procedure:
Script line: 4 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 'INSERT INTO escuelas_asignacion_maestro (idEscuela,
idCiclo, `idGrado' at line 31
Code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `zz73ff`.`pr_trasladar_asignaciones_por_ciclo` $$
CREATE PROCEDURE `zz73ff`.`pr_trasladar_asignaciones_por_ciclo`
(IN p_ciclo int, IN n_ciclo int)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE escuela, ciclo, grado, nGrupos, maestro INT;
DECLARE fecha datetime;
DECLARE cursor_asignaciones CURSOR FOR
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo
AND idEstatus = 1
ORDER BY idEscuela, idGrado;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SELECT now() INTO fecha;
OPEN cursor_asignaciones;
REPEAT
FETCH cursor_asignaciones INTO escuela, ciclo, grado, nGrupos, maestro;
IF grado > 1 THEN
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro INTO
escuela, ciclo, grado, nGrupos, maestro
FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo AND idEstatus = 1 AND idEscuela = escuela
AND idGrado = (grado - 1);
END IF
INSERT INTO `escuelas_asignacion_maestro`
(`idEscuela`, `idCiclo`, `idGrado`, `numeroGrupos`, `idEstatus`, `idMaestro`, `fechaModificacion`)
VALUES (escuela, n_ciclo, grado, nGrupos, 1,0, fecha);
UNTIL done END REPEAT;
CLOSE cursor_asignaciones;
END $$
DELIMITER ;
Trying to writing this function in mysql but it is giving error that:
Error Code : 1064
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 'IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END ' at line 8
DELIMITER$$
CREATE FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))
RETURNS REAL
DETERMINISTIC
BEGIN
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL
IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;
RETURN t;
END IF;
END $$
DELIMITER;
any help should be appreciated. Thanx in advance.
missing THEN ?
DELIMITER$$
CREATE FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))
RETURNS REAL
DETERMINISTIC
BEGIN
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL **THEN**
IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;
RETURN t;
END IF;
END $$
DELIMITER;