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 ;
Related
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 ;
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 a newbie to mysql, I and I have written a stored procedure in which I am always getting syntax errors , I am using SQL Workbench 6.0, Could some one please help know what am I doing wrong.
CREATE DEFINER=`root`#`localhost` PROCEDURE `loginAuthentication`(IN user_name VARCHAR(45),IN pass VARCHAR(45),OUT returnvalue INT)
BEGIN
DECLARE no_of_records INT; (Syntax Error on this Line)
SELECT COUNT(*) INTO no_of_records
FROM tablename.registration
WHERE tablename.registration.user_name=user_name AND tablename.registration.pass=pass;
IF no_of_records = 1 THEN (Syntax Error on this Line)
SET returnvalue = 1;
END IF; (Syntax Error on this Line)
END; (Syntax Error on this Line)
You have to change the delimiter. Otherwise MySQL thinks that your procedure ends after the very first ;
Write it like this:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `loginAuthentication`(IN user_name VARCHAR(45),IN pass VARCHAR(45),OUT returnvalue INT)
BEGIN
DECLARE no_of_records INT;
SELECT COUNT(*) INTO no_of_records
FROM tablename.registration
WHERE tablename.registration.user_name=user_name AND tablename.registration.pass=pass;
IF no_of_records = 1 THEN
SET returnvalue = 1;
END IF;
END $$
DELIMITER ;
And btw, Workbench has nothing to do with it. It's just a client to MySQL.
DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn int)
RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;
$$
DELIMITER;
If I try to run this all I get is:
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
'RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = select count(' at line 2
A couple of changes to resolve the problem:
DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn INT)
-- RETURN INT
RETURNS INT
BEGIN
DECLARE intOut INT;
/*SET intOut = SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;*/
SET intOut = (SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn);
RETURN intOut;
END $$
DELIMITER ;
Defining the return type is done by the RETURNS keywrod, not the RETURN keyword:
CREATE FUNCTION nameOfFunct(intIn int)
RETURNS int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;
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. :-)