Unknown column error in mysql stored function - mysql

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 ;

Related

error trying to make function for counting friends

Error: There was an error while applying the SQL script to the database.
code:
USE `my_first_db`;
DROP function IF EXISTS `show_users_firends_count`;
DELIMITER $$
USE `my_first_db`$$
CREATE FUNCTION `show_users_firends_count` (ff int)
RETURNS int(11)
BEGIN
Select
count(user_friend.user_id + user_friend.friend_id)
From
user_friend
Where
user_friend.user_id = ff;
END$$
DELIMITER ;
You had it almost right.
A functions ahs to return something so you send the count back.
You should check the query anyway. your count doesn't look right. because you add useid and freind_id and count that, that is wrong. so check the query and change it. it now count all friends_id that are connected to a user_id
DROP function IF EXISTS `show_users_friends_count`;
DELIMITER $$
CREATE FUNCTION `show_users_friends_count` (ff int)
RETURNS int
DETERMINISTIC
BEGIN
Select
count(user_friend.friend_id) INTO #count
From
user_friend
Where
user_friend.user_id = ff;
RETURN #count;
END$$
DELIMITER ;

How to create a SQL function which returns a table output in MySQL

delimiter //
create function myfunc(dnum decimal(4,0))
returns table mytable( ssn char(9), dno decimal(4,0))
return table(
select *
from employee
where employee.dno=dnum)//
delimiter ;
Unrecognized data type. (near "table" at position 51)
use stored procedure instead in a function.
Like
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `your_procedure`(IN dnum decimal(4,0))
BEGIN
select *
from employee
where employee.dno=dnum;
END$$
DELIMITER ;

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 ;

How to return the table using mysql Function

I am using MySQL. I want to return the table using MySQL function. In SQL its working fine but not in MySQL. I attach my partial code
DELIMITER $$
CREATE FUNCTION myFunction() RETURNS #tmptable TABLE (item varchar(20))
BEGIN
insert into #tmptable(item) values('raja')
return
END; $$
Using functions you can not return a table.
However you can use stored procedure to return the table.
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_Name`(OUT po_ErrMessage VARCHAR(200))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET po_ErrMessage = 'Error in procedure sp_Name';
END;
SELECT * FROM table_name;
END

Help with MySQL incorrect number of arguments function error

I am getting an error in MySQL that is driving me crazy and I just can't figure out what's wrong. I makes the following call:
CALL ProfileUpdateProgress(107)
MySQL returns the error: "Incorrect number of arguments for FUNCTION ccms.fnGetProfileAlbumsPhotoCount; expected 2, got 1"
Now, as you can see in the code below, the call being made to that function is: fnGetProfileAlbumsPhotoCount(_profileId, profileUserId)
That's two arguments isn't it?? Why is it erroring??
I'm going mad!!
Database procs:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProgress`(
IN _profileId integer
)
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
CALL ProfileUpdateProfileProgress(_profileId);
END $$
DELIMITER ;
which in turn calls:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProfileProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProfileProgress`(IN _profileId int)
BEGIN
-- Declarations here
SELECT profileEyes, profileSex, profileHair, profileBustBand, profileBustCup, profileBirthCountry, profileProfession , profileAbout,
profileBiography, fnGetProfilePhoto(_profileId, null) AS profilePhoto, fnGetProfileAlbumsPhotoCount(_profileId, profileUserId) AS albumPhotoCount,
userAllowMultipleProfiles, profileIsPrimary, fnUserGetChildrenProfileCount(userId) AS ownerProfileCount
INTO _profileEyes, _profileSex, _profileHair, _profileBustBand, _profileBustCup, _profileBirthCountry, _profileProfession,
_profileAbout, _profileBiography, _profilePhoto, _albumPhotoCount, _userAllowMultipleProfiles, _profileIsPrimary,
_ownerProfileCount
FROM profile
INNER JOIN user
ON profileUserId = userId
WHERE profileId = _profileId;
-- Other irrelevant code here
END $$
DELIMITER ;
and the function being called that errors looks like:
DELIMITER $$
DROP FUNCTION IF EXISTS `fnGetProfileAlbumsPhotoCount` $$
CREATE DEFINER=`root`#`%` FUNCTION `fnGetProfileAlbumsPhotoCount`(
_profileId int,
_userId int
) RETURNS int(11)
BEGIN
DECLARE outProfileAlbumsPhotoCount int DEFAULT 0;
-- Irrelvant Code
RETURN outProfileAlbumsPhotoCount;
END $$
DELIMITER ;
Ah finally solved it. Another function called fnUserGetChildrenProfileCount in the select columns was the culprit as it too had a call to the fnGetProfileAlbumsPhotoCount() function and THAT call only had one argument, i.e. missing the second one.