I've been searching for 20 minutes why I get this error in mySql but couldn't find an answer.
"#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 5 "
Here is the code block in question:
CREATE PROCEDURE marouri_insert_users_emails()
BEGIN
DECLARE a INT;
DECLARE b char(16);
DECLARE cur1 CURSOR FOR SELECT id,name FROM glpi_users;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a,b;
IF a > 6 THEN
INSERT INTO glpi_useremails(users_id,is_default,is_dynamic,email) VALUES (a,1,0,CONCAT(b, '#alomrane.ma');
END IF;
END LOOP;
CLOSE cur1;
END;
New to mysql btw. Thanks in advance.
For multiple statements in a procedure or function or trigger, you must set another delimiter than ;. Otherwise MySQL thinks, that your procedure is finished after the first ;, which leads to the syntax error. Try it like this:
DELIMITER $$
CREATE PROCEDURE marouri_insert_users_emails()
BEGIN
DECLARE a INT;
DECLARE b char(16);
DECLARE cur1 CURSOR FOR SELECT id,name FROM glpi_users;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a,b;
IF a > 6 THEN
INSERT INTO glpi_useremails(users_id,is_default,is_dynamic,email) VALUES (a,1,0,CONCAT(b, '#alomrane.ma');
END IF;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
Oh, and you might want to declare a continue handler to handle the situation when the cursor doesn't find more rows. Please see the according manual page for examples.
Related
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 am trying to make my first cursor in MySQL and I am receiving an error. It says incorrect integer value. I was thinking this would grab the value in row one from column customer_Id, and store it into the IdValue variable. How do I code this correctly and fix this error?
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR
SELECT customer_Id FROM customers;
OPEN myCursor;
FETCH myCursor INTO IdValue;
CLOSE myCursor;
SELECT IdValue;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS CursorProcedure;
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR SELECT customer_Id FROM customers;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN myCursor;
read_loop: LOOP
FETCH myCursor INTO IdValue;
IF done THEN
LEAVE read_loop;
END IF;
--
-- YOU ARE IN YOUR READ LOOP
-- DO SOMETHING WITH IT HERE
--
END LOOP;
CLOSE myCursor;
END$$
DELIMITER ;
But in the "do something with it here", don't do a SELECT on it because it will generate multiple result sets. Do something meaningful.
Manual page on Cursors.
As an aside, cursors are rarely your friends. They are extremely slow. Use them in dire emergencies only.
It's my first time working with cursors on MySQL, and I am having some problems, it doesn't accept the declaration of my cursor, what's wrong with my code? it shows me this error messagae:
#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 #MyCursor CURSOR' at line 1
and my code is:
DECLARE #MyCursor CURSOR;
DECLARE #MyField varchar2(255);
BEGIN
SET #MyCursor = CURSOR FOR
SELECT codeMaint from affectationmc where iduser=29;
OPEN #MyCursor
FETCH NEXT FROM #MyCursor
INTO #MyField
WHILE ##FETCH_STATUS = 0
BEGIN
delete from mcorr where codemaint=#MyField ;
FETCH NEXT FROM #MyCursor
INTO #MyField
END;
CLOSE #MyCursor ;
DEALLOCATE #MyCursor;
END;
here is the template I use for cursors. It just taking the items from the select and maps them into the variables declared up top and executes your statement. Once it processes the statement in the section --Actions to Loop Here, it changes the variables by selecting the next row from your select statement. This repeats until it process all of the rows in the select.
DECLARE
#variable_A AS varchar(15)
,#variable_B AS INT
,#variable_C AS INT
,#variable_D AS VARCHAR(255)
DECLARE Process_Name CURSOR LOCAL FAST_FORWARD FOR
SELECT
T.Col_A,T.Col_B,T.Col_C,T.Col_D
FROM
tab.my_table AS T;
OPEN Process_Name
FETCH NEXT FROM Process_Name INTO #variable_A,#variable_B,#variable_C,#variable_D
WHILE ##FETCH_STATUS = 0
BEGIN
--Actions to Loop Here
FETCH NEXT FROM Process_Name INTO #variable_A,#variable_B,#variable_C,#variable_D
END
CLOSE Process_Name
DEALLOCATE Process_Name
You're attempting to declare your cursor in one statement, then associate the query with it in a second statement. While some databases will allow this, I'm fairly sure MySQL isn't one of them. Also, MySQL is quite strict about not declaring cursors until after all variables have been declared.
Here's how I would do it:
BEGIN -- note all declarations are inside the BEGIN/END
DECLARE done INT DEFAULT FALSE; -- Good practice is to avoid global variables whenever possible
DECLARE MyField varchar(255); -- varchar2 is Oracle, not MySQL
DECLARE MyCursor CURSOR FOR
SELECT codeMaint
FROM affectationmc
WHERE iduser = 29;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE; -- this will detect end-of-result-set
OPEN MyCursor;
read_loop: LOOP
FETCH NEXT
FROM MyCursor
INTO MyField;
IF done THEN
LEAVE read_loop;
END IF;
DELETE FROM mcorr
WHERE codemaint = MyField ;
END LOOP;
CLOSE MyCursor;
END;
I cannot copy stored procedure from MySQL 5.0.5 to MySQL 5.0.1.
Although I can copy all the tables. When I copy stored procedure it gives 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 'PROCEDURE `insertAccumCursor`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLAR' at line 1
the stored procedure on 5.0.51b-community-nt is
CREATE DEFINER=`nobody`#`localhost` PROCEDURE `insertAccumCursor`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE userid INT;
DECLARE counter INT;
DECLARE cur1 CURSOR FOR SELECT id FROM users WHERE InstitutionID='ReportTest';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
Set counter = 1;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO userid;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO myeltaccumgradebook VALUES (counter +12000,0, userid, 'ReportTest', 576389201, 0,1,1,1,1,CEIL(RAND() * 99) +50, '2013-07-10 16:10:54','2013-07-10 16:10:54', 0);
set counter = counter+1;
END LOOP;
CLOSE cur1;
END
How do you make procedure insert in new DB? With which tool? If you use phpmyadmin I can suggest you to save stored procedure to file and try to import it with cli
I am running the following command in PHPMyAdmin:
DELIMITER #
CREATE PROCEDURE addid()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a,b FLOAT DEFAULT 0;
DECLARE c,d INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT time FROM results;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
IF a - b > 60 THEN
SET c = c+1;
ELSE
UPDATE results SET uid=c WHERE time=a;
END IF;
SET b = a;
END LOOP;
CLOSE cur1;
END#
DELIMITER ;
CALL addid();
Maybe it will do what I want, maybe not. But I don't know because I can't call it.
I get the following 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 'LOOP; CLOSE cur1; END# call addid()' at line 20
Sometime, depending on how I tweak it, I can get the create to run but then it cant find the stored procedure, like it just does not get made.
This is my first time ever using stored procedures so it's probably something stupid. I'm running MySQL 5.0.7.
Just now I saw what seems to be the problem in your code....
IF done THEN
LEAVE read_loop;
END IF;
The endif is missing.