Example: Mutual Recursion - Advanced SQL Engine - Teradata Database

SQL Data Definition Language Syntax and Examples

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
wgr1555383704548.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1144
lifecycle
previous
Product Category
Teradata Vantage™

This example demonstrates mutual recursion, which is not supported for recursive view definitions.

Mutual recursion occurs when both of the following are true:

  • Recursive view A invokes, either directly or indirectly, recursive view B.
  • Recursive view B invokes, either directly or indirectly, recursive view A.

In the two mutually recursive view definitions provided here, the view named odd references the view named even, and vice versa.

    CREATE RECURSIVE VIEW even (n) AS (
      SELECT * 
      FROM (SELECT 0) AS a(i)
    UNION ALL
      SELECT m + 1
      FROM odd);
    CREATE RECURSIVE VIEW odd (m) AS (
      SELECT *
      FROM (SELECT 1) AS a(i)
    UNION ALL
      SELECT n + 1
      FROM even);