I am not a MYSQL developer, but just had to write some code. Please accept my apologies if I am doing anything silly in my code.
My need is to get a single row like select * from users where userID = 1 limit 1 and assign it to a variable and access columns for doing some calculation. Firstly, is this possible? I have tried to go through step by step, that is why I wrote a simple function like below
DELIMITER $$
CREATE DEFINER=`user`@`%` FU开发者_StackOverflowNCTION `GetReportees`(userid VARCHAR(255)) RETURNS varchar(50) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE Var1 varchar(120);
DECLARE Var2 varchar(120);
Select @Var1=forename, @Var2=surname from company_users.users where userID = @userid limit 1;
return @Var1;
END
when I try to save this function, it says that ERROR 1415: Not allowed to return a result set from a function
. But I clearly return a varchar variable.
Could anyone tell me what I am doing wrong? it should not be this much hard, I believe.
Many thanks
Regards
You should read the documentation on the difference between user-defined variables and local variables.
In your example, you have a parameter and 2 local variables, so you should use them like this:
DELIMITER $$
CREATE DEFINER=`user`@`%` FUNCTION `GetReportees`(p_userid VARCHAR(255))
RETURNS varchar(120) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE Var1 varchar(120);
DECLARE Var2 varchar(120);
Select forename, surname
into Var1,Var2
from company_users.users
where userID = p_userid
limit 1;
return Var1;
END
精彩评论