MySQL cursor declaration with multiple joins - mysql

I am attempting to declare a cursor with multiple joins in a stored procedure. The query runs perfectly outside of the stored procedure, but the stored procedure gives me an error at the cursor declaration, claiming there is a syntax error.
DROP PROCEDURE IF EXISTS getCheaters;
DELIMITER $$
CREATE PROCEDURE getCheaters()
BEGIN
DECLARE id INT (11);
DECLARE first_name VARCHAR (255);
DECLARE last_name VARCHAR(255);
DECLARE file_name VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE userCursor FOR
SELECT last_name, first_name, users.id
FROM users JOIN documents ON (users.id = documents.user_id)
JOIN licenses ON (licenses.user_id = users.id)
WHERE multi_user_license_id IS NULL
GROUP BY last_name, first_name
HAVING count(documents.title) > 60;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN userCursor;
SELECT FOUND_ROWS() INTO num_rows;
read_loop: LOOP
/*Do stuff*/
IF no_more_rows THEN
CLOSE userCursor;
LEAVE read_loop;
END IF;
SET loop_cntr = loop_cntr + 1;
END LOOP;
END $$
DELIMITER;
And I get 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 'DELIMITER' at line 1
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 'FOR
(SELECT last_name, first_name, users.id
FROM users JOIN docu' at line 13
Does any one see where my error is?

Change:
...
DECLARE userCursor FOR
...
by:
...
DECLARE userCursor CURSOR FOR
...
SQL Fiddle demo

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 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 ;

How to and use variables in MySQL

DELIMITER $$
CREATE PROCEDURE usp_SetGems (p_requestid int, p_akcija int)
BEGIN
if(p_akcija=0)
then
declare v_userId int;
declare v_vingems decimal;
SELECT r.user_id INTO v_userId FROM Requests r WHERE r.Id=p_requestid;
end
$$
delimiter;
/* 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 'declare v_userId int;
declare v_vingems decimal;
SELECT r.user_id INTO v_u' at line 8 */
Don't know where the problem is...
First declare all the variables than use if condition like this:
CREATE PROCEDURE usp_SetGems (p_requestid int, p_akcija int)
BEGIN
declare v_userId int;
declare v_vingems decimal;
if(p_akcija=0) then
SELECT r.user_id INTO v_userId FROM Requests r WHERE r.Id=p_requestid;
end
$$
delimiter;

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 ;

stored procedure error message

I have implemented the stored procedure as below, however i am getting the following error message when i try applying it:
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 'WHERE symbol_id = id GROUP BY symbol_id;
I did some debugging and found that it was caused by the #max variable which i am trying to write the result into, however i do not see anything wrong with the syntax, can anyone please advise?
DROP PROCEDURE IF EXISTS `GENERATE_REPORT`;
DELIMITER $$
CREATE DEFINER=CURRENT_USER PROCEDURE `GENERATE_REPORT`()
BEGIN
DECLARE id INT;
DECLARE max INT;
DECLARE at_end BIT DEFAULT 0;
DECLARE cur CURSOR
FOR SELECT symbol_id from trade;
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000' SET at_end=1;
OPEN cur;
FETCH cur INTO id;
WHILE (NOT at_end) DO
SELECT SUM(quantity) FROM trade INTO **#max** WHERE symbol_id = id GROUP BY symbol_id;
FETCH cur into id;
END WHILE;
CLOSE cur;
END
$$
DELIMITER ;
You have incorrect syntax in your SELECT ... INTO:
Change
SELECT SUM(quantity)
FROM trade
INTO #max -- Incorrect placement
WHERE symbol_id = id
GROUP BY symbol_id;
To
SELECT SUM(quantity)
INTO #max -- Correct placement
FROM trade
WHERE symbol_id = id
GROUP BY symbol_id;
The INTO should come right after the SELECT and before the FROM