Syntax error in a create function, MySQL - mysql

So, below is the function i'm writing(in MySQL) and I get a syntax error at the last "RETURN" line (RETURN d_count). I'm sure it's some simple thing, but I can't figure it out.
Thanks!
DELIMITER $$
CREATE FUNCTION dept_count (dept_name VARCHAR(20))
RETURNS INT DETERMINISTIC
BEGIN
DECLARE d_count INT;
SELECT COUNT(*) into d_count
FROM instructor
WHERE instructor.dept_name=dept_name
RETURN d_count;
END $$

You should separate SELECT and RETURN with semicolon ; :
DELIMITER $$
CREATE FUNCTION dept_count (dept_name VARCHAR(20))
RETURNS INT DETERMINISTIC
BEGIN
DECLARE d_count INT;
SELECT COUNT(*) into d_count
FROM instructor
WHERE instructor.dept_name=dept_name;
RETURN d_count;
END $$

Related

MySQL ERROR #1415 - Not allowed to return a result set from a function

My question is about a function returning only one value but I still get this error, so What I'm supposed to get is average day between the order date and the shipped date, the query is doing that and returning me only one value which is the average. If I use just the SELECT statement outside the of the function I get one column/row answer of 8.4920.
How can I fix that please. Thanks.
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
END//
DELIMITER ;
You can try below
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
DECLARE var_name DECIMAL(10,2);
SET var_name = 0;
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) INTO var_name
FROM Orders;
RETURN var_name;
END
I don't understand why your function would have an argument. So, I'm thinking:
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel ()
RETURNS DOUBLE Deterministic
BEGIN
DECLARE #diff DOUBLE;
SELECT #diff := AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
RETURN #diff;
END//
DELIMITER ;

**When i execute this procedure than get error 1064**

CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50)
SET #CCode = (SELECT CountryID from countrylist where CountryName = _CountryName);
SELECT #CCode;
END
You need to redefine the Delimiter to something else (eg: $$) other than ;. At the end, reset the limiter back to ;. Also, a semicolon was missing in the Declare statement:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50); -- semicolon was missing here
SET #CCode = (SELECT CountryID
from countrylist
where CountryName = _CountryName);
SELECT #CCode;
END$$
DELIMITER ;

Unknown column error in mysql stored function

Following is my code of MySQL Stored Function.
DELIMITER $$
USE `mac_db`$$
DROP FUNCTION IF EXISTS `moving_average`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `moving_average`(`table_name` VARCHAR(255),`column_name` VARCHAR(255),`order_column` VARCHAR(255),`row_cnt` INT) RETURNS DOUBLE
DETERMINISTIC
BEGIN DECLARE result_avg DOUBLE DEFAULT 0;
SELECT AVG(`column_name`) INTO result_avg FROM table_name ORDER BY order_column DESC LIMIT row_cnt;
RETURN result_avg;
END$$
DELIMITER ;
I am calling the function as follows:
SELECT moving_average('my_table','my_col','id',4);
I am getting the following error.
Table 'mac_db.table_name' doesn't exist
Please help to resolve the error.
table_name is a parameter not the name of table.
replace you proc parameter as below:
DELIMITER $$
USE mac_db$$
DROP FUNCTION IF EXISTS moving_average$$
CREATE DEFINER=root#localhost FUNCTION moving_average(#table_name VARCHAR(255),#column_name VARCHAR(255),#order_column VARCHAR(255),#row_cnt INT) RETURNS DOUBLE
DETERMINISTIC
BEGIN DECLARE result_avg DOUBLE DEFAULT 0;
SELECT AVG(#column_name) INTO result_avg FROM #table_name ORDER BY #order_column DESC LIMIT #row_cnt;
RETURN result_avg;
END$$
DELIMITER ;

MYSQL function, is it Workbench or just noobish me?

I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)

What's wrong with this MySQL Stored Function?

Having trouble getting this to apply in MySQL Workbench 5.2.15
DELIMITER //
CREATE
DEFINER=`potts`#`%`
FUNCTION
`potts`.`fn_create_category_test` (test_arg VARCHAR(50))
RETURNS int
BEGIN
DECLARE new_id int;
SET new_id = 8;
RETURN new_id;
END//
The actual function will have a lot more between BEGIN and END but as it stands, even this 3 liner won't work.
Thanks!
DELIMITER $$
CREATE FUNCTION `fn_create_category_test` (test_arg varchar(50))
RETURNS INT
BEGIN
DECLARE new_id int;
set new_id=8;
return new_id;
END $$
DELIMITER ;
Works fine for me, try getting rid of DEFINER?