2

I have a table with a persons id as the primary key which contains their manager's id. I want to set up a while loop that will display all of that person's managers ids, all the way to the top of the list. I built a while loop, but it's returning null. What am I doing wrong?

CREATE FUNCTION `whilefunction`() RETURNS varchar(255)
BEGIN
    declare l_loop varchar(25) default '123456';
    declare result varchar(255) default '';
    while l_loop is not null do
        set result = result + (select managerid from table where personid = l_loop);
        set l_loop = (select managerid from table where personid = l_loop);
    end while;
RETURN result;
END

1 Answer 1

1

You have declared result as a string. Then you use addition. No wonder your code is not doing what you expect.

Perhaps concat() is what you want:

    set result = concat_ws(',', result, (select managerid from table where personid = l_loop);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm gonna go ahead and facepalm super hard right now. Thank you so much!

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.