Strange error near '' MySQL with a function - mysql

I ask because I don't know where is the error.
I need to create a function in MySQL. I'm using PhpMyAdmin.
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150)
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso')
IF(hapermesso < 1) BEGIN
RETURN 'Errore. Non hai i permessi.';
END
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`) VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso) RETURN 'Nuova operazione aggiunta con successo.';
END
DELIMITER;
The error is
#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 3
What is the error?

The error was because you were using wrong syntax for Return and IF. I have corrected your query and it works fine now.
DELIMITER $$
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150) CHARSET latin1
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene
WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso');
BEGIN RETURN IF(hapermesso > 1,'Errore. Non hai i permessi.','None');
END;
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`)
VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso);
RETURN 'Nuova operazione aggiunta con successo.';
END$$
DELIMITER ;

Related

MySQL CREATE FUNCTION with email parameter with IF SELECT as return

I receive the following error when using the below function and can not figure out how to fix it.
Error:
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 ';
RETURN IF(result IS NOT NULL, result, '0');
END' at line 8
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(255);
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Email would be read in as a varchar or text such as "user#domain.com"
expiration_date is a of type TIMESTAMP
payer_email is of type VARCHAR(255)
I've also tried the following function with a different error being shown.
Error:
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 ';
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET res' at line 2
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS TIMESTAMP;
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
I want to use either of these functions so that an application can just pass in expiration(email); and get a result if the email is of length 9 or greater or otherwise receive nothing.
UPDATE:
Thank you KAILLINGER. Spent 2 hours working on this, and it was a typo! :(
Correct answer for completeness:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(200)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(200);
SET result = NULL;
IF (LENGTH(email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
END IF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Your ENDIF; is not good, it should be END IF; :)

MySQL Create function returns error

I am trying to create a function that returns rowcount. But it returns error again and again.
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 11
DELIMITER $$
CREATE FUNCTION func1(userid INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE var_name INT;
SET var_name = 0;
SELECT COUNT(*) INTO var_name
FROM wps_bal
WHERE u_id = userid;
RETURN var_name;
END$$
Most probably your version of PHPMyAdmin does not support the DELIMITER statement which is not MySQL statement. Here you can find how to create the function in PHPMyAdmin: Store procedures in phpMyAdmin
Yes. This was helpfull
I have created the solution:
DELIMITER $$
CREATE FUNCTION func1(userid INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE var_name INT;
SET var_name = 0;
SELECT COUNT(*) INTO var_name
FROM wps_bal
WHERE u_id = userid;
RETURN var_name;
END$$
DELIMITER ;

Syntax error in sql create function

I'm getting the following error whenever i'm trying to create the following function in mysql.
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 '' at line 4
And my function is
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
RETURN result;
END
You need to change the delimiter before creating a function. If you don't your CREATE statement terminates at the first semi-colon.
Try this:
DELIMITER $$
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
END IF
RETURN result;
END$$
DELIMITER ;
Thanks to #Ravinder for catching the missing END IF.

Unable to CREATE FUNCTION in MYSQL ERROR 1064 (42000)

Hi I'm using MySQL to create a function:
CREATE FUNCTION INSERTGROUP(name VARCHAR(50))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE 'idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO 'idGroup' FROM groupsTable WHERE groupName='name';
RETURN 'idGroup';
END//
But I get this error when I try to create 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
''idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM serverGroupsTable WHERE gro' at line 5
I've been through forums and other questions similar to this one but I can not make it create the function.
What I'm doing wrong? Is the syntax correct? Do I need to add something else?
Ok this is what I've try:
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = name))
THEN INSERT INTO groupsTable (groupName) VALUES (name);
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName=name;
RETURN idGroup;
END//
and
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName='name';
RETURN idGroup;
END//
and for the last two I got this error:
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 13
Similar to the one above
Also I do DELIMITER // to change it from ;
remove the quotes from the declaration
DECLARE idGroup INT;
and you forgot the delimiter change at the start of the function
delimiter //
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
and you forgot to end your if statement
end if;
whereever you want it to end

Can't create stored function - wrong syntax?

I've got the following Problem.
I want to create a stored function which converts an entity_id into the corresponding sku
Here's the code:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
Now my problem is if i fire the query i get the following message:
[Window Title]
Error
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 'LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT' at line 3
The db im using is MySQL 5.0.51a
Thanks in advance for your ideas.
MySQL by default uses ; as a delimiter, so when it encounters the ; at line 9:
DECLARE returnvalue varchar(50);
MySQL thinks the command ends - it is trying to execute:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
which isn't valid SQL.
Set a new delimiter:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
$$
You can then change the delimiter back with:
DELIMITER ;
This should work:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue VARCHAR(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
RETURN returnvalue;
END$$
DELIMITER ;
You had not specified the varchar length. :-)