im having trouble finding the correct syntax i always having error #1064.
error:
#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 ';
this is the code;
DELIMITER $$
DROP FUNCTION IF EXISTS formula$$
CREATE FUNCTION formula(iq INT, sq INT, ig INT) RETURNS INT
BEGIN
DECLARE result INT;
SET result = (iq-((sq/30)) * ig;
IF result >= 0 THEN
RETURN result;
ELSEIF result < 0 THEN
RETURN 0;
END IF;
END;
$$
DELIMITER ;
i've searching for the answer in the web nothing i can find. what is the wrong with my syntax?
you have a very small mistake in the code.
DELIMITER $$
DROP FUNCTION IF EXISTS formula$$
CREATE FUNCTION formula(iq INT, sq INT, ig INT) RETURNS INT
BEGIN
DECLARE result INT;
SET result = (iq-((sq/30))) * ig; ---> You missed an closing bracket
(")") here
IF result >= 0 THEN
RETURN result; ELSEIF result < 0 THEN
RETURN 0; END IF; 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 have a problem with the next code:
create function proc1 (id int)
returns float
begin
declare sum float
select sum = (note1+note2+note3)/3 from test1.note where note.id=id;
return sum;
end
I got this 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 'select sum = (note.note1+note.note2+note.note3)/3 from test1.note where note.id=' at line 5
I've spent much time searching for a solution but no solution was found :(
delimiter |
create function proc1 (id int)
returns float
begin
declare sum float;
select (note1+note2+note3)/3 into sum from test1.note where note.id=id;
return sum;
end
|
delimiter ;
You had multiple errors:
no delimiter defined. Otherwise the DB thinks your function definition stops at the first ; which is wrong
you need to use select ... into. Otherwise you get a "not allowed to return a resultset from a function" error
you forgot a semicolon after your declare
Use set instead of select or better return without declaring a variable:
delimiter //
create function proc1 (id int)
returns float
begin
return (select (note1+note2+note3)/3 from test1.note where note.id=id);
end
//
delimiter ;
or with variable:
delimiter //
create function proc1 (id int)
returns float
begin
declare sum float;
set sum = (select (note1+note2+note3)/3 from test1.note where note.id=id);
return sum;
end
//
delimiter ;
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.
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.