What's wrong w/ MySql Statement (Function) - mysql

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.

Related

Declare inside if condition showing syntax error - MYSQL Triggers

I try to update some columns using trigger before insert
DROP TRIGGER IF EXISTS update_p_posts_places;
DELIMITER $$
CREATE TRIGGER update_p_posts_places BEFORE
INSERT
ON
`p_posts` FOR EACH ROW
BEGIN
DECLARE
p_post_group_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
BEGIN
DECLARE
place1_id_ int;
place2_id_ int;
place3_id_ int;
place4_id_ int;
place5_id_ int;
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
END $$
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
DELIMITER ;
It's showing some syntax errors.
#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
place1_id_ int;
place2_id_ int;
' at line 20
Here is a working trigger. I tested creating it on MySQL 5.7.
CREATE TRIGGER update_p_posts_places BEFORE INSERT ON `p_posts`
FOR EACH ROW
BEGIN
-- all declarations must be before any other statements
DECLARE p_post_group_id_, place1_id_, place2_id_, place3_id_,
place4_id_, place5_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
You can use one DECLARE for multiple local variables, but you must do like var1, var2, var3, ... int. In other words, name the type only once at the end. See documentation: https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
No need for the BEGIN..END inside the IF and definitely do not use $$ until after the last END because that will terminate the parser's interpretation of your whole CREATE TRIGGER statement before it's complete.

MySQL Error 1064: You have an error in your SQL syntax

DELIMITER //
CREATE FUNCTION fnc_credit_custstatus
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>='1000',THEN SET custstatus='VIP';
ELSEIF CustCredit<'1000',THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
1 queries executed, 0 success, 1 errors, 0 warnings
Query:
CREATE function fnc_credit_custstatus returns varchar(6) deterministic
begin DECLARE custstatus VARCHAR(6); if CustCredit>='1000...
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 'varchar(6) deterministic
begin
DECLARE custstatus VARCHAR(6);
if CustCredit>=' at line 2
Try remove comma
DELIMITER //
CREATE FUNCTION fnc_credit_custstatus
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
(and use proper comparision data type)

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 ;

Create MySQL function statement

I've written a function but it gives me mistake a the second line (create statement) if anyone could help me, I really appreciate:
CREATE FUNCTION GetPrefix (phone_num VARCHAR(30)) RETURNS varchar(30)
deterministic
BEGIN
DECLARE x INT;
DECLARE prefix varchar(30);
SET x = 0;
for prefix in SELECT code
FROM tab_len
while (length(phone_num)) > 0
do
if prefix<>left(phone_num, length(phone_num)-x)
then set x=x+1 ;
else return 1 ;
END while ;
END $$;
and I receive this error :
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 prefix in SELECT code
FROM tab_len while (length(phone_n' at line 9
DELIMITER $$
DROP FUNCTION IF EXISTS GetPrefix $$
CREATE FUNCTION GetPrefix
(
phone_num VARCHAR(30)
)
RETURNS varchar(30)
BEGIN
DECLARE var_x INT DEFAULT 0;
DECLARE var_prefix VARCHAR(100);
SET phone_num = IFNULL(phone_num,'');
-- your logic will go here.
return phone_num;
END$$
DELIMITER ;
SELECT GetPrefix('test');
This is right syntax to write a function in mysql.
check out the differences. Take a look Here

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 ;