(12) mysql cursor, 12mysql cursor

Source: Internet
Author: User

(12) mysql cursor, 12mysql cursor
Overview

The query statement may query multiple records and use the cursor in the stored procedure and function to read records in the query result set one by one. In some books, the cursor is called a cursor. The use of the cursor includes declaring the cursor, opening the cursor, using the cursor, and closing the cursor. The cursor must be declared before the handler and after the variables and conditions. The cursor can be analogous to the loop pointer in the array.

Declare cursor
declare cursor_name cursor for select_statement;

The cursor_name parameter indicates the name of the cursor, And the select_statement parameter indicates the content of the SELECT statement.

Example of declaring a cursor

DECLARE cur_employee SELECT name, age FROM employe;

In the above example, declare a cursor named cur_employee. The cursor name is cur_employee; the SELECT statement is used to query the value of the name and age fields from the employee table.

Open cursor
open cursor_name;
Use cursor
fetch cursor_name into var_name[,var_name]...

The cursor_name parameter indicates the name of the cursor. The var_name parameter indicates storing the information queried by the SELECT statement in the cursor into this parameter. Var_name must be defined before the cursor is declared.

Close the cursor
close cursor_name;

After being disabled, you cannot use FETCH to use the cursor.

Usage of the cursor in a stored procedure or function

Currently, the cursor in mysql can only be used in stored procedures or functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.