Simple Cursor Traversal 2

Developed In: SQL — Contributed by: Serdar S. Kacar

In this template, there is no loop control variable (like "done") and no label (like "MyLabel:"). For the "my cursor traversal code is not looking good" type of freaks :)

Note : Template is the convertion of the sample given in "Cursors" topic of MySQL manual,

Beware :

1. There is a secondary BEGIN .. END envelop between OPEN cursor and CLOSE cursor.

2. NOT FOUND handler moved to the inner BEGIN .. END and its action is EXIT !


Serdar S. Kacar
SQL
  1. CREATE PROCEDURE curdemo()
  2. BEGIN
  3. DECLARE a CHAR(16);
  4. DECLARE b,c INT;
  5. DECLARE cur1 CURSOR FOR SELECT id,DATA FROM test.t1;
  6. DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
  7.  
  8. OPEN cur1;
  9. OPEN cur2;
  10.  
  11. BEGIN
  12. DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END;
  13. LOOP
  14. FETCH cur1 INTO a, b;
  15. FETCH cur2 INTO c;
  16. IF b < c THEN
  17. INSERT INTO test.t3 VALUES (a,b);
  18. ELSE
  19. INSERT INTO test.t3 VALUES (a,c);
  20. END IF;
  21. END LOOP;
  22. END;
  23.  
  24. CLOSE cur1;
  25. CLOSE cur2;
  26. END
  27.  

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

2 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment