views:

33

answers:

2
+1  Q: 

MySQL Cursor Issue

I've got the following code - this is the first time I've really attempted using cursors.

DELIMITER $$
DROP PROCEDURE IF EXISTS demo$$
DROP TABLE IF EXISTS temp$$

CREATE TEMPORARY TABLE temp(
    id INTEGER NOT NULL AUTO_INCREMENT,
    start DATETIME NOT NULL,
    end DATETIME NOT NULL,
    PRIMARY KEY(id)
) $$

CREATE PROCEDURE demo()
BEGIN

DECLARE done INT DEFAULT 0;
DECLARE a, b DATETIME;
DECLARE cur1 CURSOR FOR SELECT MAX(end) AS end
FROM    (
        SELECT  id, start,
                end,
                @r := @r + (start > @edate) AS num,
                @edate := GREATEST(@edate, end)
        FROM    (
                SELECT  @r := 0,
                        @edate := '0001-01-01'
                ) vars,
                student_lectures
        WHERE   (
                student_id = 1 AND
                start >= '2010-04-26 00:00:00' AND
                end <= '2010-04-30 23:59:59'
                )
        ORDER BY
                start
        ) q
GROUP BY num;

DECLARE cur2 CURSOR FOR SELECT MIN(start) AS start
FROM    (
        SELECT  id, start,
                end,
                @r := @r + (start > @edate) AS num,
                @edate := GREATEST(@edate, end)
        FROM    (
                SELECT  @r := 0,
                        @edate := '0001-01-01'
                ) vars,
                student_lectures
        WHERE   (
                student_id = 1 AND
                start >= '2010-04-26 00:00:00' AND
                end <= '2010-04-30 23:59:59'
                )
        ORDER BY
                start
        ) q
GROUP BY num
LIMIT 1, 18446744073709551615;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur1;
OPEN cur2;

    REPEAT
        FETCH cur1 INTO a;
        FETCH cur2 INTO b;
        IF NOT done THEN
            INSERT INTO temp(start, end) VALUES(a,b);
        END IF;
    UNTIL done END REPEAT;

CLOSE cur1;
CLOSE cur2;

END $$

SELECT * FROM temp;

I'm not getting anything outputted into the temp table. Running the following query gives me output, so I know there's rows it should be matching - but I imagine I've made some obvious mistake.

SELECT MAX(end) AS end
FROM    (
        SELECT  id, start,
                end,
                @r := @r + (start > @edate) AS num,
                @edate := GREATEST(@edate, end)
        FROM    (
                SELECT  @r := 0,
                        @edate := '0001-01-01'
                ) vars,
                student_lectures
        WHERE   (
                student_id = 1 AND
                start >= '2010-04-26 00:00:00' AND
                end <= '2010-04-30 23:59:59'
                )
        ORDER BY
                start
        ) q
GROUP BY num;

The output this query returns:

+---------------------+
| end                 |
+---------------------+
| 2010-04-26 13:00:00 |
| 2010-04-26 18:15:00 |
| 2010-04-27 11:00:00 |
| 2010-04-27 13:00:00 |
| 2010-04-27 18:15:00 |
| 2010-04-28 13:00:00 |
| 2010-04-29 13:00:00 |
| 2010-04-29 18:15:00 |
| 2010-04-30 13:00:00 |
| 2010-04-30 15:15:00 |
| 2010-04-30 17:15:00 |
+---------------------+
11 rows in set (0.02 sec)
A: 

I am not sure, but should not you explicitly commit after insert?

newtover
A: 

Are you calling the procedure after defining it? as in: CALL demo();

Hammerite
No, I wasn't. Thanks. :-)
James Inman