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;
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.
Getting error on following trigger:
DELIMITER $$
CREATE TRIGGER limit_refferals AFTER INSERT
ON wpmr_aff_referrals
FOR EACH ROW
BEGIN
DECLARE vCNT INT;
DECLARE USERID varchar(50);
DECLARE AFFILIATEID varchar(50);
DECLARE i INTEGER;
DECLARE curs1 CURSOR FOR SELECT USER_ID,affiliate_id
FROM `wpmr_aff_referrals` WHERE affiliate_id=:NEW.affiliate_id;
SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3)
set i=1;
OPEN curs1;
read_loop: LOOP
FETCH curs1 INTO USERID,AFFILIATEID;
SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3)
set i=i+1;
ELSE
set new.affiliate_id= AFFILIATEID;
END IF;
END LOOP read_loop;
CLOSE curs1;
END IF;
END$$
DELIMITER ;
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 ':NEW.affiliate_id; SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO
vCNT;
IF (vC' at line 10
DELIMITER $$
CREATE TRIGGER limit_refferals BEFORE INSERT
ON wpmr_aff_referrals
FOR EACH ROW
BEGIN
DECLARE vCNT INT;
DECLARE USERID varchar(50);
DECLARE AFFILIATEID varchar(50);
DECLARE i INTEGER;
DECLARE curs1 CURSOR FOR
SELECT USER_ID,affiliate_id
FROM `wpmr_aff_referrals` WHERE affiliate_id=NEW.affiliate_id;
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
IF (vCNT >=3) THEN /*<------------Made changes at this line*/
set i=1;
OPEN curs1;
read_loop : LOOP
FETCH curs1 INTO USERID,AFFILIATEID;
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3) THEN /*<------------Made changes at this line*/
set i=i+1;
ELSE
set new.affiliate_id= AFFILIATEID;
END IF;
END LOOP read_loop;
CLOSE curs1;
END IF;
END$$
DELIMITER ;
Try above code.
One more thing you can't use NEW row for BEFORE UPDATE TRIGGER.So i had made changes to AFTER UPDATE TRIGGER.
We don't need : for NEW and OLD value,you can directly check that value using NEW.VAL and OLD.VAL.
Also whenever you use IF IN TRIGGER,PROCEDURE and FUNCTION put THEN and then write you logic
Hope this will help you.
Remove the ":" to pass the parameter NEW.affiliate_id to the CUSTOMERLEVEL function like this:
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT
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.
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.
DELIMITER $$
CREATE PROCEDURE INSERT_NONE_HISTORY_CHECKBOX()
BEGIN
DECLARE note_id bigint(20);
FOR c1 IN
(SELECT question_id
FROM question_master
WHERE question_type LIKE '%check box%')
LOOP
SELECT note_section_id INTO note_id
FROM answer_master
WHERE question_id = c1.question_id
LIMIT 1;
INSERT INTO answer_master(QUESTION_ID, NOTE_SECTION_ID, ANSWER_TEXT
, ROS_INPUT_TEXT, HAS_CHILD_QUES, MEDICATIONS_LIST_ID, STATUS_CODE)
VALUES(c1.question_id,note_id,'none',null,0,null,1);
END LOOP;
END $$
DELIMITER ;
i am getting error like ::
Script line: 3 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 'for c1 in (select question_id >from question_master where question_type like '%ch' at line 6
What am I doing wrong?
I don't think MySQL supports the FOR IN syntax you'll have to declare a cursor and loop using that.
DELIMITER $$
CREATE PROCEDURE INSERT_NONE_HISTORY_CHECKBOX()
BEGIN
DECLARE note_id bigint(20);
DECLARE Myquestion_id INTEGER;
DECLARE done BOOLEAN DEFAULT 0; //loop variable
DECLARE cur1 CURSOR FOR
SELECT question_id
FROM question_master
WHERE question_type LIKE '%check box%'; //declare the cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; //stop when done.
OPEN cur1; //Open it.
insert_loop: LOOP
FETCH cur1 INTO myquestion_id;
IF done THEN LEAVE insert_loop; END IF;
SELECT note_section_id INTO note_id
FROM answer_master
WHERE question_id = c1.question_id
LIMIT 1;
INSERT INTO answer_master(QUESTION_ID, NOTE_SECTION_ID, ANSWER_TEXT
, ROS_INPUT_TEXT, HAS_CHILD_QUES, MEDICATIONS_LIST_ID, STATUS_CODE)
VALUES(myquestion_id,note_id,'none',null,0,null,1);
END LOOP;
CLOSE cur1;
END $$
DELIMITER ;
The syntax is a bit cumbersome, but this should work.
See: http://dev.mysql.com/doc/refman/5.0/en/cursors.html