<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/Virtual+Procedures">Virtual Procedures</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" > <br>h1. Virtual Procedure Definition <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Usage:</span> <span class="diff-added-words"style="background-color: #dfd;">In Designer:</span> {code:SQL}[CREATE VIRTUAL PROCEDURE] statement{code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">In DDL: [DDL Metadata#Create Procedure/Function] <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Within the body of the procedure, any valid [statement|Procedure Language] may be used. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Virtual procedures are defined using the Teiid procedural language. A virtual procedure has zero or more input/inout/out parameters, an optional return parameter, and an optional result set. Virtual procedures support the ability to execute queries and other SQL commands, define temporary tables, add data to temporary tables, walk through result sets, use loops, and use conditional logic.</p>

<h1><a name="VirtualProcedures-VirtualProcedureDefinition"></a>Virtual Procedure Definition</h1>
<p>In Designer: <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 VIRTUAL PROCEDURE] statement</pre>
</div></div></p>

<p>In DDL: <a href="/author/display/TEIID/DDL+Metadata#DDLMetadata-CreateProcedure%2FFunction">DDL Metadata#Create Procedure/Function</a></p>

<p>Within the body of the procedure, any valid <a href="/author/display/TEIID/Procedure+Language" title="Procedure Language">statement</a> may be used.</p>

<p>There is no explicit cursoring or value returning statement, rather the last unnamed command statement executed in the procedure that returns a result set will be returned as the result. The output of that statement must match the expected result set and parameters of the procedure.</p>

<h1><a name="VirtualProcedures-ProcedureParameters"></a>Procedure Parameters</h1>
<p>Virtual procedures can take zero or more IN/INOUT parameters and may also have any number of OUT parameters and an optional RETURN parameter. Each input has the following information that is used during runtime processing: </p>

<ul>
        <li>Name - The name of the input parameter</li>
</ul>


<ul>
        <li>Datatype - The design&#45;time type of the input parameter</li>
</ul>


<ul>
        <li>Default value - The default value if the input parameter is not specified</li>
</ul>


<ul>
        <li>Nullable - NO&#95;NULLS, NULLABLE, NULLABLE&#95;UNKNOWN; parameter is optional if nullable, and is not required to be listed when using named parameter syntax<br/>
You reference a parameter in a virtual procedure by using the fully&#45;qualified name of the param &#40;or less if unambiguous). For example, MySchema.MyProc.Param1.</li>
</ul>


<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Example of Referencing an Input Parameter and Assigning an Out Parameter for 'GetBalance' Procedure</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
BEGIN 
  MySchema.GetBalance.RetVal = UPPER(MySchema.GetBalance.AcctID);
  SELECT Balance FROM MySchema.Accts WHERE MySchema.Accts.AccountID = MySchema.GetBalance.AcctID; 
END
</pre>
</div></div>

<p>If an INOUT parameter is not assigned any value in a procedure it will remain the value it was assigned for input. Any OUT/RETURN parameter not assigned a value will remain the as the default NULL value. The INOUT/OUT/RETURN output values are validated against the NOT NULL metadata of the parameter.</p>


<h1><a name="VirtualProcedures-ExampleVirtualProcedures"></a>Example Virtual Procedures</h1>
<p>This example is a LOOP that walks through a cursored table and uses CONTINUE and BREAK.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Virtual Procedure Using LOOP, CONTINUE, BREAK</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
BEGIN
  DECLARE double total;
  DECLARE integer transactions;
  LOOP ON (SELECT amt, type FROM CashTxnTable) AS txncursor
  BEGIN
    IF(txncursor.type &lt;&gt; 'Sale')
    BEGIN
      CONTINUE;
    END ELSE 
    BEGIN
      total = (total + txncursor.amt);
      transactions = (transactions + 1);
      IF(transactions = 100)
      BEGIN
        BREAK;
      END
    END
  END
  SELECT total, (total / transactions) AS avg_transaction;
END
</pre>
</div></div>

<p>This example is uses conditional logic to determine which of two SELECT statements to execute.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Virtual Procedure with Conditional SELECT</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
BEGIN 
  DECLARE string VARIABLES.SORTDIRECTION; 
  VARIABLES.SORTDIRECTION = PartsVirtual.OrderedQtyProc.SORTMODE; 
  IF ( ucase(VARIABLES.SORTDIRECTION) = 'ASC' ) 
  BEGIN 
    SELECT * FROM PartsVirtual.SupplierInfo WHERE QUANTITY &gt; PartsVirtual.OrderedQtyProc.QTYIN ORDER BY PartsVirtual.SupplierInfo.PART_ID; 
  END ELSE 
  BEGIN 
    SELECT * FROM PartsVirtual.SupplierInfo WHERE QUANTITY &gt; PartsVirtual.OrderedQtyProc.QTYIN ORDER BY PartsVirtual.SupplierInfo.PART_ID DESC;
  END
END 
</pre>
</div></div>

<h1><a name="VirtualProcedures-ExecutingVirtualProcedures"></a>Executing Virtual Procedures</h1>
<p>You execute procedures using the SQL <a href="/author/display/TEIID/DML+Commands#DMLCommands-EXECUTECommand">EXECUTE</a> command. If the procedure has defined inputs, you specify those in a sequential list, or using "name=value" syntax. You must use the name of the input parameter, scoped by the full procedure name if the parameter name is ambiguous in the context of other columns or variables in the procedure.</p>

<p> A virtual procedure call will return a result set just like any SELECT, so you can use this in many places you can use a SELECT. Typically you'll use the following 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;">SELECT * FROM (EXEC ...) AS x</pre>
</div></div>

<h1><a name="VirtualProcedures-Limitations"></a>Limitations</h1>
<p>Teiid virtual procedures may only return 1 result set.  If you need to pass in a result set or pass out multiple result set, then consider using global temporary tables.</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/Virtual+Procedures">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646267&revisedVersion=6&originalVersion=5">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/Virtual+Procedures?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>