How to and use variables in MySQL - 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;

Related

What's wrong w/ MySql Statement (Function)

I'm using MySql57
What's wrong with this script?(I'm Mysql newbie)
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 '= 'A0001'; ELSE BEGIN gubun1 = LEFT(cur_max,0,1); gu' at line 10
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE cur_max varchar(100);
DECLARE gubun1 varchar(1);
DECLARE gubun2 varchar(100);
DECLARE RTN_VAL varchar(100);
SELECT IFNULL(MAX(gubun_code),'A0001') INTO cur_max from gubun_code;
IF cur_max = 'A0001' THEN RTN_VAL = 'A0001';
ELSE
BEGIN
gubun1 = LEFT(cur_max,0,1);
gubun2 =
LPAD(CONVERT(CONVERT(RIGHT(cur_max,4),UNSIGNED)+1,CHAR),4,'0');
RTN_VAL = CONCAT(gubun1,gubun2);
END
RETURN RTN_VAL;
END $$
DELIMITER ;
There are several errors in the function. I think this might work:
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE v_cur_max varchar(100);
DECLARE v_RTN_VAL varchar(100);
SELECT COALESCE(MAX(gubun_code), 'A0001')
INTO v_cur_max
FROM gubun_code;
IF cur_max = 'A0001' THEN
SET v_RTN_VAL = 'A0001';
ELSE
SET v_RTN_VAL = CONCAT(LEFT(cur_max, 1), RIGHT(cur_max, 4) + 1);
END IF;
RETURN v_RTN_VAL;
END $$
DELIMITER ;
I would further simplify this by doing all the logic in the SQL statement, but this seems to be in the spirit of your solution.

MySQL Syntax error while creating stored function

I am getting a syntax error on executing the below function in MySQL.
CREATE FUNCTION FABC (P_MEMBER_EMAIL VARCHAR(30))
RETURNS int
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END;
The error is as follows "#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 4
Not sure what's causing this. Can anyone help me ?
You need to set a delimiter that will tell MySQL the END of the function definition.
DELIMITER $$
CREATE FUNCTION `FABC`(`P_MEMBER_EMAIL` VARCHAR(30)) RETURNS int(11)
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END$$
DELIMITER ;

MySQL cursor declaration with multiple joins

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

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 ;

Getting a MYSQL 1064 error when creating a function

DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn int)
RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;
$$
DELIMITER;
If I try to run this all I get 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
'RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = select count(' at line 2
A couple of changes to resolve the problem:
DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn INT)
-- RETURN INT
RETURNS INT
BEGIN
DECLARE intOut INT;
/*SET intOut = SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;*/
SET intOut = (SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn);
RETURN intOut;
END $$
DELIMITER ;
Defining the return type is done by the RETURNS keywrod, not the RETURN keyword:
CREATE FUNCTION nameOfFunct(intIn int)
RETURNS int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;