Wrong MYSQL Create Function syntax? - mysql

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?

Related

error in writing procedure in Phpmyadmin

I'm trying to write a stored procedure in Mysql phpmyadmin, the procedure is
DELIMITER $$
DROP FUNCTION IF EXISTS `shopping_portal`.`f_authenticate_admin`$$
CREATE PROCEDURE =`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_password VARCHAR(50)) RETURNS int(11)
BEGIN
DECLARE exist INT DEFAULT 0;
SELECT count(*) INTO exist FROM admin WHERE username=l_username and password=MD5(l_password);
RETURN exist;
END$$
DELIMITER ;
but it is throwing the 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 '=`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_pas' at line 1
Help me in writing this procedure. Thanks in advance.
Well, multiple issues:
You're mixing procedures and functions, those are two different stories. And you're probably looking for DEFINER with your = root#localhost. And you want to use single quotes instead of backticks (I'm not sure though, if that's really an issue). Anyway, let me rewrite it for you...
DROP PROCEDURE IF EXISTS `shopping_portal`.`f_authenticate_admin`;
DELIMITER $$
CREATE DEFINER = 'root'#'localhost' PROCEDURE `f_authenticate_admin`(IN l_username VARCHAR(50), IN l_password VARCHAR(50), OUT result tinyint)
BEGIN
SELECT EXISTS(SELECT 1 FROM admin WHERE username=l_username and password=MD5(l_password)) INTO result;
END$$
DELIMITER ;
You then call it like this:
CALL f_authenticate_admin('test_username', 'a_password', #a_variable);
Then you have your result in #a_variable.
SELECT #a_variable;
Result is either 1 or 0.

error while creating mysql procedure having SYS_REFCURSOR as out param

I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
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 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.

mysql syntax error, but where?

I'm following some tutorials on mysql function creation but I keep getting the following 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 5:
CREATE FUNCTION getstatisticscount (h VARCHAR(35),d date)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE tel INT;
SELECT count(hash) into tel from statsitieken where hash=h and lastvisit between concat(d,' 00:00:00') and concat(d,' 23:59:59') group by hash;
RETURN tel;
END;
I can for my life not find where line 5 is, but no matter which line I put it on, I keep getting this error.
If I remove this function from the sql fiddle code it's all fine.
I can't find what's wrong with it... except maybe flawed tutorials.
http://sqlfiddle.com/#!2/70f0a
Use Delimiter
delimiter //
CREATE FUNCTION getstatistics(h VARCHAR(35),d date)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE tel INT;
SELECT count(hash) INTO tel
FROM statistics
WHERE
hash=h
AND lastvisit BETWEEN concat(d,' 00:00:00') AND concat(d,' 23:59:59')
GROUP BY hash;
RETURN tel;
END
//
DELIMITER ;
For more info: http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
Edit: basically since your stored procedure separates out statements with semicolons (;) and the method that defines your procedure ALSO uses semicolons to separate out statements, it's hard to impossible for MySQL to figure out where your procedure begins and ends.
I also edited the SQL statement above to return the delimiter back to the default semicolon.

mysql error, stored procedure creation

I am new to mysql and I cant see why I have an error when I create my stored procedure.
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total|
SET #total=#total+1|
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var|
END|
DELIMITER;
I get :
#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 3
I also dont get, why do I need to use that delimiter syntax.. ? DELIMITER | and then again DELIMITER;...what its function
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT, IN file_name_var VARCHAR(110))
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var, file_name=file_name_var;
END|
DELIMITER;
This works for me. no need to put | delimiter in sored procedure. I think it is meant to be for the stored procedure and not for what is inside the body
You can't simply assign variables like that, you need the SET keyword first.
http://dev.mysql.com/doc/refman/5.1/en/set-statement.html
So you code should be something like this (tested with phpMyAdmin):
DELIMITER //
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var;
END;//
DELIMITER ;
The DELIMITER keyword is used to stop additional semicolons in your procedure to be the end of the current statement, so by redefining the delimiter to // MySql will process the whole CREATE PROCEDURE-block as one single statement and not stop at the first semicolon but instead wait for the first occurrence of //.
http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

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.