0

I need create a store-procedure in mysql but i have problem, i don't know if is possible that i can a loop with return rows from query

this a tableOfExample

--------------------+-------+------+
fecha               | cod   | Ucod |
--------------------+-------+------+
2020-11-10 00:20:50 | 24167 |    1 |
2020-11-10 00:20:24 | 24166 |    1 |
2020-11-08 03:01:29 | 24165 |    1 |
2020-11-08 01:18:21 | 24161 |    1 |
2020-11-07 20:26:59 | 24160 |    1 |
2020-11-06 20:55:09 | 24159 |    1 |
2020-11-06 20:51:58 | 24158 |    1 |
2020-11-06 20:49:27 | 24155 |  390 |
2020-11-06 20:28:40 | 24153 |    1 |
2020-11-06 20:22:50 | 24150 |    1 |
2020-11-06 18:18:57 | 24149 |    1 |
2020-11-06 15:14:27 | 24148 |   48 |
2020-11-06 14:55:44 | 24147 |  107 |
2020-11-06 14:52:22 | 24146 |  107 |
2020-11-06 14:50:48 | 24145 |  107 |
2020-11-06 14:29:31 | 24144 |    1 |
2020-11-06 13:20:33 | 24143 |    1 |
2020-11-05 22:00:37 | 24142 |    1 |
2020-11-05 14:58:00 | 24141 |  107 |
2020-11-05 14:57:06 | 24140 |  107 |
--------------------+-------+------+

And my sql code

DROP PROCEDURE IF EXISTS testProc;
DELIMITER //
CREATE PROCEDURE testProc(user int)
BEGIN

    -- DECLARE cursor_history CURSOR FOR SELECT * FROM tableOfExample WHERE uCod=user;
    SELECT @user:=uCod FROM tableOfExample WHERE uCod=user;
    SELECT @results:=FOUND_ROWS();

    IF @results > 0
        THEN
            -- wile doesn't work for my logic
            WHILE i < @results DO
                SET i := i + 1;
            END WHILE;
    END IF;
END //
DEMILMITER;

Thanks

2
  • post your code as text, highlight it and hit the {} button. Voilà! You will have your code as expected. There is no reason why this shouldn't be valid code Commented Nov 10, 2020 at 12:57
  • here is the solved stackoverflow.com/questions/49239885/mysql-foreach-loop Commented Feb 8, 2021 at 23:24

0

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.