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 ;
Related
I try to update some columns using trigger before insert
DROP TRIGGER IF EXISTS update_p_posts_places;
DELIMITER $$
CREATE TRIGGER update_p_posts_places BEFORE
INSERT
ON
`p_posts` FOR EACH ROW
BEGIN
DECLARE
p_post_group_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
BEGIN
DECLARE
place1_id_ int;
place2_id_ int;
place3_id_ int;
place4_id_ int;
place5_id_ int;
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
END $$
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
DELIMITER ;
It's showing some syntax errors.
#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 'DECLARE
place1_id_ int;
place2_id_ int;
' at line 20
Here is a working trigger. I tested creating it on MySQL 5.7.
CREATE TRIGGER update_p_posts_places BEFORE INSERT ON `p_posts`
FOR EACH ROW
BEGIN
-- all declarations must be before any other statements
DECLARE p_post_group_id_, place1_id_, place2_id_, place3_id_,
place4_id_, place5_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
You can use one DECLARE for multiple local variables, but you must do like var1, var2, var3, ... int. In other words, name the type only once at the end. See documentation: https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
No need for the BEGIN..END inside the IF and definitely do not use $$ until after the last END because that will terminate the parser's interpretation of your whole CREATE TRIGGER statement before it's complete.
I'm trying to make a Nested Cursor in Mysql by following this instruction.
Then i got this issue:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;' at line 22
I've 2 table 'account' and 'n_activity' (n = account_id in table 'account')
Ex: i've table 'account' and '20_activity'.
So i want to loop the 'account_id' and get the 'activity_id' from that loop.
Here is my code:
DROP PROCEDURE if exists update_schema_activity_startdate_and_duedate;
DELIMITER $$
CREATE PROCEDURE update_schema_activity_startdate_and_duedate()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE accountid INT;
--
-- GET ALL ACCOUNT ID
--
DECLARE accountids CURSOR FOR SELECT account_id FROM account;
--
-- LOOP
--
OPEN accountids;
read_loop: LOOP
FETCH accountids INTO accountid;
BLOCK2: BEGIN
SET #_activity = CONCAT(accountid,'_activity');
DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;
END LOOP;
CLOSE accountids;
END$$
DELIMITER ;
CALL update_schema_activity_startdate_and_duedate();
Please help, thanks.
I'm using MySql57
What's wrong with this script?(I'm Mysql newbie)
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 '= 'A0001'; ELSE BEGIN gubun1 = LEFT(cur_max,0,1); gu' at line 10
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE cur_max varchar(100);
DECLARE gubun1 varchar(1);
DECLARE gubun2 varchar(100);
DECLARE RTN_VAL varchar(100);
SELECT IFNULL(MAX(gubun_code),'A0001') INTO cur_max from gubun_code;
IF cur_max = 'A0001' THEN RTN_VAL = 'A0001';
ELSE
BEGIN
gubun1 = LEFT(cur_max,0,1);
gubun2 =
LPAD(CONVERT(CONVERT(RIGHT(cur_max,4),UNSIGNED)+1,CHAR),4,'0');
RTN_VAL = CONCAT(gubun1,gubun2);
END
RETURN RTN_VAL;
END $$
DELIMITER ;
There are several errors in the function. I think this might work:
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE v_cur_max varchar(100);
DECLARE v_RTN_VAL varchar(100);
SELECT COALESCE(MAX(gubun_code), 'A0001')
INTO v_cur_max
FROM gubun_code;
IF cur_max = 'A0001' THEN
SET v_RTN_VAL = 'A0001';
ELSE
SET v_RTN_VAL = CONCAT(LEFT(cur_max, 1), RIGHT(cur_max, 4) + 1);
END IF;
RETURN v_RTN_VAL;
END $$
DELIMITER ;
I would further simplify this by doing all the logic in the SQL statement, but this seems to be in the spirit of your solution.
I am not strong in PhpMyAdmin, nor in MySql nonstandard syntax. I am trying to create a simple stored procedure using PhpMyAdmin and the engine just repeats the same:
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
Simple stored procedure is:
CREATE PROCEDURE proc_test()
BEGIN
DECLARE int_val INT;
SET int_val = int_val + 1;
SELECT int_val;
END;
Here is the version:
select ##version
5.6.28
I am starting to hate MySql. Why I can not manage to create such a simple stored procedure?
You need to add some delimiter to the code. This should do:
DELIMITER $$
CREATE PROCEDURE proc_test()
BEGIN
DECLARE int_val INT;
SET int_val = int_val + 1;
SELECT int_val;
END $$
you should use the DELIMITER. link.
DELIMITER //
CREATE PROCEDURE proc_test()
BEGIN
DECLARE int_val INT;
SET int_val = int_val + 1;
SELECT int_val;
END //
DELIMITER ;
but when you call this procedure it always show null. so you should set int_val value by DEFAULT value or using SET
DECLARE int_val INT DEFAULT 0;
OR
SET int_val = 0
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.