Note that there are some explanatory texts on larger screens.

plurals
  1. POSELECT works on its own but returns 0 rows when assigned to a cursor
    primarykey
    data
    text
    <p>I have a SELECT statement which works on its own, and returns ~8000 rows. I am trying to add it to a stored procedure as a cursor but it's returning nothing.</p> <p>Can you spot anything I've missed?</p> <p><strong>Works</strong></p> <pre><code>SELECT a.customer, a.order_id FROM temp_orders a INNER JOIN ( SELECT customer, MAX(order_id) AS last_order FROM temp_orders WHERE pay_status = 3 OR pay_status = 4 GROUP BY customer ) AS b ON a.customer = b.customer AND a.order_id = b.last_order WHERE pay_status = 3 OR pay_status = 4 ORDER BY a.customer; </code></pre> <p><strong>No longer works</strong></p> <pre><code>DELIMITER // DROP PROCEDURE IF EXISTS get_last_orders // CREATE PROCEDURE get_last_orders() BEGIN DECLARE order_id VARCHAR(15); DECLARE customer_id INT; DECLARE done INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT a.customer, a.order_id FROM temp_orders a INNER JOIN ( SELECT customer, MAX(order_id) AS last_order FROM temp_orders WHERE pay_status = 3 OR pay_status = 4 GROUP BY customer ) AS b ON a.customer = b.customer AND a.order_id = b.last_order WHERE pay_status = 3 OR pay_status = 4 ORDER BY a.customer; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; DROP TABLE IF EXISTS temp_last_orders; CREATE TABLE temp_last_orders ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, order_id VARCHAR(15) NOT NULL, customer_id INT NOT NULL ) ENGINE = MYISAM COMMENT = 'Last paid order from each customer'; OPEN cur1; read_loop: LOOP FETCH cur1 INTO order_id, customer_id; IF done THEN LEAVE read_loop; END IF; INSERT INTO temp_last_orders (order_id, customer_id) VALUES (order_id, customer_id); END LOOP; CLOSE cur1; END; // CALL get_last_orders(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload