<html>
<head>
    <base href="https://docs.jboss.org/author">
            <link rel="stylesheet" href="/author/s/en/2172/19/5/_/styles/combined.css?spaceKey=TEIID&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://docs.jboss.org/author/display/TEIID/WITH+Clause">WITH Clause</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{note}The WITH clause is also subject to optimization and it&#39;s entries may not be processed if they are not needed in the subsequent query.{note} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;"> <br>h3. Recursive Common Table Expressions <br> <br>A recursive common table expression is a special form of a common table expression that is allowed to refer to itself to build the full common table result in a recursive or iterative fashion. <br> <br>Usage: <br> <br>{code:lang=SQL}WITH name [(column, ...)] AS (anchor query expression UNION [ALL] recursive query expression) ...{code} <br> <br>The recursive query expression is allowed to refer to the common table by name.  Processing flows with The anchor query expression executed first.  The results will be added to the common table and will be referenced for the execution of the recursive query expression.  The process will be repeated against the new results until there are no more intermediate results.   <br> <br>{note}A non terminating recursive common table expression can lead to excessive processing.{note} <br> <br>To prevent runaway processing of a recursive common table expression, processing is by default limited to 10000 iterations.  Recursive common table expressions that are pushed down are not subject to this limit, but may be subject to other source specific limits.  The limit can be modified by setting the session variable teiid.maxRecusion to a larger integer value.  Once the max has been exceeded an exception will be thrown. <br> <br>Example: <br> <br>{code:lang=SQL}SELECT teiid_session_set(&#39;teiid.maxRecursion&#39;, 25); <br>WITH n (x) AS (values(&#39;a&#39;) UNION select chr(ascii(x)+1) from n where x &lt; &#39;z&#39;) select * from n{code} <br> <br>This will fail to process as the recursion limit will be reached before processing completes. <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Teiid supports non-recursive common table expressions via the WITH clause.&nbsp; WITH clause items may be referenced as tables in subsequent with clause items and in the main query.  The WITH clause can be thought of as providing query scoped temporary tables.</p>

<p>Usage:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">WITH name [(column, ...)] AS (query expression) ...</pre>
</div></div>

<p>Syntax Rules:</p>

<ul>
        <li>All of the projected column names must be unique.  If they are not unique, then the column name list must be provided.</li>
</ul>


<ul>
        <li>If the columns of the WITH clause item are declared, then they must match the number of columns projected by the query expression.</li>
</ul>


<ul>
        <li>Each with clause item must have a unique name.</li>
</ul>


<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>The WITH clause is also subject to optimization and it's entries may not be processed if they are not needed in the subsequent query.</td></tr></table></div>

<h3><a name="WITHClause-RecursiveCommonTableExpressions"></a>Recursive Common Table Expressions</h3>

<p>A recursive common table expression is a special form of a common table expression that is allowed to refer to itself to build the full common table result in a recursive or iterative fashion.</p>

<p>Usage:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">WITH name [(column, ...)] AS (anchor query expression UNION [ALL] recursive query expression) ...</pre>
</div></div>

<p>The recursive query expression is allowed to refer to the common table by name.  Processing flows with The anchor query expression executed first.  The results will be added to the common table and will be referenced for the execution of the recursive query expression.  The process will be repeated against the new results until there are no more intermediate results.  </p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>A non terminating recursive common table expression can lead to excessive processing.</td></tr></table></div>

<p>To prevent runaway processing of a recursive common table expression, processing is by default limited to 10000 iterations.  Recursive common table expressions that are pushed down are not subject to this limit, but may be subject to other source specific limits.  The limit can be modified by setting the session variable teiid.maxRecusion to a larger integer value.  Once the max has been exceeded an exception will be thrown.</p>

<p>Example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">SELECT teiid_session_set('teiid.maxRecursion', 25);
WITH n (x) AS (values('a') UNION select chr(ascii(x)+1) from n where x &lt; 'z') select * from n</pre>
</div></div>

<p>This will fail to process as the recursion limit will be reached before processing completes.</p>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://docs.jboss.org/author/users/removespacenotification.action?spaceKey=TEIID">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://docs.jboss.org/author/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://docs.jboss.org/author/display/TEIID/WITH+Clause">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646224&revisedVersion=4&originalVersion=3">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/WITH+Clause?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>