Knowledge Walls
John Peter
Pune, Maharashtra, India
Cursor in Mysql of Code Template
7163 Views
Cursor Syntax
DECLARE c_rno INT;
DECLARE c_name VARCHAR(100);

DECLARE cursor_var CURSOR FOR SELECT rno,name FROM students;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cursor_var;

	cursor_var_loop: LOOP
	     FETCH cursor_var INTO c_rno,c_name;
	       
	     -- cursor loop statements
	
	
	     IF done THEN
	            LEAVE cursor_var_loop;
	     END IF;
	END LOOP;

CLOSE cursor_var;
Multiple continue handler template
DECLARE c_rno INT;
DECLARE c_name VARCHAR(100);
DECLARE cursor_var CURSOR FOR SELECT rno,name FROM students;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_var;
    cursor_var_loop: LOOP
         FETCH cursor_var INTO c_rno,c_name;
           

         -- cursor loop statements
   
         SELECT_BLOCK: BEGIN
             DECLARE CONTINUE HANDLER FOR NOT FOUND
             BEGIN END;
                
              -- put SELECT statements here if your select statement
              -- has no records then local handler will get fired
             SELECT * FROM table_name WHERE 0;

         END SELECT_BLOCK;

    
         IF done THEN
                LEAVE cursor_var_loop;
         END IF;
    END LOOP;
CLOSE cursor_var;
Next
Next Topics
Next lessons of current book.
Sub Lessons
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details