Twenty-four chapters: Use the cursor

@author: Tobin
@date: 2019/11/7 16:56:50

Cursor is mainly used for interactive applications, where the user needs to scroll through the data on the screen, and browse data and make changes.
In MySQL, it can only be used for stored procedures.

CREATE PROCEDURE processorders()
BEGIN
    DECLARE done BOOLEAN DEFAULT 0;
    DECLARE o INT;
    DECLARE t DECIMAL(8, 2);
    DECLARE ordernumbers CURSOR
    FOR
    SELECT order_num FROM orders;
    DECLARE CONTINUE HANDLER FOR SQLTATE '02000' SET done=1;

    CREATE TABLE IF NOT EXISTS ordertotals
    (order_num INT, total DECIMAL(8, 2));
    OPEN ordernumbers;
    REPEAT
        FETCH ordernumbers INTO o;
        CALL ordertotal(o, 1, t);
        INSERT INTO ordertotals(order_num, total)VALUES(o, t);
        UNTIL done END REPEAT;
        CLOSE ordernumbers;
END;

Guess you like

Origin www.cnblogs.com/zuotongbin/p/11814182.html