MySQL must know must know the study notes Chapter 24 using cursors

MySQL 5 adds support for cursors.

A cursor is a database query stored on the MySQL server. It is not a SELECT statement, but a result set retrieved by the statement. After the cursor is stored, one or more rows can be moved forward or backward in the retrieved rows.

Unlike most DBMSs, MySQL cursors can only be used in stored procedures and functions.

Steps to use the cursor:
1. It must be declared (defined) before use. This process does not actually retrieve data, it just defines the SELECT statement to be used.
2. After the declaration, the cursor must be opened for use. This process uses the previously defined SELECT statement to actually retrieve the data.
3. Take out (retrieve) each row as needed.
4. At the end of use, close the cursor.

After declaring the cursor, you can switch the cursor frequently. After the cursor is opened, fetch operations can be performed frequently.

Create a cursor:

CREATE PROCEDURE procedureName()
BEGIN
    DECLARE cursorName CURSOR
    FOR
    SELECT语句
END;

After the stored procedure is processed, the cursor disappears.

Open the cursor:

OPEN cursorName;

The query is executed when the OPEN statement is executed, and the retrieved data is stored for browsing and scrolling.

Close the cursor:

CLOSE cursorName;

The CLOSE statement releases all internal memory and resources used by the cursor. If you want to open the cursor again, call OPEN again.

If the cursor is not closed explicitly, it will be closed automatically when the END statement is reached.

After the cursor is opened, you can use the FETCH statement to access each row of it separately. It will also move the cursor's internal row pointer forward so that the next FETCH statement retrieves the next row:

CREATE PROCEDURE procedureName()
BEGIN
    DECLARE o INT;

    DECLARE cursorName CURSOR
    FOR
    SELECT order_num FROM orders;

    OPEN cursorName;

    FETCH cursorName INTO o;

    CLOSE cursorName;
END;

Retrieve data in a loop, from the first row to the last row:

CREATE PROCEDURE procedureName()
BEGIN
    DECLARE done BOOLEAN DEFAULT 0;
    DECLARE o INT;

    DECLARE cursorName CURSOR
    FOR
    SELECT order_num FROM orders;

    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;    -- SQLSTATE ‘02000’是未找到条件,即FETCH最后一条数据之后的数据时出现

    OPEN cursorName;

    REPEAT
        FETCH cursorName INTO o;
    UNTIL done END REPEAT;

    CLOSE cursorName;
END;

The local variables defined by DECLARE must be defined before the cursor or CONTINUE HANDLER, and the CONTINUE HANDLER must be defined after the cursor.

In addition to REPEAT, MySQL also supports loop statements, which will loop until you exit manually using the LEAVE statement. Usually the syntax of the REPEAT statement is more suitable for cursor operations.

A table can be created in a stored procedure, and another stored procedure can also be called. If parameters are required when calling another stored procedure:

CREATE PROCEDURE procedureName1()
BEGIN
    DECLARE o INT;

    CALL procedureName2(o);    -- 不需要@
END;

Guess you like

Origin blog.csdn.net/tus00000/article/details/111596534