Mysql 5.6 Function error statement - mysql

I try to write mysql function. I have mysql 5.6, and I don`t know why every time i try do it:
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END;
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 '' at line 1 */
I know it is basic mistake, but I don`t know how I can fix it...

You need to change the delimiter that MySQL is using or it will stop processing the CREATE request after the first semi-colon.
Do this:
DELIMITER $$
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END$$
DELIMITER ;

Related

MySql function tax (calculate IVA)

The problem is i'm writing the function directly from shell(bash)
trying to translate the syntax from some IDE that i don't know !!
The original function is:
CREATE FUNCTION CalcIVA(#Price money)RETURNS money
AS
BEGIN
DECLARE #IVA money
SET #IVA = #Price * 1.16
RETURN #IVA
END;
So changed some syntax to work with mysql> prompt:
CREATE FUNCTION CalculoIVA(x INT)RETURNS INT DETERMINISTIC
BEGIN
DECLARE IVA INT
SET IVA = x *1.16
RETURN IVA
END;
What i'm doing wrong?
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 'SET IVA = x *1.16 RETURN IVA END' at line 1
Determine what datatypes you want. INT won't work so hot with your 1.16 idea. You lose to rounding to an INT. The below does not use INT
DROP FUNCTION IF EXISTS CalculoIVA;
DELIMITER $$
CREATE FUNCTION CalculoIVA
(x DECIMAL(12,4))
RETURNS DECIMAL(12,4)
DETERMINISTIC
BEGIN
DECLARE IVA DECIMAL(12,4); -- OR FLOAT etc
SET IVA = x *1.16;
RETURN IVA;
END;$$
DELIMITER ;
select CalculoIVA(9.12);
-- 10.5792
If you want the datatypes to be DECIMAL(12,2) or FLOAT just make the necessary changes.
Note: the DELIMITER wrapper is unnecessary for PHPMyAdmin users.

SQL Error: 1064 You have an error in your SQL syntax;

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 ;

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
//

syntax error for mysql declaration of variable

CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END
I get an syntax 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 '' at line 3
But for me, everything seems to be correct. i really don't have any clue! can anybody help?
thanks
You need to temporarily change the delimiter so the MySQL client doesn't think you're done with your statement when it sees the semicolon on line 3:
DELIMITER //
CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END//
DELIMITER ;
Remove the DECLARE, you should be able to just do this:
SET #x = 0;
Also, variables need to be prefixed with the # symbol

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.