Let face it: one thing relational databases can be particularly clumsy is recursion. Oracle as the primitive "connect internal", SQL Server 2005 as finally address it with CTEs (Common Table Expressions).
Here's a sample from infoq:
;WITH FirstCTE (column1, column2) AS
(SELECT column1, column2 FROM MyTable),
SecondCTE(column1, column2) AS
(SELECT column1, column2 FROM OtherTable)
Select * FROM FirstCTE UNION ALL Select * FROM SecondCTE
- Create the query that returns the top level (this is the anchor member).
- Write a recursive query (this is the recursive member).
- UNION the first query with the recursive query.
- Make sure you have a case where no rows will be returned (this is your termination check).
No comments:
Post a Comment