Stored procedure in MySQL - mysql

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.

Related

Getting Syntax Error in MySQL Stored Procedure while trying nested Cursors

I have a MySQL Stored Procedure Script in which I am trying to implement a nested cursors . Following is my code
DELIMITER //
DROP PROCEDURE IF EXISTS PopulateStaleEntityTable//
DELETE FROM bucket_stale;
LOAD DATA LOCAL INFILE '/home/centos/mysql1.txt' INTO TABLE bucket_stale;
CREATE PROCEDURE PopulateStaleEntityTable()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE finished_stale INTEGER DEFAULT 0;
DECLARE product_offer_id_var bigint(20) DEFAULT 0;
DECLARE product_offer_group_id_var bigint(20) DEFAULT 0;
DECLARE batch_count INTEGER DEFAULT 0;
DECLARE max_batch_count INTEGER DEFAULT 100;
DECLARE bucket_id_var bigint(10) DEFAULT 0;
DECLARE stale_cursor CURSOR FOR
SELECT staleid FROM bucket_stale;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished_stale = 1;
OPEN stale_cursor;
insert_stale : LOOP
FETCH stale_cursor INTO bucket_id_var;
SELECT bucket_id_var;
BEGIN
DECLARE product_cursor CURSOR FOR
SELECT product_offer_id FROM product where bucket_id='1086';
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN product_cursor;
insert_entity: LOOP
START TRANSACTION;
FETCH product_cursor INTO product_offer_id_var;
SELECT product_offer_group_id INTO product_offer_group_id_var FROM product_offer where id=product_offer_id_var;
INSERT IGNORE INTO stale_cached_entity (entity_type,entity_id,cache_action,created,field_change) VALUES ('PRODUCT_OFFER_GROUP',product_offer_group_id_var, 'UPDATE',now(),'DEFAULT_SUPC_LIST1');
SET batch_count=batch_count+1;
IF batch_count > max_batch_count THEN
COMMIT;
SET batch_count=0;
END IF;
IF finished = 1 THEN
LEAVE insert_entity;
END IF;
END LOOP insert_entity
END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF;
END LOOP insert_stale;
CLOSE stale_cursor;
END//
DELIMITER ;
When i try and run this script it gives the following 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 'END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF; ' at line 38
I tried to figure out whats wrong but i couldn't . What am I missing ?
The problem lies in this line:
END LOOP insert_entity
If you review the example of cursor code at https://dev.mysql.com/doc/refman/5.7/en/cursors.html you see that END LOOP does not need the label, and it needs a semicolon statement terminator.
Like this:
END LOOP;
I agree with #tadman's comment. I understand some developers who have had experience with Oracle or Microsoft have an expectation of using stored procedures, but frankly, MySQL's stored procedure support sucks so bad you will be happier and more productive writing some simple Python scripts (or any other language).

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;

error code #1064 while Declaring Cursor in MySQL

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;

Mysql stranger syntax error #1064

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.

Copy stored procedure from MySQL5.0.5 to MySQL 5.0.1

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