I'm new to MariaDB, I only have some experience in MS SQL Server.
I'm trying to call a procedure for every row returned from a query.
But I can't find the error when trying to create a procedure with a cursor.
I'm using Heidy to connect to MariaDB 10.0
CREATE PROCEDURE SetAll()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE id INT ;
DECLARE cur CURSOR FOR SELECT aID FROM Customers ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO id;
IF done THEN
LEAVE testLoop;
END IF;
CALL SetById(id);
END LOOP testLoop;
CLOSE cur;
END
Error: "/* Error de SQL (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 4 */"
My precedure SetById(id) work just fine.
Related
I'm trying to call a stored procedure inside another stored procedure.
I'm getting a syntax error in the statement where main procedure calls the child procedure.
here is my query
DROP PROCEDURE GetAllProducts;
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
DECLARE finished INT DEFAULT 0;
DECLARE myName varchar(60);
DEClARE myCursor CURSOR FOR
SELECT Name FROM Test;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished =1 ;
OPEN myCursor;
get_email: LOOP
FETCH myCursor INTO myName;
IF finished = 1 THEN
LEAVE get_email;
END IF;
-- Line which shows a syntax error
EXEC UpdateProdcut myName;
END LOOP get_email;
CLOSE myCursor;
DEALLOCATE myCursor;
END //
DELIMITER ;
This is the error message
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 'UpdateProdcut myName OUTPUT;
mysql version is 5.5.54
any idea about this ?
Thanks.
Use CALL instead:
call UpdateProdcut(myName);
execute is used for prepared statements.
Ref:
https://dev.mysql.com/doc/refman/5.5/en/call.html
I have been breaking my head over this problem. Could not find any answer yet on SO, so I would be greatful if anyone could help me out!
The following query results in a suntax error. Any clue what's wrong?
BEGIN
DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrape = FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
DECLARE done INT DEFAULT 0;
read_loop: LOOP
FETCH cur INTO record;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE lifetimes SET end_time=extract(epoch from now()) WHERE object_id=record.object_id AND end_time IS NULL;
END LOOP;
CLOSE cur;
END
The syntax error:
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 'DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
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 'DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
Thank you all for your help! the epoch stuff is indeed postgresql, have changed that. #solarflare was right with the create_procedure remark, that did the trick eventually!
CREATE PROCEDURE set_end_time (out done int)
BEGIN
DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrape = FALSE;
OPEN cur;
LOOP
UPDATE lifetimes SET end_time=UNIX_TIMESTAMP(NOW()) WHERE hemnet_id=cur.hemnet_id AND end_time IS NULL;
END LOOP;
CLOSE cur;
END
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.
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 trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
SQL 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 5.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.