开发者

Problem with my first MySQL Function. Getting one error after the other

So, I'm making my first MySQL function.

BEGIN
DECLARE ID INT;  
SELECT
    LandID INTO ID
FROM
    Landen
WHERE
    Landnaam = landnaam;
RETURN ID;
END

The parameter is a varchar. So, let's say I got a record in Landen with the Landnaam 'Nederland'.

I run my function, give 'Nederland' as parameter. But then it gives me 开发者_如何学Goto following error:

1172 - result consisted of more than one row

Which makes no sense at all. Cause when I give something as a parameter that is not in my database, like 'ASDASD', I get the same error. And when limmiting the results with

LIMIT 1

It just always returns 1.

What am I doing wrong?


drop function if exists myfunc;
delimiter //
create function myfunc(str varchar(50))
returns int
reads sql data
begin
declare id int;  
select landid into id from Landen where landnaam = str limit 1;
return id;
end//
delimiter ;

select myfunc('nederland');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜