mysql declare variable syntax error - mysql

I am trying to migrate a scalar valued function of MS SQL to function in MySQL and have been using the following syntax as Queries to create function named xx4.
DROP FUNCTION IF EXISTS `xx4`;`
CREATE DEFINER = CURRENT_USER FUNCTION `xx4`(code VARCHAR(3))`
RETURNS CHAR`
BEGIN
`DECLARE coden INT UNSIGNED DEFAULT 0= CAST(CODE AS UNSIGNED)`
`DECLARE ret CHAR =CASE`
`when coden<50 then 'A'`
`when coden<100 then 'B'`
`when coden<350 then 'C'`
`when coden<360 then 'D'`
`else null`
`END`
`RETURN ret`
`END;
It gives me error as:
at Declare ret char=Case when coden<50 then 'A' when coden<100 w' at line 4
Can you please tell me where I am wrong? Appreciate your help.

I'm not sure why there are so many backticks, but the following syntax works (mind your semicolons):
DROP FUNCTION IF EXISTS xx4;
DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION xx4(`code` VARCHAR(3))
RETURNS CHAR
BEGIN
DECLARE coden INT UNSIGNED DEFAULT 0 = CAST(`CODE` AS UNSIGNED);
DECLARE ret CHAR;
set ret = CASE
when coden<50 then 'A'
when coden<100 then 'B'
when coden<350 then 'C'
when coden<360 then 'D'
else null
END;
RETURN ret;
END;$$
DELIMITER ;

Related

Mysql bigint to int

Anybody knows how to convert a big int to int in a MySQL stored procedure.
Solution in this posthttp://stackoverflow.com/questions/11595217/convert-bigint-unsigned-to-int is not working as I tried:
set #x=BigToInt(2147483649);
ERROR 1264 (22003): Out of range value for column
I tried this and it doesn't work either:
DELIMITER //
DROP FUNCTION IF EXISTS BigToInt;
CREATE FUNCTION BigToInt (n BIGINT) RETURNS INTEGER
begin
DECLARE result INTEGER default 0;
set result = CAST(n as SIGNED);
return result;
end//
DELIMITER ;
it gives me same error as #1.
Other option:
DELIMITER //
CREATE FUNCTION BigToInt (n BIGINT) RETURNS VARCHAR(50)
begin
DECLARE result VARCHAR(50) default 0;
set result = CAST(n as signed);
return result;
end
// DELIMITER ;
This will accept a long int but limit to 19 value only.
Integer has a maximum value

MySQL function giving same result

I have created the following function in MySQL and it always give me the result as A whatever the integer number I give as parameter value.
DELIMITER $$
DROP FUNCTION IF EXISTS ISICN4A;
CREATE FUNCTION cdn(coden INT)
RETURNS CHAR
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE coden INT UNSIGNED DEFAULT 0;
DECLARE ret CHAR;
SET ret=CASE WHEN coden<=50 then 'A'
when coden BETWEEN 51 AND 100 then 'B'
when coden BETWEEN 101 AND 350 then 'C'
when coden BETWEEN 351 AND 355 then 'D'
when coden>355 then 'E'
ELSE NULL
END;
RETURN ret;
END;$$
DELIMITER $$
Appreciate your kind review and suggestion to modify it.
Your function accepts a parameter coden INT, but in function body you declare a variable with the same name as your parameter.
Try to remove DECLARE coden INT UNSIGNED DEFAULT 0; from your function body
so that your function will look like:
DELIMITER $$
DROP FUNCTION IF EXISTS ISICN4A;
CREATE FUNCTION cdn(coden INT) RETURNS CHAR
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE ret CHAR;
SET ret=CASE WHEN coden<=50 THEN 'A'
WHEN coden BETWEEN 51 AND 100 THEN 'B'
WHEN coden BETWEEN 101 AND 350 THEN 'C'
WHEN coden BETWEEN 351 AND 355 THEN 'D'
WHEN coden>355 THEN 'E'
ELSE NULL
END;
RETURN ret;
END;$$

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

#1064-error in sql syntax

I have been trying to validate a user through his username and password through a mySql procedure
delimiter //
create function validate_user(username_1,password_1)
return char(1)
DETERMINISTIC
begin
declare ret char(1);
if exists(select * from logintable where userid=username_1 and password=password_1) then
set ret='1';
endif;
return (ret);
end;//
delimiter ;
But i've been getting error #1064 error in sql syntax.
line no 3 in your code
return char(1)
should be
returns char(1)
A few things;
You're not declaring a type for your parameters.
RETURN should be RETURNS in the function declaration
ENDIF should be END IF
In other words;
create function validate_user(username_1 VARCHAR(32), password_1 VARCHAR(32))
returns char(1)
DETERMINISTIC
begin
declare ret char(1);
if exists(select * from logintable where userid=username_1 and password=password_1) then
set ret='1';
end if;
return (ret);
end;//
An SQLfiddle with all corrections.

MySQL query - weird syntax error

I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why. I did try to use the built-in function creator, but it's messed up and I don't like it. Any help appreciated!
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END
Try the following:
DELIMITER $$
DROP FUNCTION IF EXISTS `check_if_card_exists`$$
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END$$
DELIMITER ;