MySql function tax (calculate IVA) - mysql

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.

Related

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

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 ;

MariaDB Error creating Function when it worked on MySQL

Please consider the following function defination. I created and set it up on MySQL 5.1 but it's failing in MariaDB 5.5
CREATE DEFINER=`root`#`127.0.0.1` FUNCTION `weighted_mean_by_kpi`(`KPIID` INT, `employee_id` INT, `date` DATE)
RETURNS decimal(6,3)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE rating_number INT DEFAULT 0;
DECLARE rating_count INT DEFAULT 0;
DECLARE rating_total INT DEFAULT 0;
DECLARE weighted_total DOUBLE DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT COUNT(rating), rating FROM employees_KPIs WHERE kpi_id = KPIID AND employee_id = employee_id AND employees_KPIs.created_at LIKE CONCAT("%",DATE_FORMAT(date,'%Y-%m'),"%") GROUP BY rating;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
RATING: LOOP
FETCH cur INTO rating_count, rating_number;
IF done = 1 THEN
LEAVE RATING;
END IF;
SET weighted_total = weighted_total + (rating_number * rating_count);
SET rating_total = rating_total + rating_count;
END LOOP RATING;
return (weighted_total/rating_total);
#return (weighted_total);
CLOSE cur;
END
I get the following error:
ERROR 1064 (42000) at line 1: 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 '' at line 8
Thanks
Mysql sees the ';' delimiters in the function and breaks your CREATE FUNCTION statement.
To avoid this, change the delimiter before you define the function, and then change it back afterward:
Like:
DELIMITER //
-- your create function definition statement here
//
DELIMITER ;
As in your code the first ; semicolon was found at line 8, it tried to execute it the code up to the ';', and the syntax was invalid because it was incomplete (BEGIN without END).

Mysql 5.6 Function error statement

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 ;

Can't create stored function - wrong syntax?

I've got the following Problem.
I want to create a stored function which converts an entity_id into the corresponding sku
Here's the code:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
Now my problem is if i fire the query i get the following message:
[Window Title]
Error
SQL 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 'LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT' at line 3
The db im using is MySQL 5.0.51a
Thanks in advance for your ideas.
MySQL by default uses ; as a delimiter, so when it encounters the ; at line 9:
DECLARE returnvalue varchar(50);
MySQL thinks the command ends - it is trying to execute:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
which isn't valid SQL.
Set a new delimiter:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
$$
You can then change the delimiter back with:
DELIMITER ;
This should work:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue VARCHAR(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
RETURN returnvalue;
END$$
DELIMITER ;
You had not specified the varchar length. :-)