0

I have a table and when i make select i have results with two records. So now is ok. This result is INT. I want create function when i get each one of this value and add as string and return as string. Now i have result. What is wrong ?

pno

123 873

and after function i want

pno

123, 873, etc etc

BEGIN
        DECLARE done INT DEFAULT FALSE;
        declare i_myfield int;
        declare abc int;
        declare mycur cursor for SELECT pno FROM ptable WHERE no = no_id;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
        set abc = 0;
        open mycur;

        read_loop: loop
            fetch mycur into i_myfield;
            IF done THEN
                LEAVE read_loop;
            END IF;
            SET abc = abc + CONVERT(i_myfield, CHAR(50));

        END loop;
    close mycur;
    return pono;
END

1 Answer 1

1

Consider having your database do this for you (instead of looping through your cursor, which can be expensive):

 SELECT GROUP_CONCAT(pno) FROM ptable WHERE no = no_id;

That replicates all of this code in one simple statement.

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

1 Comment

No worries. We've all reinvented the wheel once or twice ;)

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.