MYSQL cursor to learn and use examples


? who (Cursor what is?)
cursor (cursor) official definition: is the system for the user to open a data buffer to store sql execution results. Each area has a name cursor, the user can obtain one by one by the sql statement from the cursor record, and assigned to the variable, further referred to the primary language processing;
personal understanding: cursors and pointers similar sense, the implementation of the rows of the specified result set;

 

? why (Why learn cursors)
cursor allows applications to select rows in the result set returned by the query each row of the same or different operations, rather than once for the entire result set the same kind of operation;
it also provides location-based cursor the data in the table the ability to delete or update;
moreover, it is the cursor as a set-oriented database management systems and line-oriented programming linking the two, the two data processing methods to communicate.

 

what? (cursor lifecycle)
- 1. Declare the cursor

- 2. Open the cursor

- 3. variable declaration cursor fetch data to be stored

- 4. Positioning the cursor where his party

 

Use Case

1, while with the use of loops

Code # purpose: Data table update t_shop

the BEGIN
- declare a cursor variable stored the DECLARE v_shop_name VARCHAR ( 255 ); the DECLARE v_shop_area VARCHAR ( 32 ); the DECLARE DONE int the DEFAULT 0 ; The DECLARE chang_cursor CURSOR for SELECT shop_name, shop_area from t_shop; - the cursor is set to the content is empty. 1 the DECLARE the CONTINUE HANDLER the FOR the NOT FOUND the SET DONE = . 1 ; - the cursor is opened the OPEN chang_cursor; the while DONE = 0 do - the cursor assignment variable corresponding to the FETCH chang_cursor INTO v_shop_name, v_shop_area; IF v_shop_name = ' refrigerator flagship ' THEN Update t_shop SET shop_area = ' Shenzhen ' WHERE shop_name = v_shop_name; the END the IF ; End the WHILE ; - Close cursor Close chang_cursor; the END

2, the use of loops and loop

Code ## Objective: stripes statistics table t_shop

the BEGIN
the DECLARE DONE int the DEFAULT 0 ; the DECLARE v_shop_id VARCHAR ( 32 ); the DECLARE Total int the DEFAULT 0 ; the DECLARE v_shop_name VARCHAR ( 255 ); - the cursor definition the DECLARE count_rnt CURSOR for SELECT shop_id , shop_name from t_shop; - DONE data changes the setting not found, FETCH rows of data acquisition points, and the pointer to the next line, what is the last line that will cause the cursor overflow, causing mysql predefined not found end of an error, so you can let the overflow by setting the variable DECLARE The CONTINUE HANDLER the FOR the NOT found SET DONE = . 1 ; - the cursor is opened Open count_rnt; - a circulation condition read_loop: Loop the FETCH count_rnt INTO v_shop_id, v_shop_name; IF DONE = . 1 THEN leave read_loop; End the IF ; the SET Total = Total + . 1 ; End LOOP; - close the cursor the CLOSE count_rnt; - output SELECT Total; the END

3, and repeat use of loops



The BEGIN
the DECLARE v_shop_id VARCHAR ( 30 ); the DECLARE v_shop_name VARCHAR ( 255 ); the DECLARE DONE int default 0 ; the DECLARE v_total int the DEFAULT 0 ; - cursor definition the DECLARE count_rnt1 CURSOR for SELECT shop_id, shop_name from t_shop WHERE shop_name regexp ' beauty ' ; - defines the cursor overflow DECLARE the CONTINUEHANDLER the FOR the NOT found SET DONE = . 1 ; - the cursor is opened Open count_rnt1; - Loops the REPEAT the FETCH count_rnt1 INTO v_shop_id, v_shop_name; IF DONE = 0 THEN SET v_total = v_total + . 1 ; End IF ; DONE the UNTIL = . 1 End REPEAT; - close the game the CLOSE count_rnt1; SELECT v_total; END

 

 

 


Note: Knowledge
types of
MS SQL SERVER support three types of cursors: Transact_SQL cursor, API server and client cursor cursor.
(. 1) Transact_SQL cursor
  Transact_SQL cursor is defined by the DECLARE CURSOR syntax Transact_SQL mainly used in a script, stored procedures and triggers. Transact_SQL cursor is mainly used on the server managed by sent from the client to the server Transact_SQL statements or batches, stored procedures, triggers Transact_SQL. Transact_SQL cursor does not support the data blocks or multiple rows.
(2) API cursor
  API cursor support the use of cursor functions in OLE DB, ODBC and DB_library mainly used on the server. Each time the client application calls API cursor function, MS SQL SEVER the OLE DB provider, ODBC driver DB_library or dynamic link library (DLL) will take these client requests to the server for processing the API cursor.
(3) client cursor
  client only when the cursor is mainly the result set caching on the client machine. In the client cursor, a default set of results is used to cache the entire result set on the client. Customer cursor only supports dynamic rather than static cursor cursor. As the cursor server does not support all Transact-SQL statement or batch, so customers cursor is often used only as a secondary server cursor. Because under normal circumstances, the server can support the vast majority of the cursor cursor operation. As the cursor API and Transact-SQL cursors use on the server side, it is known as server cursor, also known as the background cursor, while the cursor is client-side cursor called the front desk. In this chapter of our main server (background) cursor.

Classification of the cursor
  according to the situation cursor result set to detect changes in the consumption of resources and capabilities of different, SQL Server support the API server cursor into about four kinds:

Static cursor: static cursor result set, established in TempDB, whether you're in cursor operation, how to operate the database the data set, the cursor will not change when the cursor is opened. Such as data before you open the cursor when the cursor query data table of additions and deletions to the data, after the operation, the static cursor select the data displayed is still not operational. If you want the same data after the operation, re-opening the cursor can be closed.
Dynamic cursor: The cursor is relatively static, when scrollable cursors, animated cursors reaction of all the changes the result set. Line data in the result set value, and members of the order will vary at each extraction. All users do CRUD statements are visible through the cursor. If using an API function or T-SQL Where Current of clause updated by a cursor, they will be immediately visible. Updates made outside the cursor visible only until submitted.
Only cursor: after only cursor does not support scrolling, only supports order to extract data from beginning to end, the database to perform CRUD, when the extraction is visible, but the cursor can only enter can not scroll backwards, so the line extraction make additions and deletions to the line is not visible.
Keyset-driven cursor: keyset-driven cursor is opened, each of which has membership and sequence table is fixed. When the cursor is opened, the result set is doing these row data deletion, the user scrolls the cursor is visible unique identifier identifies a group, identified column, if not identified by the columns, not visible, such as an insert data, It is not visible, if visible, must close and reopen the cursor. Static cursor can not detect changes in the data table when scrolling, but relatively few resources consumed. Dynamic cursor as you scroll detects all table data changes, but more resources are consumed. Keyset-driven cursor is in the middle of them, so build their own cursors on demand, to avoid waste of resources.

Reference: https://blog.csdn.net/xiansheng0813/article/details/91322716

https://www.cnblogs.com/cjm123/p/8619942.html

 

Guess you like

Origin www.cnblogs.com/linwx1/p/12001228.html