MySQL stored function problem - mysql

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, '%')

Related

how to create a function in mySQL workbench

trying to get the total number of boxes ordered from a given type("mytype").
something seems to be wrong with my syntax
create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE #numorders int
select #numOrders = count(*)
from BOXES as B
where B.Type = mytype
return #numOrders
end
;
In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables
delimiter $$
create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int;
set numorders = (select count(*)
from boxes as B
where B.Type = mytype
);
return numOrders;
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

Can't get this MySQL UDF to Create

I'm receiving a variety of error codes when I try to create this function, most recently this one: "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 '' at line 2 "
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2);
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END;
Any clues as to what I'm doing wrong?
You need to set a different delimiter during your function definition.
So something like this:
DELIMITER $$
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2)
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END$$
DELIMITER ;
Solution was a combo of needing a different delimiter and removing ; after RETURNS FLOAT(10,2)
DELIMITER $$
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2)
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END$$
DELIMITER ;
Thanks!

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 ;

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