I'm trying to create stored procedure but phpmyadmin saying I've syntax error in it but I can't see any 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 'CURSOR CUR_ID_FORUM
IS
SELECT ForumID FROM Forum' at line 3
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
CURSOR CUR_ID_FORUM
IS
SELECT ForumID FROM Forum //
IF(
previous_role=1,
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0,
SELECT ForumID FROM Forum
FOR REC_ID_FORUM IN CUR_ID_FORUM
LOOP
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
)
END //
delimiter ;
My updated code :
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
DECLARE REC_ID_FORUM INT(11) //
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
IF(
previous_role=1,
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0,
SELECT ForumID FROM Forum
FOR REC_ID_FORUM IN CUR_ID_FORUM
LOOP
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
)
END //
delimiter ;
and phpmyadmin's updated 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
UPDATED:
Your syntax is incorrect in many places. You need to declare the REC_ID_FORUM variable to use for your cursor fetch:
DECLARE REC_ID_FORUM INTEGER //
You need to use the correct cursor syntax of:
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
After you declare your cursor you need to open it:
OPEN CUR_ID_FORUM //
And in your if statement you need to FETCH from the cursor in your LOOP:
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
Your IF statement syntax is also wrong. You're using the IF function that is used in MySQL select statements, not the IF conditional used in stored procedures.
Here is what I think your entire code should look like:
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
DECLARE REC_ID_FORUM INTEGER //
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
IF previous_role = 1 THEN
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0 //
ELSE
OPEN CUR_ID_FORUM //
LOOP
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
END IF //
END //
delimiter ;
This is very much untested but should get you closer.
Related
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.
Write a script that creates and calls a stored procedure named test. This stored procedure should
declare a variable and set it to the count of all products in the Products table. If the count is greater
than or equal to 7, the stored procedure should display a message that says, “The number of products
is greater than or equal to 7”. Otherwise, it should say, “The number of products is less than 7”.
DROP PROCEDURE IF EXISTS test;
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id)
into count_of_7
FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
end if;
call test();
21:38:55 CREATE procedure test() BEGIN DECLARE count_of_7 DECIMAL(10,2) 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 '' at line 3 0.016 sec
You need to change the delimiter before declaring your procedure. Also, you are missing an END statement. This should work:
DROP PROCEDURE IF EXISTS test;
DELIMITER //
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id) into count_of_7 FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
END IF;
END //
DELIMITER ;
call test();
This question already has answers here:
syntax error for mysql declaration of variable
(2 answers)
Closed 4 years ago.
I'm trying to make a stored procedure that include a cursor inside it and fill one of my tables based on another table's data , every day .
I think I'm doing something wrong with syntax , I already wrote a simple Stored procedure with cursor and it worked totally right , but when it get a little more complicated it does not work any more .
I'm getting
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 'DECLARE brandId int ;' at line 1.
Please note that I'm using Mysql 5.7 and I'm creating this at phpmMyAdmin .
CREATE PROCEDURE ّFillCommentGrowth()
BEGIN
DECLARE brandId int;
DECLARE todayComment int ;
DECLARE brandCount int ;
DECLARE yesterdayComment int;
DECLARE crs CURSOR for SELECT id from brands;
SET brandCount = (SELECT count(*) from brands);
open crs;
WHILE brandCount > 0 DO
FETCH crs into brandId ;
set todayComment = (select IFNULL((select count(*) from comments as c where date(c.created_at) = date(subdate(NOW(),1)) and c.brand_id = brandId ),0));
set yesterdayComment = (select IFNULL((select commentAmount from commentsGrowth where moment = date(subdate(NOW(),2)) and brand_Ref= brandId),0));
INSERT INTO commentsGrowth
(
brand_Ref,
commentAmount,
diffrenceByYesterday,
degree,
AmountPercent,
moment)
VALUES
(brandId ,
todayComment,
(todayComment - yesterdayComment ) ,
(((ATAN(todayComment - yesterdayComment )*180))/PI()),
(degree*(1.1)),
date(subdate(NOW(),1)));
SET brandCount = brandCount - 1;
END WHILE;
close crs;
END
The error you are getting has nothing to do with cursor. You need to change the DELIMITER from standard semicolon (;). For example
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
The DELIMITER statement changes the standard delimiter which is semicolon ( ; ) to another. In this case, the delimiter is changed from the semicolon( ; ) to double-slashes //. Why do we have to change the delimiter? Because we want to pass the stored procedure to the server as a whole rather than letting mysql tool interpret each statement at a time. Following the END keyword, we use the delimiter // to indicate the end of the stored procedure. The last command ( DELIMITER; ) changes the delimiter back to the semicolon (;).
I need to modify all values in a column with a function.
I have wrote a function and added it to stored function.
I have wrote a stored procedure that will loop over all the values in the column with a cursor and switch the values.
This is a windows server running wamp, php 5.6, mysql 5.0
Tried ec2 linux server with mysql 5.6 as well.
DELIMITER $$
DROP PROCEDURE IF EXISTS `my_proc` $$
CREATE PROCEDURE `my_proc`()
BEGIN
DECLARE val1 INT DEFAULT NULL;
DECLARE val2 INT DEFAULT NULL;
DECLARE done TINYINT DEFAULT FALSE;
DECLARE cursor1 CURSOR FOR SELECT `col1` FROM `table1` WHERE 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor1;
my_loop:
LOOP
FETCH NEXT FROM cursor1 INTO val1;
IF done = TRUE THEN
LEAVE my_loop;
ELSE
SET val2 = CALL the_other_procedure1(val1);
UPDATE `table1`
SET `col1` = val2
WHERE `col1` = val1 ;
END IF;
END LOOP my_loop;
END $$
DELIMITER ;
I get 2 errors when running it in sql section of phpmyadmin:
1 errors were found during analysis.
Unexpected character. (near ":" at position 364)
and:
#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 'CALL the_other_procedure1(val1);
UPDATE `table1`
SET `col1` = val2
WHER' at line 25
Here is an example on update in the same table we read from.
Two remarks:
The statement FETCH NEXT FROM cursor1 INTO val1; should become just FETCH cursor1 INTO val1;;
and
The assignment SET val2 = CALL the_other_procedure1(val1); should be called without keyword CALL, i.e SET val2 = the_other_procedure1(val1);.
Also make sure you the_other_procedure1() is a function (i.e. it must return some value).
Read more about differences between stored procedures and functions.
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.