sql cursor small note

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_35972907/article/details/55662174
Cursor advantages: 
    cursor line allows applications to query result set returned select each row the same or different operations, rather than once for the entire result set of operating same; 
it also provides location for the cursor based on the data in table the ability to delete or update a 
drawback: 
    when dealing with large amounts of data, inefficient, big memory for 
 
general, can be used in other ways when processing the data, it is best not to use the cursor, except when you use a while loop, sub-queries, temporary tables time, watch variables, self-built function or other methods can not handle certain operations, 
then consider using a cursor, the cursor a common use is to save the query results for later use. Cursor result set is generated by the SELECT statement, if the process needs to be repeated using a record set, 
then create a cursor and reused several times, much faster than the repeated query the database. 


Each time a record extraction mechanism comprising a cursor is actually a plurality of data records from the result set. The role of acting as a cursor pointer. Although the cursor can traverse all the rows in the result, but he only pointed a row. 
In summary, the SQL cursor is a temporary database objects, which can be used to copy the data rows stored in the database table, a pointer to the data line may be stored in the database. 
Cursor a method of operating a data table row by row basis. 

Relational database operations may affect the entire set of rows work. For example, the row set returned by the SELECT statement includes all rows satisfying the statement WHERE clause conditions. This is called a complete set of rows returned by the statement result set. 
 Applications, particularly interactive online applications, the entire result set is not always possible as a means to effectively process. These applications require a mechanism to deal with each row or row portion.
Cursor is to provide a set of extensions to the result of such a mechanism. A cursor to a result of processing extended by:
  • Allow locating particular rows in the result set.
  • Current position search result set from one row or row portion.
  • Support for the current position in the result set row data modifications.
  • Supported by the visibility of other users with different levels of changes made to display the results of a centralized database data.
  • Providing scripts, stored procedures and triggers Transact-SQL statements used to access the result set of data.

2 Basic usage:

2.1 Declare the cursor

DECLARE cursor-name CURSOR

FOR SELECT statement

2.2 Open the cursor

OPEN cursor name

Acquiring data from the cursor 2.3

FETCH NEXT FROM cursor name [INTO FETCH_LIST]

2.4 Close the cursor

CLOSE cursor name

The cursor can not be read after closing operation, etc., may be used again OPEN Statement

Cursor 2.5 release

DEALLOCATE cursor name

Cursor is deleted, no longer available

Guess you like

Origin blog.csdn.net/qq_35972907/article/details/55662174