First, we will introduce the basic knowledge of downstream targets:
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.1 advantages of cursors and cursors
In databases, cursor is a very important concept. A cursor provides a flexible means to operate the data retrieved from a table. In essence, A cursor is actually a mechanism that can extract a record from a result set that contains multiple data records. A cursor is always associated with a T_ SQL selection statement because it is a result set (it can be zero or one or multiple records retrieved by the relevant 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 the cursor to perform the operation? BR> 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.
1.2 cursor type
Ms SQL server supports three types of cursors: Transact_ SQL, API SERVER, and customer cursors.
(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. Each client application calls the API cursor function, ms SQL SEVER's OLE DB provider, ODBC drive, or DB_library 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 will focus on server (background) cursors.
The following describes the technical implementation of the cursor.
To learn the cursor in MSQSQL, we need to first create a data table to simulate the data:
Create a data table Student with four columns
Create table Student
(
Id int identity () primary key, -- auto-increment number
Name varchar (20) not null, -- Name
Age int not null, -- Age
Address varchar (100) not null, -- Address
)
There are four processes for using a cursor: declaring a cursor, opening a cursor, extracting data, and closing a cursor.
Declare pcurr cursor for -- declare a cursor named pcurr
Declare @ customer nvarchar (50)
Declare @ age int
Select name, age from Student -- the query result is very important. This is the dataset we need to read with a cursor.
Open pcurr -- open a cursor
Fetch next from pcurr into @ customer, @ age
While (@ fetch_status = 0) -- determines whether the cursor has been read. After reading is completed, 100 instead of 0 is returned.
Begin
Print (@ customer)
Print (@ age)
Fetch next from pcurr into @ customer, @ age -- value. Because our full result set returns two rows of records, we need two custom parameters to retrieve the result set.
Close pcurr -- close the cursor
Deallocate pcurr -- release cursor
Differences between close and deallocate:
Close is to close the cursor. At this time, the cursor still exists. We can open it again. deallocate is to release the cursor. At this time, the cursor does not exist. If we need to use the cursor, we are not allowed to declare it again.
The above is a simple operation of the cursor, you can refer to the learning next!