Can't get this MySQL UDF to Create - mysql

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!

Related

How can I use the loop in MySQL?

DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END;
DELIMITER ;
The 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 'DELIMITER $$
CREATE PROCEDURE repeat()
BEGIN
DECLARE i INT DEFAULT 1;
' at line 1
I'm trying to insert 100 rows inside the table using the loop but that does not work.
REPEAT is a reserved word in MySQL. If you want to use it for userland names, you should quote it or rather use another name.
Use $$ delimiter to properly mark the end of CREATE PROCEDURE statement.
The result:
DELIMITER $$
CREATE PROCEDURE `repeat`()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= 100) DO
INSERT INTO VISITS VALUES ("C9YAoq", "2022-05-03 00:00:00");
SET i=i+1;
END WHILE;
END$$
DELIMITER ;

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

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 ;

MySQL stored function problem

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