简体   繁体   中英

MySQL Stored Procedure, Multiple SELECTs only return one row?

I've been working on a stored procedure that runs a select statement in a loop.

When viewing results through mysqli or phpmyadmin, I only receive one row. What do I need to do to return multiple rows?

Here's a really simple example that illustrates my problem....

DROP PROCEDURE IF EXISTS simple //
CREATE PROCEDURE simple()
BEGIN

DECLARE c INT(10);
SET c = 1;

REPEAT

  SELECT c;
  SET c = c + 1;

UNTIL c >= 10 END REPEAT;

END //

I think the best way to handle this would actually be to store your output into a temporary table, and then do a final select at the end of the while loop.

DROP PROCEDURE IF EXISTS simple //
CREATE PROCEDURE simple()
BEGIN

    CREATE TEMPORARY TABLE output (finalC INT(10));

    DECLARE c INT(10);
    SET c = 1;

    REPEAT

      INSERT INTO output SELECT c;
      SET c = c + 1;

    UNTIL c >= 10 END REPEAT;

    SELECT * FROM output;

END //

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM