Can anyone see why I would get:
[Err] 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 '//
DELIMITER' at line 11
For the following snippet:
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END//
DELIMITER;
This is a simplified version of what I am actually writing and it brings up exactly the same error. Everything is great until I add the nested BEGIN END block. It works perfectly well on phpMyAdmin but fails on Navicat 9
It should have a space after DELIMITER.
DELIMITER ;
Like that.
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END //
DELIMITER ;
On execution, it generates:
0 rows affected, 0 rows found. Duration for 2 queries: 0.000 sec.
Related
I have a stored procedure in Mysql database like this:
DELIMITER $$
USE `vboard_75`$$
DROP PROCEDURE IF EXISTS `sp_LongWaitCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_LongWaitCall`()
BEGIN
UPDATE cdr SET cdr.CallStatus='DISCONNECTED',cdr.EndTime=NOW() WHERE cdr.CallStatus='RINGINGIN'
AND MINUTE(DATEDIFF(cdr.StartTime,NOW())) >=7;
DECLARE _StatVal FLOAT;
DECLARE _DevID INT;
DECLARE Record_Fetch INT DEFAULT 0;
DECLARE crsr_Board CURSOR FOR
SELECT IFNULL(MAX(SECOND(DATEDIFF(CDR.StartTime, IFNULL(CDR.EndTime, NOW())))), 0) AS LRT,vw_Boards_Ext.boardid
FROM vw_Boards_Ext RIGHT OUTER JOIN
boards ON vw_Boards_Ext.boardid = boards.boardid LEFT OUTER JOIN
CDR ON vw_Boards_Ext.Ext = CDR.DDI AND
STR_TO_DATE(CONCAT(boards.ResetDate,' ',boards.ResetTime),'%m/%d/%Y %H:%i') < CDR.timestamp
AND CDR.CallStatus='RINGINGIN'
GROUP BY vw_Boards_Ext.boardid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Record_Fetch = 1;
OPEN crsr_Board;
FETCH crsr_Board INTO _StatVal, _DevID;
WHILE Record_Fetch = 0 DO
UPDATE stat_values AS sv SET sv.StatValue = _StatVal,sv.timestamp= NOW()
WHERE sv.itemId = _DevID AND sv.itemType = 'boards' AND sv.StatId = 3;
FETCH crsr_Board INTO _StatVal, _DevID;
END WHILE;
CLOSE crsr_Board;
DEALLOCATE PREPARE crsr_Board;
END$$
DELIMITER ;
Error:
Query: CREATE DEFINER=root#localhost PROCEDURE sp_LongWaitCall() BEGIN update cdr set cdr.CallStatus='DISCONNECTED',cdr.EndTime=n...
Error Code: 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 _StatVal FLOAT;
DECLARE _DevID INT;
DECLARE Record_Fetch INT DEFAULT 0' at line 8
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.001 sec
This documentation refers to having the DECLARE statements at the top and also before the creation of any CURSORS and HANDLERS, which you are following but not at the beginning of the BEGIN... END block.
This documentation clarifies the question that OP had posted!
It is mandatory to have your declarations at the begin of your BEGIN... END block, but not under an UPDATE statement.
Additional reference to a similar issue can be found on this question, even which refers to the same solution.
Hope this helps!
I have problem with declare variable in stored procedure. I used MySQL. My example code:
CREATE PROCEDURE `name`()
BEGIN
DECLARE varname INT;
SELECT * FROM `table` INTO var;
END
MySQL returns error:
error 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 '' at line 3
When you have multiple statements in a procedure you have to change the delimiter. Otherwise MySQL thinks that the procedure declaration is finished after the first statement.
Here's an example that works. And btw, var is not a reserved keyword like others are trying to tell you.
DELIMITER $$
CREATE PROCEDURE test ()
BEGIN
DECLARE var INT;
SELECT 1 INTO var;
SELECT var;
END $$
DELIMITER ;
CALL test();
+------+
| var |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
var is a reserved key word
CREATE PROCEDURE test ()
BEGIN
DECLARE var2 INT;
// Do something
END;
Update :
I saw MAriaDB in the error , the correct way to declare variable in MariaDB is
SET #var = 0;
You should tag your question with the correct keywords
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 ;
I am trying to create a trigger that marks items as deleted when they are inserted into the database.
Sadly I can't get my DECLARE to stop erroring, I have looked at the DECLARE docs and also at a few examples but I must be missing something.
The query I have so far is:
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END
The error message is showing:
#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 4
Thanks for your help and preventing me from defenestrating myself!
Try this:
DELIMITER $$
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END$$
DELIMITER ;
You need to change the delimiter when you create TRIGGER or STORED PROCEDURE.
By default, MySQL itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause MySQL to pass the entire stored program definition to the server. Otherwise, MySQL breaks CREATE TRIGGER, before it reaches the END statement (on the first semicolon, which, in your case, is DECLARE statement).
You can see the documentation for more details:
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.html
I am trying to create a simple Stored procedure that allows me to conduct mass inserts, However I am running into syntactical troubles and unable to figure out where what's going wrong, despite comparing my procedure syntax to existing examples, and it seems to be correct.
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END
Upon running, MYSQL states:
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
and highlits this guy:
CREATE PROCEDURE populateUserTable( ) BEGIN DECLARE counter INT( 10 ) ;
What's up here?
Have you used
DELIMITER $$
At the start?
Try
DELIMITER $$
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END $$
DELIMITER ;