Teradata SQL Syntax - Common Table Expressions

common-table-expression, recursive-query, sql, teradata

Solution

Multiple CTEs are supported on version 14.0 of Teradata. You can download TD v14 for VMware from the Teradata Developer Network web site and test it out.

Problem

When using multiple CTE's in MSSQL 2008, I normally separate them with a comma. But when I try this in a Teradata environment, I get an error with the syntax. Works in MS SQL: ``` WITH CTE1 AS (SELECT TOP 2 Name FROM Sales.Store) ,CTE2 AS (SELECT TOP 2 ProductNumber, Name FROM Production.Product) ,CTE3 AS (SELECT TOP 2 Name FROM Person.ContactType) SELECT * FROM CTE1,CTE2,CTE3 ``` Now, attempting to put into Teradata syntax: ``` WITH RECURSIVE CTE1 (Name) AS (SELECT TOP 2 Name FROM Sales.Store) ,RECURSIVE CTE2 (ProductNumber, Name) AS (SELECT TOP 2 ProductNumber, Name FROM Production.Product) ,RECURSIVE CTE3 (Name) AS (SELECT TOP 2 Name FROM Person.ContactType) SELECT * FROM CTE1,CTE2,CTE3 ``` Syntax error, expected something like a name or a Unicode delimited identifier between ',' and the 'RECURSIVE' keyword. 2nd attempt (without using RECURSIVE multiple times) ``` WITH RECURSIVE CTE1 (Name) AS (SELECT TOP 2 Name FROM Sales.Store) ,CTE2 (ProductNumber, Name) AS (SELECT TOP 2 ProductNumber, Name FROM Production.Product) ,CTE3 (Name) AS (SELECT TOP 2 Name FROM Person.ContactType) SELECT * FROM CTE1,CTE2,CTE3 ``` Multiple WITH definitions are not supported.

Original source