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
Related
I have a stored procedure that checks whether or not a new entry is already existing in the table. if it exists, the insert will not happen. when I run it, there is an error
DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$
CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;
IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END
here is the error
Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...
Error Code: 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 'SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8
1) You cannot declare a select statement - a declare has to be for a variable..(and I would not use an output parameter for that) 2) or you can use exists instead
if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;
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 ;
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
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 ;
I'm creating a stored function like this
CREATE FUNCTION getVendorID(IN venname VARCHAR(255))
RETURNS INT
BEGIN
DECLARE a INT;
SELECT vendorid FROM vendors WHERE vendorname LIKE venname INTO a;
RETURN a;
END$$
but I receive an 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 'IN venname VARCHAR(255))
RETURNS INT
BEGIN
DECLARE a INT;
SELECT vendorid FRO' at line 1
MySQL functions only takes IN-parameters, and therefor they cannot be declared as IN.
DELIMITER $$
CREATE FUNCTION getVendorID(venname VARCHAR(255))
RETURNS INT
BEGIN
DECLARE a INT;
SELECT vendorid INTO a FROM vendors WHERE vendorname LIKE venname;
RETURN a;
END$$
DELIMITER ;
no IN for functions
INTO after select list, before FROM
READS SQL DATA to avoid binary log issues
So your function definition should look like:
DELIMITER $$
DROP FUNCTION IF EXISTS getVendorID$$
CREATE FUNCTION getVendorID( venname VARCHAR(255) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE a INT;
SELECT vendorid INTO a FROM vendors WHERE vendorname LIKE venname;
RETURN a;
END$$
DELIMITER ;
Why all that code? Use this:
CREATE FUNCTION getVendorID(IN venname VARCHAR(255))
RETURNS INT
BEGIN
RETURN (SELECT vendorid FROM vendors WHERE vendorname LIKE venname LIMIT 1);
END$$
Also note introduction od LIMIT 1. Your code will explode if more than one vendor matches; you can't put the vendorid from multiple rows into one variable.
You may consider auto-wrapping with % as a service to your callers: WHERE vendorname LIKE CONCAT('%', venname, '%')