mysql study notes - the cursor

The basic concept 1, the cursor

Data buffer thought : Cursor design is a data buffer thought, used to store the results of SQL statements executed .

Prior data base : the cursor is earlier retrieve data from the data table technology before continuing flexible operation.

Like pointers : pointing cursor data structure similar to the stack pointer , a pop for data points, and each taking only a.

If you read the previous mysql function, you will find multi-line statement can not be used to return the results. But if you really want to use and you need to use the cursor, the cursor can help you choose a certain result (so that you can do to return a single result).

In addition, the cursor can be easily removed in the retrieved row move forward or backward a result of one or more rows .

Cursor can traverse the return of multi-line results .

 

  • Mysql cursors only applies to stored procedures and functions.

2, using the cursor

Using cursors generally divided into five steps, mainly: the cursor definition -> Open cursor -> cursor -> close the cursor -> released cursor .

premise:

- Create two tables, tables without primary keys would have been circulating 
the CREATE  TABLE class (the above mentioned id int ( 5 ) PRIMARY  KEY , name VARCHAR ( 10 ));
 the CREATE  TABLE Class2 (the above mentioned id int ( 5 ) PRIMARY  KEY , name VARCHAR ( 10 )) ; 

- class data inserted in 
the iNSERT  the INTO class (id` `,` name`) the VALUES ( ' . 1 ' , ' ceshi ' );
 the SELECT  *  the FROM class;

- class data table to delete or drop a table 
DELETE  the FROM Class2;
 DROP  TABLE Class2;
the Create  Procedure P2 ()
 the begin 
    DECLARE the above mentioned id int ;
     DECLARE name VARCHAR ( 15 );
     - Declare the cursor 
    DECLARE mc the Cursor  for  the SELECT  *  from class;
     - open the cursor 
    Open mc;
     - obtaining results 
    Loop   - loop, the table of contents class2 are transferred to the 
    FETCH MC iNTO ID, name;
     - here to obtain the results show 
    INSERT  iNTO class2 values (ID, name);
     -Close the cursor 
    End Loop;
     Close MC; 
    
End ;

The code given

 

Table class2 have a primary key, continuous cycle, it always reaches the end of the table, can not continue to the end of the fetch, in general should avoid error, to have a mysql defined before the end of the

DELIMITER // 
the Create  Procedure P2 ()
 the begin 
    DECLARE the above mentioned id int ;
     DECLARE name VARCHAR ( 15 );
     - Declare the cursor 
    DECLARE mc the Cursor  for  the SELECT  *  from class;
     - open the cursor 
    Open mc;
     - obtaining results 
    Loop   - cycle, contents of the table are transferred to the class2 
    FETCH MC iNTO ID, name;
         IF in Flag = . 1  the then  -When not fetch triggers the Continue Handler 
        the Leave L2;
     End  IF ;
     - here to get the results show 
    INSERT  INTO Class2 values (the above mentioned id, name);
     - close the cursor 
    End Loop;
     use Close mc; 
    
End ;
 // 
DELIMITER; 
the CALL P2 ( );

 

Guess you like

Origin www.cnblogs.com/shishibuwan/p/10972655.html