<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/FROM+Clause">FROM 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 (2)</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" >* FROM table1 left outer join [/*\+ optional \*/|Federated Optimizations#Optional Join] table2 ON join-criteria <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >* FROM <span class="diff-changed-words">[TEXTTABLE...|<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">#</span>TEXTTABLE]</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>* FROM [XMLTABLE...|#XMLTABLE] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{note} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h1. TEXTTABLE <br> <br>The TEXTTABLE function processes character input to produce tabular ouptut.  It supports both fixed and delimited file format parsing. The function itself defines what columns it projects. The TEXTTABLE function is implicitly a nested table and may be correlated to preceding FROM clause entries. <br> <br>Usage: <br> <br>{code:lang=SQL}TEXTTABLE(expression [SELECTOR string] COLUMNS &lt;COLUMN&gt;, ... [NO ROW DELIMITER | ROW DELIMITER char] [DELIMITER char] [(QUOTE|ESCAPE) char] [HEADER [integer]] [SKIP integer] [NO TRIM]) AS name{code} <br> <br>Where &lt;COLUMN&gt; <br> <br>{code:lang=SQL}COLUMN := name (FOR ORDINALITY | datatype [WIDTH integer [NO TRIM]] [SELECTOR string integer]){code} <br> <br>Parameters <br> <br>* expression - the text content to process, which should be convertible to CLOB. <br> <br>* SELECTOR is used with files containing multiple types of rows (example: order header, detail, summary).  A TEXTTABLE SELECTOR specifies which lines to include in the output.  Matching lines must begin with the selector string.  The selector in column delimited files must be followed by the column delimiter. <br>** If a TEXTTABLE SELECTOR is specified, a SELECTOR may also be specified for column values.  A column SELECTOR argument will select the nearest preceding text line with the given SELECTOR prefix and select the value at the given 1-based integer position (which includes the selector itself). If no such text line or position with a given line exists, a null value will be produced.  A column SELECTOR is not valid with fixed width parsing. <br> <br>* NO ROW DELIMITER indicates that fixed parsing should not assume the presence of newline row delimiters. <br> <br>* ROW DELIMITER sets the row delimiter / new line to an alternate character.  Defaults to the new line character - with built in handling for treating carriage return new line as a single character.  If ROW DELIMITER is specified, carriage return will be given no special treatment. <br> <br>* DELIMITER sets the field delimiter character to use.  Defaults to &#39;,&#39;. <br> <br>* QUOTE sets the quote, or qualifier, character used to wrap field values.  Defaults to &#39;&quot;&#39;. <br> <br>* ESCAPE sets the escape character to use if no quoting character is in use. This is used in situations where the delimiter or new line characters are escaped with a preceding character, e.g. \, <br> <br>* HEADER specifies the text line number (counting every new line) on which the column names occur.  All lines prior to the header will be skipped. If HEADER is specified, then the header line will be used to determine the TEXTTABLE column position by case-insensitive name matching.  This is especially useful in situations where only a subset of the columns are needed. If the HEADER value is not specified, it defaults to 1. If HEADER is not specified, then columns are expected to match positionally with the text contents. <br> <br>* SKIP specifies the number of text lines (counting every new line) to skip before parsing the contents.  HEADER may still be specified with SKIP. <br> <br>* A FOR ORDINALITY column is typed as integer and will return the 1-based item number as its value. <br> <br>* WIDTH indicates the fixed-width length of a column in characters - not bytes.  With the default ROW DELIMITER, a CR NL sequence counts as a single character. <br> <br>* NO TRIM specified on the TEXTTABLE, it will affect all column and header values. If NO TRIM is specified on a column, then the fixed or unqualified text value not be trimmed of leading and trailing white space. <br> <br>Syntax Rules: <br> <br>* If width is specified for one column it must be specified for all columns and be a non-negative integer. <br> <br>* If width is specified, then fixed width parsing is used ESCAPE, QUOTE, column SELECTOR, nor HEADER should not be specified. <br> <br>* If width is not specified, then NO ROW DELIMITER cannot be used. <br> <br>* The columns names must not contain duplicates. <br> <br>* The QUOTE, DELIMITER, and ROW DELIMITER must all be different characters. <br> <br>Examples <br> <br>* Use of the HEADER parameter, returns 1 row \[&#39;b&#39;\]: <br>{code:lang=SQL}SELECT * FROM TEXTTABLE(UNESCAPE(&#39;col1,col2,col3\na,b,c&#39;) COLUMNS col2 string HEADER) x{code} <br> <br>* Use of fixed width, returns 2 rows \[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;\], \[&#39;d&#39;, &#39;e&#39;, &#39;f&#39;\]: <br>{code:lang=SQL}SELECT * FROM TEXTTABLE(UNESCAPE(&#39;abc\ndef&#39;) COLUMNS col1 string width 1, col2 string width 1, col3 string width 1) x{code} <br> <br>* Use of fixed width without a row delimiter, returns 3 rows \[&#39;a&#39;\], \[&#39;b&#39;\], \[&#39;c&#39;\]: {code:lang=SQL}SELECT * FROM TEXTTABLE(&#39;abc&#39; COLUMNS col1 string width 1 NO ROW DELIMITER) x{code} <br> <br>* Use of ESCAPE parameter, returns 1 row \[&#39;a,&#39;, &#39;b&#39;\]: <br>{code:lang=SQL}SELECT * FROM TEXTTABLE(&#39;a:,,b&#39; COLUMNS col1 string, col2 string ESCAPE &#39;:&#39;) x{code} <br> <br>* As a nested table: <br>{code:lang=SQL}SELECT x.* FROM t, TEXTTABLE(t.clobcolumn COLUMNS first string, second date SKIP 1) x{code} <br> <br>* Use of SELECTORs, returns 2 rows \[&#39;c&#39;, &#39;d&#39;, &#39;b&#39;\], \[&#39;c&#39;, &#39;f&#39;, &#39;b&#39;\]: <br>{code:lang=SQL}SELECT * FROM TEXTTABLE(&#39;a,b\nc,d\nc,f&#39; SELECTOR &#39;c&#39; COLUMNS col1 string, col2 string col3 string SELECTOR &#39;a&#39; 2) x{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. XMLTABLE <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{info}See also [XQuery Optimization] <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>The FROM clause specifies the target table(s) for SELECT, UPDATE, and DELETE statements.</p>

<p>Example Syntax:</p>

<ul>
        <li>FROM table [[AS] alias]</li>
</ul>


<ul>
        <li>FROM table1 [INNER&#124;LEFT OUTER&#124;RIGHT OUTER&#124;FULL OUTER] JOIN table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM table1 CROSS JOIN table2</li>
</ul>


<ul>
        <li>FROM (subquery) [AS] alias</li>
</ul>


<ul>
        <li>FROM <a href="#FROMClause-NestedTableReference">TABLE(subquery)</a> [AS] alias</li>
</ul>


<ul>
        <li>FROM table1 JOIN /*&#43; MAKEDEP &#42;/ table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM table1 JOIN /*&#43; MAKENOTDEP &#42;/ table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM /*&#43; MAKEIND &#42;/ table1 JOIN table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM /*&#43; NO_UNNEST &#42;/ vw1 JOIN table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM table1 left outer join <a href="/author/display/TEIID/Federated+Optimizations#FederatedOptimizations-OptionalJoin">/*&#43; optional &#42;/</a> table2 ON join-criteria</li>
</ul>


<ul>
        <li>FROM <a href="/author/display/TEIID/TEXTTABLE" title="TEXTTABLE">TEXTTABLE...</a></li>
</ul>


<ul>
        <li>FROM <a href="#FROMClause-XMLTABLE">XMLTABLE...</a></li>
</ul>


<ul>
        <li>FROM <a href="/author/display/TEIID/ARRAYTABLE" title="ARRAYTABLE">ARRAYTABLE...</a></li>
</ul>


<ul>
        <li>FROM <a href="/author/display/TEIID/OBJECTTABLE" title="OBJECTTABLE">OBJECTTABLE...</a></li>
</ul>


<ul>
        <li>FROM (<a href="/author/display/TEIID/Subqueries#Subqueries-Inlineviews">SELECT ...</a></li>
</ul>


<h1><a name="FROMClause-FromClauseHints"></a>From Clause Hints</h1>

<p>From clause hints are typically specified in a comment block preceding the affected clause.  If multiple hints apply to that clause, the hints should be placed in the same comment block.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Example Hint</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
FROM /*+ MAKEDEP PRESERVE */ (tbl1 inner join tbl2 inner join tbl3 on tbl2.col1 = tbl3.col1 on tbl1.col1 = tbl2.col1), tbl3 WHERE tbl1.col1 = tbl2.col1
</pre>
</div></div>

<h3><a name="FROMClause-DependentJoins"></a>Dependent Joins</h3>

<p>MAKEIND, MAKEDEP, and MAKENOTDEP are hints used to control <a href="/author/display/TEIID/Federated+Optimizations#FederatedOptimizations-DependentJoins">dependent join</a> behavior. They should only be used in situations where the optimizer does not choose the most optimal plan based upon query structure, metadata, and costing information.  The hints may appear in a comment that proceeds the from clause. The hints can be specified against any from clause, not just a named table.</p>

<ul>
        <li>MAKEIND - treat this clause as the independent (feeder) side of a dependent join if possible.</li>
        <li>MAKEDEP - treat this clause as the dependent (filtered) side of a dependent join if possible.</li>
        <li>MAKENOTDEP - do not treat this clause as the dependent (filtered) side of a join.</li>
</ul>


<h3><a name="FROMClause-OtherHints"></a>Other Hints</h3>

<p>NO_UNNEST can be specified against a subquery from clause or view to instruct the planner to not merge the nested SQL in the surrounding query - also known as view flattening.  This hint only applies to Teiid planning and is not passed to source queries.  NO_UNNEST may appear in a comment that proceeds the from clause.</p>

<p>The PRESERVE hint can be used against an ANSI join tree to preserve the structure of the join rather than allowing the Teiid optimizer to reorder the join.  This is similar in function to the Oracle ORDERED or MySQL STRAIGHT_JOIN hints.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Example PRESERVE Hint</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
FROM /*+ PRESERVE */ (tbl1 inner join tbl2 inner join tbl3 on tbl2.col1 = tbl3.col1 on tbl1.col1 = tbl2.col1)
</pre>
</div></div>

<h1><a name="FROMClause-NestedTableReference"></a>Nested Table Reference</h1>

<p>Nested tables may appear in the FROM clause with the TABLE keyword. They are an alternative to using a view with normal join semantics.  The columns projected from the command contained in the nested table may be used just as any of the other FROM clause projected columns in join criteria, the where clause, etc.</p>

<p>A nested table may have correlated references to preceding FROM clause column references as long as INNER and LEFT OUTER joins are used. This is especially useful in cases where then nested expression is a procedure or function call.</p>

<p>Valid 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 * from t1, TABLE(call proc(t1.x)) t2</pre>
</div></div>

<p>Invalid example, since t1 appears after the nested table in the from clause:</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 * from TABLE(call proc(t1.x)) t2, t1</pre>
</div></div>

<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><b>Multiple Execution</b><br />The usage of a correlated nested table may result in multiple executions of the table expression - once for each correlated row.</td></tr></table></div>

<h1><a name="FROMClause-XMLTABLE"></a>XMLTABLE</h1>

<p>The XMLTABLE function uses XQuery to produce tabular ouput. The XMLTABLE function is implicitly a nested table and may be correlated to preceding FROM clause entries.  XMLTABLE is part of the SQL/XML 2006 specification.</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;">XMLTABLE([&lt;NSP&gt;,] xquery-expression [&lt;PASSING&gt;] [COLUMNS &lt;COLUMN&gt;, ... )] AS name</pre>
</div></div>

<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;">COLUMN := name (FOR ORDINALITY | (datatype [DEFAULT expression] [PATH string]))</pre>
</div></div>

<p>See <a href="/author/display/TEIID/XML+Functions#XMLFunctions-XMLELEMENT">XMLELEMENT</a> for the definition of NSP - XMLNAMESPACES.</p>

<p>See <a href="/author/display/TEIID/XML+Functions#XMLFunctions-XMLQUERY">XMLQUERY</a> for the definition of PASSING.</p>

<p>See also <a href="/author/display/TEIID/XML+Functions#XMLFunctions-XMLQUERY">XMLQUERY</a></p>


<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>See also <a href="/author/display/TEIID/XQuery+Optimization" title="XQuery Optimization">XQuery Optimization</a></td></tr></table></div>

<p>Parameters</p>

<ul>
        <li>The optional XMLNAMESPACES clause specifies the namepaces for use in the XQuery and COLUMN path expressions.</li>
</ul>


<ul>
        <li>The xquery-expression should be a valid XQuery.  Each sequence item returned by the xquery will be used to create a row of values as defined by the COLUMNS clause.</li>
</ul>


<ul>
        <li>If COLUMNS is not specified, then that is the same as having the COLUMNS clause: "COLUMNS OBJECT_VALUE XML PATH '.'", which returns the entire item as an XML value.</li>
</ul>


<ul>
        <li>A FOR ORDINALITY column is typed as integer and will return the 1-based item number as its value.</li>
</ul>


<ul>
        <li>Each non-ordinality column specifies a type and optionally a PATH and a DEFAULT expression.</li>
</ul>


<ul>
        <li>If PATH is not specified, then the path will be the same as the column name.<br/>
Syntax Rules:</li>
</ul>


<ul>
        <li>Only 1 FOR ORDINALITY column may be specified.</li>
</ul>


<ul>
        <li>The columns names must not contain duplicates.</li>
</ul>


<ul>
        <li>The blob datatype is supported, but there is only built-in support for xs:hexBinary values.&nbsp; For xs:base64Binary, use a workaround of a PATH that uses the explicit value constructor "xs:base64Binary(&lt;path&gt;)".</li>
</ul>


<ul>
        <li>The column expression must evaluate to a single value if a non-array type is expected.</li>
</ul>


<p>Examples</p>

<p>Use of passing, returns 1 row [1]:</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 * from xmltable('/a' PASSING xmlparse(document '&lt;a id="1"/&gt;') COLUMNS id integer PATH '@id') x</pre>
</div></div>

<p>As a nested table:</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 x.* from t, xmltable('/x/y' PASSING t.doc COLUMNS first string, second FOR ORDINALITY) x</pre>
</div></div>

<p>Invalid multi-value:</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 * from xmltable('/a' PASSING xmlparse(document '&lt;a&gt;&lt;b id="1"/&gt;&lt;b id="2"/&gt;&lt;/a&gt;') COLUMNS id integer PATH 'b/@id') x</pre>
</div></div>

<p>Array multi-value:</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 * from xmltable('/a' PASSING xmlparse(document '&lt;a&gt;&lt;b id="1"/&gt;&lt;b id="2"/&gt;&lt;/a&gt;') COLUMNS id integer[] PATH 'b/@id') x</pre>
</div></div>
    </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/FROM+Clause">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646226&revisedVersion=19&originalVersion=18">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/FROM+Clause?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>