What is the cursor?

Source: Internet
Author: User

Yesterday, I shared two articles on the cursor: A Case Study on the Performance of the cursor script, and a case study on the performance of the cursor script, the cursor is briefly described in the article. Today, I found a blog article about cursor, And I will study it with you here.

Cursor)

CursorIt is a data buffer provided by the system for users to store the execution results of SQL statements. Each cursor area has a name. You can use SQL statements to obtain records from the cursor one by one and assign them to the primary variables.Main languageFurther processing.Main languageIs record-oriented. A group of primary variables can only store one record at a time. Only using primary variables does not fully meet the requirements for SQL statements to output data to applications. Embedded SQL introducesCursor Concept,Used to coordinate the two different processing methods.

During database development, when the data you retrieve is only one record, the transaction statement code you write usually uses the select insert statement. However, we often encounter this situation, that is, reading a record from a result set one by one. How can this problem be solved? Cursors provide us with an excellent solution.

1. Advantages of cursors and cursors

In databases, cursor is a very important concept.CursorIt provides a flexible means to operate the data retrieved from the table. In essence,A cursor is actually a mechanism that can extract a record from a result set that contains multiple data records.The cursor is always associated with an SQL selection statement because the cursor can be zero in the result set, one record, or multiple records retrieved by the related selection statement) and the cursor position in the result set pointing to a specific record. When processing a result set, a cursor pointing to the result set must be declared. If you have written a program to process a file in C language, the cursor is like the file handle you get when you open the file. If the file is successfully opened, the file handle can represent the file. For a cursor, the principle is the same. The visible cursor can process the result set from the basic table in a similar way as the traditional program reads the flat file, so as to present the data in the table to the program in the form of a flat file.

We know that the relational database management system is actually set-oriented. In ms SQL SERVER, there is no way to describe a single record in a table, unless you use the where clause to limit that only one record is selected. Therefore, we must use a cursor to process a single record. The cursor allows the application to perform the same or different operations on each row in the row result set returned by the select statement, rather than performing the same operation on the entire result set at a time; it also provides the ability to delete or update table data based on the cursor position. In addition, it is the combination of the cursor as a collection-oriented database management system and row-oriented programming, enable communication between the two data processing methods.

2. cursor type

Ms SQL SERVER supports three types of cursors:Transact_ SQL cursor, API server cursor and customer cursor.

1) Transact_ SQL cursor

Transact_ SQL CURSOR is defined by the declare cursor syntax and is mainly used in Transact_ SQL scripts, stored procedures, and triggers. Transact_ SQL cursors are mainly used on servers. Transact_ SQL statements sent from clients to servers or Transact_ SQL statements in batch processing, stored procedures, and triggers are managed. Transact_ SQL cursor does not support extracting data blocks or multiple rows.

2) API cursor

API cursors support using cursors in ole db, ODBC, and DB_library, and are mainly used on servers. Every client application calls the API cursor function, ms SQL SEVER's OLE DB provider, ODBC drive, or DB_library's dynamic link library DLL) all these client requests are sent to the server to process the API cursor.

3) Customer cursor

Client cursors are used only when result sets are cached on the client. In a client cursor, a default result set is used to cache the entire result set on the client. Client cursors only support static cursors rather than dynamic cursors. Because server cursors do not support all Transact-SQL statements or batch processing, client cursors are often used only as an aid for server cursors. Generally, server cursors support the vast majority of cursors. Because API cursor and Transact-SQL cursor are used on the server side, they are also called server cursor and background cursor, while client cursor is called foreground cursor. In this chapter, we mainly describe server backend) cursors.

Cursor details:

RS. open SQL, CONN, A, B

Parameter A is the type of the Set cursor, and its value is:

0: Only forward cursor. Only forward browsing records are supported. pagination, Recordset, and BookMark are not supported.

1. keyset cursor. The changes made by other users to the record will be reflected in the record set, but the addition or deletion of records by other users will not be reflected in the record set. Supports paging, Recordset, and BookMark

2. dynamic cursors are the most powerful, but consume the most resources. The added or deleted records modified by the user on the record description will be reflected in the record set. Supports full-featured browsing.

3. The static cursor is only a snapshot of the data. The changes, additions, or deletions to the record statement by the user will not be reflected in the record set. Supports moving forward or backward.

Parameter B is the lock type of the record set, and its value is:

1. Lock type. The default value is read-only and cannot be modified.

2. Lock the record immediately when editing. The safest way is

3. the record set is locked only when the Update method is called. Other operations can still modify, insert, or delete the current record.

4. The record will not be locked during editing, and modification, insertion, and deletion are completed in batch processing mode.

There are more than one method to open a data record set, but the most common method is

Rs. open SQL, method, but many people do not understand the following numerical parameters. Let's introduce them below.

In fact, the open method is followed by multiple parameters.

CursorType LockType CommandType

For example, rs. open SQL

It can also be written

Rs. cursorType = 1

Rs. LockType = 1

Rs. open SQL

CursorType indicates the record returned from a table or an SQL query result.

This parameter has four values:

AdOpenForwardOnly indicates that only the records in the record set can be moved forward. This is the default value.

AdOpenKeyset reflects the change or delete actions performed by other users on the record, but does not reflect the actions performed by other users to add new records.

AdOpenDynamic reflects the changes or deletions to records made by other users, including the newly added records.

AdOpenStatic does not reflect the changes, adding or deleting actions made by other users to the record.

The four values are pre-defined bits of VBSCRIPT.

AdOpenForwardOnly = 0

AdOpenKeyset = 1

AdOpenDynamic = 2

AdOpenStatic = 3

LockType indicates the type used by the data provider to lock the database when the record set is opened:

AdLockReadOnly data cannot be changed. This is the default value!

AdLockPessimistic data provider locks the record when it starts to edit the data

AdLockOptimistic only when the update method is called, the data provider locks the record

AdLockBatchOptimistic for batch Modification

Their constant value definitions are:

AdLockReadOnly = 1

AdLockPessimistic = 2

AdLockOptimistic = 3

AdLockBatchOptimistic = 4

Rs. open SQL, conn, 1, 1 read record select

Rs. open SQL, conn, 1, 3 only update records, preferably update

Rs. open SQL, conn, 2, 3 insert and delete preferably insert delete

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.