A simple application of a cursor. : Fetch « Cursor « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Cursor » Fetch 
A simple application of a cursor.
     

mysql>
mysql>
mysql>
mysql> CREATE TABLE titles (
    ->   titleID int(11),
    ->   title varchar(100),
    ->   subtitle varchar(100),
    ->   edition tinyint(4),
    ->   publID int(11),
    ->   catID int(11),
    ->   langID int(11),
    ->   year int(11),
    ->   isbn varchar(20),
    ->   comment varchar(255),
    ->   ts timestamp,
    ->   authors varchar(255),
    ->   PRIMARY KEY  (titleID)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql> INSERT INTO titles VALUES (1,'Linux','Installation',5,1,57,2,2000,NULL,NULL,'2005-02-28 13:34:21','Michael'),
    ->                           (2,'Excel',NULL,NULL,2,3,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','David'),
    ->                           (3,'XML',NULL,NULL,1,2,NULL,1997,NULL,NULL,'2005-02-28 13:34:22','Edwards'),
    ->                           (4,'PHP',NULL,NULL,3,6,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','Tom'),
    ->                           (5,'MySQL','',0,3,34,NULL,2000,'','','2005-02-28 13:34:22','Paul'),
    ->                           (6,'Java',NULL,NULL,4,34,NULL,1999,NULL,NULL,'2005-02-28 13:34:22','Tim');
Query OK, rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE cursortest(OUT avg_len DOUBLE)
    -> BEGIN
    ->     DECLARE t, subt VARCHAR(100);
    ->     DECLARE done INT DEFAULT 0;
    ->     DECLARE n BIGINT DEFAULT 0;
    ->     DECLARE cnt INT;
    ->     DECLARE mycursor CURSOR FOR
    ->
    ->     SELECT title, subtitle FROM titles;
    ->
    ->     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
    ->
    ->     SELECT COUNT(*FROM titles INTO cnt;
    ->     OPEN mycursor;
    ->     myloop: LOOP
    ->        FETCH mycursor INTO t, subt;
    ->        IF done=THEN LEAVE myloop;
    ->        END IF;
    ->           SET n = n + CHAR_LENGTH(t);
    ->        IF NOT ISNULL(subtTHEN
    ->           SET n = n + CHAR_LENGTH(subt);
    ->        END IF;
    ->     END LOOP myloop;
    ->     SET avg_len = n/cnt;
    -> END$$
Query OK, rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql>
mysql> CALL cursortest(@result);
Query OK, rows affected (0.00 sec)

mysql> SELECT @result;
+-------------+
| @result     |
+-------------+
6.166666666 |
+-------------+
row in set (0.00 sec)

mysql>
mysql>
mysql> drop table titles;
Query OK, rows affected (0.00 sec)

mysql>
mysql>

   
    
    
    
    
  
Related examples in the same category
1.FETCH Statement
2.Fetch a cursor
3.Leave a cursor fetch
4.Using while loop to loop through a cursor
5.Using loop and Fetch to read the data in a cursor
6.Output data in a cursor
7.Insert data from a cursor
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.