Home > Database > Mysql Tutorial > body text

How to use cursors? Introduction to MySQL cursors

巴扎黑
Release: 2017-05-18 14:37:45
Original
1673 people have browsed it

Cursors

Requires MySQL 5 MySQL 5 has added support for cursors, so the content of this article applies to MySQL 5 and later versions.

MySQL retrieval operations return a set of rows called a result set. The set of rows returned are all rows that match the SQL statement (zero or more rows). With a simple SELECT statement, for example, there is no way to get the first row, the next row, or the first 10 rows, nor is there an easy way to process all the rows one at a time (as opposed to processing them in batches).

Sometimes, you need to move forward or backward one or more lines in the retrieved lines. This is why cursors are used. A cursor is a database query stored on the MySQL server. It is not a SELECT statement, but the result set retrieved by the statement. After the cursor is stored, the application can scroll or browse the data within it as needed.

Cursors are mainly used in interactive applications where the user needs to scroll through data on the screen and browse or make changes to the data. Can only be used with stored procedures Unlike most DBMS, MySQL cursors can only be used with stored procedures (and functions).

We know that the relational database management system is essentially set-oriented. In MS SQL SERVER, there is no expression form to describe a single record in the table, unless a where clause is used to limit only one record to be selected. Therefore, we must use cursors to perform data processing for single records. It can be seen that the cursor allows the application to perform the same or different operations on each row in the row result set returned by the query statement select, instead of performing the same operation on the entire result set at once; it also provides the ability to perform the same operation on the data in the table based on the cursor position. The ability to delete or update; moreover, it is the cursor that connects the collection-oriented database management system and row-oriented programming, allowing the two data processing methods to communicate.

Using Cursors

Using cursors involves several clear steps.

1. Before a cursor can be used, it must be declared (defined). This procedure doesn't actually retrieve the data, it just defines the SELECT statement to use.

2. Once declared, the cursor must be opened for use. This process uses the SELECT statement defined earlier to actually retrieve the data.

3. For the cursor filled with data, take out (retrieve) each row as needed.

4. When ending the use of the cursor, the cursor must be closed. After a cursor is declared, it can be opened and closed as often as necessary. After the cursor is opened, fetch operations can be performed as frequently as necessary.

The above is the detailed content of How to use cursors? Introduction to MySQL cursors. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template