mysql procedure using cursor - mysql

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

Related

Nested Cursor Declare Issue Mysql

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.

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;

mysql create stored procedure . what wrong with my code?

Error
SQL query:
CREATE PROCEDURE GEN_MFREE( ) BEGIN ;
MySQL said: Documentation
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 2
what wrong with my code ?
at below is my code :
CREATE PROCEDURE GEN_MFREE()
BEGIN
DECLARE CODE VARCHAR (10);
DECLARE BLOCK VARCHAR (10);
DECLARE UNIT VARCHAR (10);
DECLARE FLOOR VARCHAR (10);
DECLARE FIRSTNAME VARCHAR(10);
DECLARE LASTNAME VARCHAR(10);
DECLARE AMT DECIMAL(18,2) ;
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_i CURSOR FOR SELECT
B_resident.CODE,
B_resident.BLOCK,
B_resident.UNIT,
B_resident.FLOOR,
B_resident.FIRSTNAME,
B_resident.LASTNAME,
B_resident.TEL,
B_ResManFree.SIZE * B_ResManFree.FREE AS AMT,
'2016-01-01' AS MDATE
FROM B_resident LEFT OUTER JOIN B_ResManFree ON
B_resident.UNIT = B_ResManFree.UNIT AND
B_resident.BLOCK = B_ResManFree.BLOCK
WHERE B_resident.MAIN_CONT ='YES'
ORDER BY B_resident.BLOCK,B_resident.FLOOR,B_resident.UNIT
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_i;
read_loop: LOOP
FETCH cursor_i INTO CODE, BLOCK, UNIT, FLOOR, FIRSTNAME, LASTNAME, AMT ;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO B_MfreeStatment(RES_CODE,
RES_BLOCK,
RES_UNIT,
RES_FLOOR,
BAN_CODE,
RES_FIRSTNAME,
RES_LASTNAME,
AMT,MDATE)
VALUES( CODE, BLOCK, UNIT, FLOOR,'001' FIRSTNAME, LASTNAME, AMT,'2016-01-01' );
END LOOP;
CLOSE cursor_i;
END;
;;
You miss to set the delimiter first:
Delimiter //
--> Your Code
--> End Code with // instead of ;;
Delimiter ;

Cursor to copy distinct record from one table to another

I want to copy all recoreds from temp1 table to anoter two tables I am using cursor for this .
DELIMITER //
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
declare tcursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
OPEN tcursor;
REPEAT
FETCH cursor into v_fn,v_ln, v_email;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fname,v_lname,v_email);
END REPEAT;
CLOSE tcursor;
END//
DELIMITER
this code is showing error
MySQL said: Documentation
#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 'for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLE' at line 8
How to resolve this
You have multiple errors in your syntax and don't exit the loop. Try this?
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
DECLARE done INT DEFAULT FALSE;
declare tcursor cursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tcursor;
read_loop: LOOP
FETCH tcursor into v_fn,v_ln, v_email;
if done then
LEAVE read_loop;
END IF;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fn,v_ln,v_email);
END LOOP;
CLOSE tcursor;
END
I tried this query and find this is working
insert into atom(id,type) select id,'Person' from user1;
INSERT INTO user( id, fname, lname, mailid ) SELECT id, fname, lname, mailid FROM user1;

if ... then stored procedure

I have an error in this stored procedure:
Script line: 4 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 'INSERT INTO escuelas_asignacion_maestro (idEscuela,
idCiclo, `idGrado' at line 31
Code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `zz73ff`.`pr_trasladar_asignaciones_por_ciclo` $$
CREATE PROCEDURE `zz73ff`.`pr_trasladar_asignaciones_por_ciclo`
(IN p_ciclo int, IN n_ciclo int)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE escuela, ciclo, grado, nGrupos, maestro INT;
DECLARE fecha datetime;
DECLARE cursor_asignaciones CURSOR FOR
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo
AND idEstatus = 1
ORDER BY idEscuela, idGrado;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SELECT now() INTO fecha;
OPEN cursor_asignaciones;
REPEAT
FETCH cursor_asignaciones INTO escuela, ciclo, grado, nGrupos, maestro;
IF grado > 1 THEN
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro INTO
escuela, ciclo, grado, nGrupos, maestro
FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo AND idEstatus = 1 AND idEscuela = escuela
AND idGrado = (grado - 1);
END IF
INSERT INTO `escuelas_asignacion_maestro`
(`idEscuela`, `idCiclo`, `idGrado`, `numeroGrupos`, `idEstatus`, `idMaestro`, `fechaModificacion`)
VALUES (escuela, n_ciclo, grado, nGrupos, 1,0, fecha);
UNTIL done END REPEAT;
CLOSE cursor_asignaciones;
END $$
DELIMITER ;