<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/Temp+Tables">Temp Tables</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</a>
    </h4>
        <br/>
                         <h4>Changes (12)</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" >Creation syntax: <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Explicit: {code:lang=SQL}CREATE LOCAL TEMPORARY TABLE <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">x</span> <span class="diff-added-words"style="background-color: #dfd;">name</span> (column type [NOT NULL], ... [PRIMARY KEY (column, ...)]){code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >Implicit: {code:SQL}INSERT INTO <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">#x</span> <span class="diff-added-words"style="background-color: #dfd;">#name</span> (column, ...) VALUES (value, ...) {code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>If #x doesn&#39;t exist, it will be defined using the given column names and types from the value expressions. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Implicit: {code:SQL}INSERT INTO <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">#x</span> <span class="diff-added-words"style="background-color: #dfd;">#name</span> [(column, ...)] select c1, c2 from t{code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>If #x doesn&#39;t exist, it will be defined using the target column names \(in not supplied, the column names will match the derived column names from the query), and the types from the query derived columns.  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Drop syntax: <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >* {code:SQL}DROP TABLE <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">x{code}</span> <span class="diff-added-words"style="background-color: #dfd;">name{code}</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Primary Key Support <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >A foreign temporary table requires explicit creation syntax: <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >{code:lang=SQL}CREATE FOREIGN TEMPORARY TABLE <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">x</span> <span class="diff-added-words"style="background-color: #dfd;">name</span> ... ON schema{code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >Where the table creation body syntax is the same as a standard CREATE FOREIGN TABLE [DDL statement|DDL Metadata]. <span class="diff-added-words"style="background-color: #dfd;"> In general usage of DDL OPTION clauses may be required to properly access the source table, including setting the name in source, updatability, native types, etc.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The schema name must specify an existing schema/model in the VDB.  The table will be accessed as if it is on that source, however within Teiid the temporary table will still be scoped the same as a non-foreign temporary table.  This means that the foreign temporary table will not belong to a Teiid schema and will be scoped to the session or procedure block where created.   <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Neither the CREATE nor a corresponding DROP of a foreign temporary table issue a pushdown command. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The DROP syntax for a foreign temporary table is the same as for a non-foreign temporary table.   <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Neither a CREATE nor a corresponding DROP of a foreign temporary table issue a pushdown command, rather this mechanism simply exposes a source table for use within Teiid on a temporary basis. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >There are two usage scenarios for a FOREIGN TEMPORARY TABLE.  The first is to dynamically access additional tables on the source.  The other is to replace the usage of a Teiid local temporary table for performance reasons.  The usage pattern for the latter case would look like: <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >source.native(&quot;CREATE GLOBAL TEMPORARY TABLE name IF NOT EXISTS ON COMMIT DELETE ROWS&quot;); <br>//- bring the table into Teiid <br></td></tr>
            <tr><td class="diff-changed-lines" >CREATE FOREIGN TEMPORARY TABLE name ... <span class="diff-added-words"style="background-color: #dfd;">OPTIONS (UPDATABLE true)</span> <br></td></tr>
            <tr><td class="diff-unchanged" >//- use the table <br>... <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Note the usage of the native procedure to pass source specific CREATE ddl to the source.  Teiid does not currently attempt to pushdown a source creation of a temporary table based upon the CREATE statement.  Some other mechanism, such as the native procedure shown above, must be used to first create the table.  Also note the table is explicitly marked as updatable, since DDL defined tables are not updatable by default.   <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Note the usage of the native procedure to pass source specific CREATE ddl to the source.  Teiid does not currently attempt to pushdown a source creation of a temporary table based upon the CREATE statement.  Some other mechanism, such as the native procedure shown above, must be used to first create the table. </span> The source&#39;s handling of temporary tables must also be understood to make this work as intended.  Sources that use the same GLOBAL table definition for all sessions while scoping the data to be session specific (such as Oracle) or sources that support session scoped temporary tables (such as PostgreSQL) will work if accessed under a transaction.  A transaction is necessary because: <br></td></tr>
            <tr><td class="diff-unchanged" >* the source on commit behavior (most likely DELETE ROWS or DROP) will ensure clean-up.  Keep in mind that a Teiid drop does not issue a source command and is not guaranteed to occur (in some exception cases, loss of db connectivity, hard shutdown, etc.). <br>* the source pool when using track connections by transaction will ensure that multiple uses of that source by Teiid will use the same connection/session and thus the same temporary table and data. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Teiid supports creating temporary,or "temp", tables. Temp tables are dynamically created, but are treated as any other physical table.</p>

<p>Temp tables can be defined implicitly by referencing them in a INSERT statement or explicitly with a CREATE TABLE statement. Implicitly created temp tables must have a name that starts with '#'.</p>

<p>Creation syntax:</p>

<p>Explicit: <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;">CREATE LOCAL TEMPORARY TABLE name (column type [NOT NULL], ... [PRIMARY KEY (column, ...)])</pre>
</div></div></p>

<p>Implicit: <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;">INSERT INTO #name (column, ...) VALUES (value, ...) </pre>
</div></div></p>

<p>If #x doesn't exist, it will be defined using the given column names and types from the value expressions.</p>

<p>Implicit: <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;">INSERT INTO #name [(column, ...)] select c1, c2 from t</pre>
</div></div></p>

<p>If #x doesn't exist, it will be defined using the target column names &#40;in not supplied, the column names will match the derived column names from the query), and the types from the query derived columns. </p>

<ul>
        <li>Use the SERIAL data type to specify a NOT NULL and auto&#45;incrementing INTEGER column.  The starting value of a SERIAL column is 1.</li>
</ul>


<p>Drop syntax:</p>

<ul>
        <li><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;">DROP TABLE name</pre>
</div></div></li>
</ul>


<p>Primary Key Support</p>

<ul>
        <li>All key columns must be comparable.</li>
</ul>


<ul>
        <li>Use of a primary key creates a clustered index that supports search improvements for comparison, in, like, and order by.</li>
</ul>


<ul>
        <li>Null is an allowable primary key value, but there must be only 1 row that has an all null key.<br/>
Limitations:</li>
</ul>


<ul>
        <li>With the CREATE TABLE syntax only basic table definition &#40;column name and type information) and an optional primary key are supported.</li>
</ul>


<ul>
        <li>The "ON COMMIT" clause is not supported in the CREATE TABLE statement.</li>
</ul>


<ul>
        <li>"drop behavior" option is not supported in the drop statement.</li>
</ul>


<ul>
        <li>Only local temporary tables are supported. This implies that the scope of temp table will be either to the session or the block of a virtual procedure that creates it.</li>
</ul>


<ul>
        <li>Session level temp tables are not fail&#45;over safe.</li>
</ul>


<ul>
        <li>Temp tables support a READ&#95;UNCOMMITED transaction isolation level.  There are no locking mechanisms available to support higher isolation levels and the result of a rollback may be inconsistent across multiple transactions. If concurrent transactions are not associated with the same local temporary table or session, then the transaction isolation level is effectively SERIALIZABLE. If you want full consistency with local temporary tables, then only use a connection with 1 transaction at a time.  This mode of operation is ensured by connection pooling that tracks connections by transaction.</li>
</ul>


<ul>
        <li>Lob values &#40;xml, clob, blob) are tracked by reference rather than by value in a temporary table. Lob values from external sources that are inserted in a temporary table may become unreadable when the associated statement or connection is closed.</li>
</ul>


<p>The following example is a series of statements that loads a temporary table with data from 2 sources, and with a manually inserted record, and then uses that temp table in a subsequent query.</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;">... 
CREATE LOCAL TEMPORARY TABLE TEMP (a integer, b integer, c integer); 
SELECT * INTO temp FROM Src1; SELECT * INTO temp FROM Src2; 
INSERT INTO temp VALUES (1,2,3); 
SELECT a,b,c FROM Src3, temp WHERE Src3.a = temp.b; 
...</pre>
</div></div>

<p>See <a href="/author/display/TEIID/Virtual+Procedures" title="Virtual Procedures">Virtual Procedures</a> for more on temp table usage.</p>

<h2><a name="TempTables-ForeignTemporaryTables"></a>Foreign Temporary Tables</h2>

<p>Unlike a Teiid local temporary table, a foreign temporary table is a reference to an actual source table that is created at runtime rather than during the metadata load.</p>

<p>A foreign temporary table requires explicit creation syntax:</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;">CREATE FOREIGN TEMPORARY TABLE name ... ON schema</pre>
</div></div>

<p>Where the table creation body syntax is the same as a standard CREATE FOREIGN TABLE <a href="/author/display/TEIID/DDL+Metadata" title="DDL Metadata">DDL statement</a>.  In general usage of DDL OPTION clauses may be required to properly access the source table, including setting the name in source, updatability, native types, etc.</p>

<p>The schema name must specify an existing schema/model in the VDB.  The table will be accessed as if it is on that source, however within Teiid the temporary table will still be scoped the same as a non-foreign temporary table.  This means that the foreign temporary table will not belong to a Teiid schema and will be scoped to the session or procedure block where created.  </p>

<p>The DROP syntax for a foreign temporary table is the same as for a non-foreign temporary table.  </p>

<p>Neither a CREATE nor a corresponding DROP of a foreign temporary table issue a pushdown command, rather this mechanism simply exposes a source table for use within Teiid on a temporary basis.</p>

<p>There are two usage scenarios for a FOREIGN TEMPORARY TABLE.  The first is to dynamically access additional tables on the source.  The other is to replace the usage of a Teiid local temporary table for performance reasons.  The usage pattern for the latter case would look like:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
//- create the source table
source.native("CREATE GLOBAL TEMPORARY TABLE name IF NOT EXISTS ON COMMIT DELETE ROWS");
//- bring the table into Teiid
CREATE FOREIGN TEMPORARY TABLE name ... OPTIONS (UPDATABLE true)
//- use the table
...
//- forget the table
DROP TABLE name
</pre>
</div></div>

<p>Note the usage of the native procedure to pass source specific CREATE ddl to the source.  Teiid does not currently attempt to pushdown a source creation of a temporary table based upon the CREATE statement.  Some other mechanism, such as the native procedure shown above, must be used to first create the table.  Also note the table is explicitly marked as updatable, since DDL defined tables are not updatable by default.  </p>

<p>The source's handling of temporary tables must also be understood to make this work as intended.  Sources that use the same GLOBAL table definition for all sessions while scoping the data to be session specific (such as Oracle) or sources that support session scoped temporary tables (such as PostgreSQL) will work if accessed under a transaction.  A transaction is necessary because:</p>
<ul>
        <li>the source on commit behavior (most likely DELETE ROWS or DROP) will ensure clean-up.  Keep in mind that a Teiid drop does not issue a source command and is not guaranteed to occur (in some exception cases, loss of db connectivity, hard shutdown, etc.).</li>
        <li>the source pool when using track connections by transaction will ensure that multiple uses of that source by Teiid will use the same connection/session and thus the same temporary table and data.</li>
</ul>


<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>Since Teiid does not yet support the ON COMMIT clause it's important to consider that the source table ON COMMIT behavior will likely be different that the default, PRESERVE ROWS, for Teiid local temporary tables.</td></tr></table></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/Temp+Tables">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646236&revisedVersion=8&originalVersion=7">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/Temp+Tables?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>