1

I am trying to write a stored function in mysql 5.1 that returns the value 'AccountIDref' for a given room. If I only query the inner SELECT statement this works (returns the value for room). But invoking the function I get the response:

'#1172 - Result consisted of more than one row'

CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
    DECLARE refID INT DEFAULT NULL;
    SELECT AccountIDref INTO refID FROM Allocation
    WHERE Room = room;
    RETURN refID;
END

What am I doing wrong here?

1
  • ah forgot: example is simplified but there should not be more than one row in this case. I have the impression, the WHERE clause is just ignored somehow... Commented Feb 14, 2013 at 22:52

2 Answers 2

1

Field name and parameter name must be different -

CREATE FUNCTION getAccountId (room_param INT) RETURNS INT
BEGIN
    DECLARE refID INT DEFAULT NULL;
    SELECT AccountIDref INTO refID FROM Allocation
    WHERE Room = room_param;
    RETURN refID;
END

In your function you were getting all tables records.

Sign up to request clarification or add additional context in comments.

Comments

0

What I am going to suggest isn't going to be much different from what you have, but I am skeptical about the where clause being in the next line and also let's use limit 1 to explicitly set the limit.

Try this :

CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
    DECLARE refID INT DEFAULT NULL;
    SELECT AccountIDref INTO refID FROM Allocation WHERE Room = room LIMIT 1;
    RETURN refID;
END

1 Comment

Meeeh did not help. Now I get the ID of the first table entry as result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.