I have one function written in my sql which returns the count of records based on some id's. But it always returns the value 1950.Here is my query.
CREATE DEFINER=`trajectory`#`%` FUNCTION `fn_ReturnAlreadyPlayedGames`(AppId int(11),AppUserid int(11))
RETURNS int(11)
BEGIN
declare Counting int(11);
set Counting=(
select count(*)
from user_sessions
where appid= 5 and appuserid= 41 and status=1);
RETURN Counting;
END
I have passed static id's for now.when I run this query as plain sql then it returns 8 rows but inside function it always returns 1950.
Please help me in this.Thanks in advance.
there is a little mistake ;)
where appid= 5 and appuserid= 41 and status=1);
you should replace appid and appuserid with your variables
The problem is using the same name for the function parameters and the columns. So use an alias when referring to the columns. Or just rename the function parameters.
DELIMITER $$
CREATE FUNCTION fn_ReturnAlreadyPlayedGames(AppId int(11),AppUserid int(11))
RETURNS int(11)
BEGIN
declare Counting int(11);
set Counting=(select count(*) from user_sessions us where us.appid= 5 and us.appuserid= 41 and us.status=1);
RETURN Counting;
END$$
DELIMITER ;
With the parameters
DELIMITER $$
CREATE FUNCTION fn_ReturnAlreadyPlayedGames(appid_param int(11),appuserid_param int(11))
RETURNS int(11)
BEGIN
declare Counting int(11);
set Counting=(select count(*) from user_sessions us where us.appid= appid_param and us.appuserid= appuserid_param and us.status=1);
RETURN Counting;
END$$
DELIMITER ;
Related
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 ;
I have this table
I want to create stored function that takes empno and returns its manager (boss) name. (one employee is boss of other employee)
this is the code i tried but it returned error
MYSQL said: The query was empty
CREATE FUNCTION MANAGER1(ENO int) RETURNS varchar DETERMINISTIC
BEGIN
DECLARE MANAGER_NAME varchar(MAX);
Select name into MANAGER_NAME where boss = ENO;
RETURN varchar MANAGER_NAME
DELIMITER $$ ;
Check once again with following query for creating stored function:
DELIMITER //
CREATE FUNCTION getManager1(eno int(11))
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE manager_name varchar(250);
Select name into manager_name FROM employees where id = eno;
return manager_name;
END //
DELIMITER ;
In your stored procedure statement, END tag is missing and table name is also missing in SELECT statement. I have executed query mentioned here. Please match with your query and check what is missing in your query posted in question.
I was told to create a mysql function that, given the artwork title, return the name of the creator. I know it sounds silly, i could use just a select.
I followed some tutorials, but none of them helped me. When I try to create the function, mysql workbench says "Error Code: 1415. Not allowed to return a result set from a function"
Here is what i'm trying to do
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
SELECT result= ar.artistName FROM artist as ar, art_object as oa
where ar.artistCode= oa.artistCode
and oa.title= workName
group by ar.artistName ;
return result;
end$$
delimiter ;
That's not how you assign the result of a variable in a SELECT statement. You have to use SELECT ... INTO variable.
You also need to declare the variable first.
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
DECLARE result VARCHAR(250);
SELECT DISTINCT ar.artistName FROM artist as ar, art_object as oa
INTO result
where ar.artistCode= oa.artistCode
and oa.title= workName;
return result;
end$$
delimiter ;
SELECT result = ar.artistName is comparing the value of result with ar.artistName, and returning the result of that comparison from the SELECT statement.
I have a question about the stored procedure:
Create a stored procedure named format_currency that accepts a character and a double number. It will return a VARCHAR(32) with the symbol in the front, followed by the number to 2 decimal places.
For example, format_currency('$', 123.4) should return $123.40
Here is my code:
CREATE OR REPLACE PROCEDURE format_currency(IN c VARCHAR2, IN n DOUBLE(9,2))
AS
BEGIN
SELECT CONCAT(c,',' n);
END;
It's not working, I have no idea how to write codes inside BEGIN and END.
Many Thanks for your help.
Henry
Instead of procedure I suggest to use function:
Code:
DELIMITER $$
DROP FUNCTION IF EXISTS test_f$$
CREATE FUNCTION test_f(a varchar(22),b double(9,2)) returns varchar(64)
BEGIN
SET #RET = (select concat(a,b));
return #RET;
END$$
DELIMITER ;
Test Run:
mysql> select test_f('$',123);
+-----------------+
| test_f('$',123) |
+-----------------+
| $123.00 |
+-----------------+
1 row in set (0.10 sec)
mysql>
DELIMITER\\
CREATE PROCEDURE format_currency (a CHAR, b DOUBLE(9,2))
BEGIN
declare ret VARCHAR(32);
set #ret = (SELECT CONCAT(a,b));
select #ret;
END
DELIMITER\\
call format_currency('$',123);
I had a similar question and nothing the other people answered worked for me in MySQL. Here is a working solution with 2 parameters as "SIGN" and "MONEY" and creating a variable called "NEWRESULT" that you delcare, set as 0, and then set as the concat of "SIGN" and "MONEY".
DELIMITER //
CREATE PROCEDURE format_currency`enter code here`
(IN SIGN char(1),IN MONEY double(9,2))
BEGIN
declare NEWRESULT char(8);
set NEWRESULT=0;
SET NEWRESULT=(SELECT CONCAT(SIGN,MONEY));
SELECT NEWRESULT;
END//
DELIMITER ;
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 ;