title: MariaDB stored procedure select statement only returning single row
body: <p>I have created the following stored procedure which creates a table with multiple rows.</p>
  
  <p>The select statement should return multiple rows as well, however it is only returning a single row.</p>
  
  <pre><code>DELIMITER $$
  DROP PROCEDURE IF EXISTS sp_loadMarginAllocations;
  CREATE  PROCEDURE sp_loadMarginAllocations(IN  leid INT(10))
  BEGIN
      DECLARE done INT(10);
      DECLARE accountId VARCHAR(64);
      DECLARE myCursor CURSOR FOR SELECT ca.account_id FROM account_balance ca WHERE ca.legal_entity_id =leid;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      DROP TEMPORARY TABLE if exists margin_balances;
  
      CREATE TEMPORARY TABLE margin_balances (
         cptyName VARCHAR(64)  DEFAULT NULL,
          cptyAccountId VARCHAR(64) DEFAULT NULL,
          qtyLocked4us decimal(32,9) DEFAULT 0.00,
          accountId VARCHAR(64) DEFAULT NULL,
          qtyLocked4cpty decimal(32,9) DEFAULT 0.00,
          trades INT(7) DEFAULT 0
      );
      SET done = 0;
  
      OPEN myCursor;
      my_loop: LOOP
          FETCH NEXT FROM myCursor INTO accountId;
          IF done THEN
              LEAVE my_loop;
          END IF;
  
          INSERT INTO margin_balances (cptyName, cptyAccountId, qtyLocked4us, accountId, qtyLocked4cpty, trades)
              SELECT t1.taker_account_name as cptyName, t1.taker_account_id AS cptyAccountId, SUM(t1.maker_amount) AS qtyLocked4us,
                  t2.maker_account_id AS accountId, SUM(t2.taker_amount) AS qtyLocked4cpty, COUNT(*) AS trades
                  FROM collateral_trade t1, collateral_trade t2 WHERE t1.collateral_id = t2.collateral_id
                  AND (t1.maker_account_id = accountId )  UNION all
              SELECT t1.maker_account_name as cptyName, t1.maker_account_id AS cptyAccountId, SUM(t1.taker_amount) AS qtyLocked4us,
                  t2.taker_account_id AS accountId, SUM(t2.maker_amount) AS qtyLocked4cpty, COUNT(*) AS trades
                  FROM collateral_trade t1, collateral_trade t2 WHERE t1.collateral_id = t2.collateral_id
                  AND (t1.taker_account_id = accountId  );
  
      END LOOP;
  
      CLOSE myCursor;
      SELECT * FROM margin_balances WHERE trades != 0 GROUP BY accountId;
  END $$
  DELIMITER ;
  call sp_loadMarginAllocations(243);
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>MariaDB [test_table]&gt; call sp_loadMarginAllocations(243);
  +-----------------------+----------------+----------------+----------------+----------------+--------+
  | cptyName              | cptyAccountId  | qtyLocked4us   | accountId      | qtyLocked4cpty | trades |
  +-----------------------+----------------+----------------+----------------+----------------+--------+
  | JPM1 PartyB Custodian | jpm1@custodian | 1874.000000000 | jpm1@custodian | 1127.000000000 |     78 |
  +-----------------------+----------------+----------------+----------------+----------------+--------+
  1 row in set (0.006 sec)
  </code></pre>
  
  <p>Table looks like this when select statement run manually (and what I want to see from the SP):</p>
  
  <pre><code>+-----------------------+-------------------+----------------+-------------------+----------------+--------+
  | cptyName              | cptyAccountId     | qtyLocked4us   | accountId         | qtyLocked4cpty | trades |
  +-----------------------+-------------------+----------------+-------------------+----------------+--------+
  | NULL                  | NULL              |           NULL | NULL              |           NULL |      0 |
  | JPM1 PartyB Custodian | jpm1@custodian    | 1874.000000000 | jpm1@custodian    | 1127.000000000 |     78 |
  | Z-PartyA              | zedclient@enduser |  413.000000000 | zedmaster@enduser |  513.000000000 |      9 |
  +-----------------------+-------------------+----------------+-------------------+----------------+--------+
  </code></pre>