Create a Cursor
First create a data table in MySql:
Now we're going to do a function with stored procedures, count the total stock of the iphone, and export the total to the console.
fetch is to get the data row currently pointed to by the cursor and point the pointer to the next row. Continuing to execute when the cursor has pointed to the last row will cause a cursor overflow. When using the loop loop cursor, he will not monitor whether to the last data, such as the following code, which will cause a dead loop;
In MySql, a predefined NOT FOUND error in MySQL is triggered when a cursor overflow occurs, so the following code is used above to specify an event that defines a continue when a non-foundation error is raised, and to specify that the value of the done variable is modified when the event occurs.
So the following code is added to the loop:
If the value of done is true, the loop ends. Continue to execute the following code.
There are three ways to use cursors: The first is the implementation of the above, using loop loops. The second way is to use the while loop as follows:
The third way is to use repeat to execute:
In mysql, each start end block is a separate scope area. Since events with the same error in MySql can only be defined once, Duplicate handler declared in the same block will be prompted at compile time if multiple definitions are used.
The above is to implement a nested loop, of course, this example is far-fetched. Just take a look.
Mysql supports dynamic SQL functions.