I'm trying to create a function in MySQL, but getting a syntax error. This is my function:
DELIMITER $$
CREATE FUNCTION gg ()
RETURNS char(8)
DETERMINISTIC
BEGIN
DECLARE cod varchar(8);
select RIGHT(MAX(idCurso),7) into cod from Curso;
IF cod is null then
set cod = 'C' + right('0000000' + convert(varchar(7),1),7);
else
set cod = 'C' + right('0000000' + convert(varchar(7), @id +1),7)
return cod;
END$$
DELIMITER ;
I am getting this error in the set cod
after the if cod is null then
:
"cod" is not valid at this position, expecting an identifier
What am I doing wrong?
There are many issues with your function.
+
icon doesn't perform concatenation in MySQL, it is used for sum. Use CONCAT or CONCAT_WS for the same. CONVERT
. Please see documentation CONVERT 0
to the left of your integer value and make it length to 7
. @a
, don't know from where you're accessing it, replaced it with cod
value and adding 1 to it. END IF
clause. DELIMITER $$
CREATE FUNCTION gg () RETURNS CHAR(8) DETERMINISTIC
BEGIN
DECLARE cod VARCHAR(8);
SELECT
RIGHT(MAX(idCurso),7) INTO cod
FROM Curso;
IF cod IS NULL THEN
SET cod = CONCAT('C', LPAD(1, 7, '0'));
ELSE
SET cod = CONCAT('C', LPAD(cod + 1, 7, '0'));
END IF;
RETURN cod;
END$$
DELIMITER ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.