(a) cursor in MySQL

Source: Internet
Author: User

Overview

Query statements may query multiple records, using cursors in stored procedures and functions to read the records in the query result set one by one. Some books call the cursor a cursor. The use of cursors includes declaring the cursor, opening the cursor, using the cursor, and closing the cursor. The cursor must be declared before the handler and declared after the variable and the condition. The cursor can be likened to a loop pointer within an array.

declaring Cursors
declare cursor_name cursor for select_statement;

Where the cursor_name parameter represents the name of the cursor; The select_statement parameter represents the contents of the SELECT statement.

Example of a declaration cursor

SELECT name, age FROM employe;

In the example above, a cursor named Cur_employee is declared. The name of the cursor is the CUR_EMPLOYEE;SELECT statement section that queries the value of the name and age fields from the employee table.

Open Cursor
open cursor_name;
using the cursor
fetch cursor_name into var_name[,var_name]...

Where the cursor_name parameter represents the name of the cursor, and the Var_name parameter indicates that the information queried by the SELECT statement in the cursor is stored in the parameter. Var_name must be well defined before the cursor is declared.

Close Cursor
close cursor_name;

After closing, you cannot use the cursor with fetch.

The use of the cursor in a stored procedure or function

目前,mysql中的光标只能在存储过程或函数中使用。

(a) cursor in MySQL

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.