Recursive DECLARE CURSOR statement 

The recursive DECLARE CURSOR statement can be used to receive bills of material by means of a command.

Syntax

<recursive declare cursor statement> ::= DECLARE <result table name> CURSOR FOR
WITH RECURSIVE <reference name> (<alias name>,...) AS
(<initial select> UNION ALL <recursive select>) <final select>

<initial select> ::= <query spec>
<recursive select> ::= <query spec>
<final select> ::= <select statement>

result table name, reference name, alias name, query spec, select statement

DECLARE C CURSOR FOR
WITH RECURSIVE PX (MAJOR, MINOR, NUMBER, MAINMAJOR) AS
  (SELECT W,X,Y,W FROM T WHERE W = 'aaa' UNION ALL
   SELECT W,X,Y,MAINMAJOR FROM T, PX WHERE MINOR = T.W)
 SELECT MAINMAJOR,MINOR,NUMBER FROM PX ORDER BY NUMBER

Explanation

If a result table name with the specified reference name existed before the recursive DECLARE CURSOR statement was executed, the corresponding cursor is closed implicitly.