How to create a MySQL create function? - mysql

I want to create a simple function like below to do an easy task:
DELIMITER $$
CREATE FUNCTION f(key TEXT, str TEXT) RETURNS INT
BEGIN
IF LOCATE(key, str) > 0 THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END $$
DELIMITER ;
But I got 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 'key T
EXT, str TEXT) RETURNS INT
BEGIN
IF LOCATE(key, str) > 0 THEN
R' at line 1
What's the problem?

The function has a syntax error because key is a reserved word in MySql. Solution: rename the parameter or put key inside backticks like this:
CREATE FUNCTION f(`key` TEXT, str TEXT) RETURNS INT

key is a keyword in mysql, put it between ` or use different name.

Related

getting syntax error on creating function

I'm trying to create a function like the following:
CREATE FUNCTION TitleToFileName(title varchar(200)) RETURNS varchar(200)
BEGIN
set title = REPLACE(title,":"," ");
set title=REPLACE(title,"/"," ");
set title=REPLACE(title,"_"," ");
RETURN title;
END
MySQL shows 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 3
I tried using ' instead of " and #title instead of title , but didn't work..
You need to redefine Delimiter to something else (eg: $$), instead of (;).
Also as a safety measure, check if the same name function already exists or not (DROP FUNCTION IF EXISTS)
At the end, redefine the DELIMITER to ;
Try :
DELIMITER $$
DROP FUNCTION IF EXISTS `TitleToFileName`$$
CREATE FUNCTION TitleToFileName(title varchar(200)) RETURNS varchar(200)
BEGIN
set title = REPLACE(title,":"," ");
set title=REPLACE(title,"/"," ");
set title=REPLACE(title,"_"," ");
RETURN title;
END $$
DELIMITER ;

Wrong MYSQL Create Function syntax?

I'm trying to create a function like this in MYSQL:
DELIMITER $$
CREATE FUNCTION `submit`(title VARCHAR(45)) RETURNS INT
BEGIN
DECLARE articleId INT;
INSERT INTO Articles (`Title`) VALUES (title);
SET #articleId = LAST_INSERT_ID();
RETURN (articleId);
END
$$ DELIMITER ;
No matter how I change it (including removing everything in the body and placing a "return 1;" instead) I get:
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 FUNCTION submit(title v' at line 1
What am I missing?
I'm using MySQL 5.5
EDIT
Apparently, MySQL is ignoring the first statement (DELIMITER $$), failing on the first ; it finds, right after articleId INT in the 4th line. I had to reduce the code to the shortest form possible to make sure that's the case.
So I guess my question now is - Why is the DELIMITER keyword ignored?

syntax error in create function MySql

I am getting a syntax error with this simple sample script in MySql 5.6.17 :
CREATE FUNCTION sampldb.fn_x(param VARCHAR(20))
RETURNS int
BEGIN
return 1;
END
What am i doing wrong? In Sql workbench, the error is shown in the last 2 lines.
use DELIMITER
DELIMITER //
CREATE FUNCTION sampldb.fn_x(param VARCHAR(20))
RETURNS int
BEGIN
return 1;
END
//

mysql 5.1: how can i use benchmark() command to test a call to a stored procedure?

I'm trying to benchmark a stored procedure.
select benchmark(100000000,(select 1));
this benchmark works
but the following benchmark doesn't:
do benchmark(1000,(call test_login_user('a')));
it produces the following 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 'call xpofb_login_user('a')))' at line 1
any ideas how to resolve the issue ?
You can't do this with benchmark(), but you could create a stored procedure to do it.
Here's an example:
delimiter $$
create procedure benchmark_test_login_user (p_username varchar(100),
p_count int unsigned)
begin
declare v_iter int unsigned;
set v_iter = 0;
while v_iter < p_count
do
call test_login_user(p_username);
set v_iter = v_iter + 1;
end while;
end $$
delimiter ;
call benchmark_test_login_user('a',1000);
You can't
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark
Only scalar expressions can be used. Although the expression can be a subquery, it must return a single column and at most a single row. For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the table t has more than one column or more than one row.

Can any one suggest me how to resolve this problem: Error Code : 1064 in MY SQL 5.5 ver

DELIMITER $$;
DROP FUNCTION IF EXISTS tonumeric $$;
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER; $$
When I executed this function, I am facing this error.
Error Code : 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 'IF EXISTS tonumeric' at line 1
(0 ms taken)
Error Code : 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 ';
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num' at line 1
(0 ms taken)
Thanks
How about this:
DELIMITER $$
DROP FUNCTION IF EXISTS tonumeric $$
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER ;
Delimiter is a special command, in that you shouldn't terminate it with a ; -- you're actually setting the delimiter to "$$;", not "$$".