<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head profile="http://internetalchemy.org/2003/02/profile"> <link rel="foaf" type="application/rdf+xml" title="FOAF" href="http://www.openlinksw.com/dataspace/uda/about.rdf" /> <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" /> <meta name="dc.title" content="Function: exec - Virtuoso Functions Guide" /> <meta name="dc.subject" content="Function: exec - Virtuoso Functions Guide" /> <meta name="dc.creator" content="OpenLink Software Documentation Team ; " /> <meta name="dc.copyright" content="OpenLink Software, 1999 - 2009" /> <link rel="top" href="index.html" title="OpenLink Virtuoso Universal Server: Documentation" /> <link rel="search" href="/doc/adv_search.vspx" title="Search OpenLink Virtuoso Universal Server: Documentation" /> <link rel="parent" href="functions.html" title="Chapter Contents" /> <link rel="prev" href="appendixa.html" title="Appendix" /> <link rel="next" href="functions.html#.html" title="" /> <link rel="shortcut icon" href="../images/misc/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" type="text/css" href="doc.css" /> <link rel="stylesheet" type="text/css" href="/doc/translation.css" /> <title>Function: exec - Virtuoso Functions Guide</title> <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" /> <meta name="author" content="OpenLink Software Documentation Team ; " /> <meta name="copyright" content="OpenLink Software, 1999 - 2009" /> <meta name="keywords" content="" /> <meta name="GENERATOR" content="OpenLink XSLT Team" /> </head> <body> <div id="header"> <a name="fn_exec" /> <img src="../images/misc/logo.jpg" alt="" /> <h1>22. Virtuoso Functions Guide - exec</h1> </div> <div id="navbartop"> <div> <a class="link" href="functions.html">Chapter Contents</a> | <a class="link" href="fn_end_result.html" title="end_result">Prev</a> | <a class="link" href="fn_exec_close.html" title="close">Next</a> </div> </div> <div id="currenttoc"> <form method="post" action="/doc/adv_search.vspx"> <div class="search">Keyword Search: <br /> <input type="text" name="q" /> <input type="submit" name="go" value="Go" /> </div> </form> <div> <a href="http://www.openlinksw.com/">www.openlinksw.com</a> </div> <div> <a href="http://docs.openlinksw.com/">docs.openlinksw.com</a> </div> <br /> <div> <a href="index.html">Book Home</a> </div> <br /> <div> <a href="contents.html">Contents</a> </div> <div> <a href="preface.html">Preface</a> </div> <br /> <div> <a class="selected" href="functions.html">Virtuoso Functions Guide</a> </div> <br /> <div> <a href="functions.html#admin">Administration</a> </div> <div> <a href="functions.html#aggr">Aggregate Functions</a> </div> <div> <a href="functions.html#array">Array Manipulation</a> </div> <div> <a href="functions.html#bpel">BPEL APIs</a> </div> <div> <a href="functions.html#backup">Backup</a> </div> <div> <a href="functions.html#compression">Compression</a> </div> <div> <a href="functions.html#cursors">Cursor</a> </div> <div> <a href="functions.html#time">Date & Time Manipulation</a> </div> <div> <a href="functions.html#debug">Debug</a> </div> <div> <a href="functions.html#dict">Dictionary Manipulation</a> </div> <div> <a href="functions.html#encoding">Encoding & Decoding</a> </div> <div> <a href="functions.html#file">File Manipulation</a> </div> <div> <a href="functions.html#ft">Free Text</a> </div> <div> <a href="functions.html#hash">Hashing / Cryptographic</a> </div> <div> <a href="functions.html#ldap">LDAP</a> </div> <div> <a href="functions.html#localization">Locale</a> </div> <div> <a href="functions.html#mail">Mail</a> </div> <div> <a href="functions.html#misc">Miscellaneous</a> </div> <div> <a href="functions.html#number">Number</a> </div> <div> <a href="functions.html#phrz">Phrases</a> </div> <div> <a href="functions.html#rdf">RDF data</a> </div> <div> <a href="functions.html#rmt">Remote SQL Data Source</a> </div> <div> <a href="functions.html#repl">Replication</a> </div> <div> <a href="functions.html#soap">SOAP</a> </div> <div> <a class="selected" href="functions.html#sql">SQL</a> </div> <div class="selected"> <div> <a href="fn___any_grants.html">__any_grants</a> </div> <div> <a href="fn_exec_close.html">close</a> </div> <div> <a href="fn_collation_define.html">collation_define</a> </div> <div> <a href="fn_complete_table_name.html">complete_table_name</a> </div> <div> <a href="fn_delay.html">delay</a> </div> <div> <a href="fn_end_result.html">end_result</a> </div> <div> <a class="selected" href="fn_exec.html">exec</a> </div> <div> <a href="fn_exec_metadata.html">exec_metadata</a> </div> <div> <a href="fn_exec_next.html">exec_next</a> </div> <div> <a href="fn_exec_result.html">exec_result</a> </div> <div> <a href="fn_exec_result_names.html">exec_result_names</a> </div> <div> <a href="fn_exec_score.html">exec_score</a> </div> <div> <a href="fn_identity_value.html">identity_value</a> </div> <div> <a href="fn_name_part.html">name_part</a> </div> <div> <a href="fn_registry_get.html">registry_get</a> </div> <div> <a href="fn_registry_get_all.html">registry_get_all</a> </div> <div> <a href="fn_registry_name_is_protected.html">registry_name_is_pro...</a> </div> <div> <a href="fn_registry_remove.html">registry_remove</a> </div> <div> <a href="fn_registry_set.html">registry_set</a> </div> <div> <a href="fn_result.html">result</a> </div> <div> <a href="fn_result_names.html">result_names</a> </div> <div> <a href="fn_row_count.html">row_count</a> </div> <div> <a href="fn_sequence_get_all.html">sequence_get_all</a> </div> <div> <a href="fn_sequence_next.html">sequence_next</a> </div> <div> <a href="fn_sequence_remove.html">sequence_remove</a> </div> <div> <a href="fn_sequence_set.html">sequence_set</a> </div> <div> <a href="fn_set_identity_column.html">set_identity_column</a> </div> <div> <a href="fn_set_row_count.html">set_row_count</a> </div> <div> <a href="fn_set_user_id.html">set_user_id</a> </div> <div> <a href="fn_signal.html">signal</a> </div> <div> <a href="fn_sinv_create_inverse.html">sinv_create_inverse</a> </div> <div> <a href="fn_sinv_create_key_mapping.html">sinv_create_key_mapp...</a> </div> <div> <a href="fn_sinv_drop_inverse.html">sinv_drop_inverse</a> </div> <div> <a href="fn_sys_stat_analyze.html">sys_stat_analyze</a> </div> <div> <a href="fn_sys_stat_histogram.html">sys_stat_histogram</a> </div> <div> <a href="fn_table_drop_policy.html">table_drop_policy</a> </div> <div> <a href="fn_table_set_policy.html">table_set_policy</a> </div> <div> <a href="fn_username.html">username</a> </div> </div> <div> <a href="functions.html#string">String</a> </div> <div> <a href="functions.html#txn">Transaction</a> </div> <div> <a href="functions.html#type">Type Mapping</a> </div> <div> <a href="functions.html#uddi">UDDI</a> </div> <div> <a href="functions.html#udt">User Defined Types & The CLR</a> </div> <div> <a href="functions.html#vad">VAD</a> </div> <div> <a href="functions.html#jvmpl">Virtuoso Java PL API</a> </div> <div> <a href="functions.html#bif">Virtuoso Server Extension Interface (VSEI)</a> </div> <div> <a href="functions.html#ws">Web & Internet</a> </div> <div> <a href="functions.html#xml">XML</a> </div> <div> <a href="functions.html#XPATH">XPATH & XQUERY</a> </div> <br /> <div> <a href="functionidx.html">Functions Index</a> </div> <br /> </div> <div id="text"> <h2>exec</h2> <div class="refpurpose">dynamic execution of SQL returning state and result set</div> <div class="funcsynopsis"> <span class="funcdef"> <span class="function">exec</span> </span> (<span class="paramdef">in <span class="parameter">str</span> varchar</span>, <span class="paramdef">out <span class="parameter">state</span> varchar</span>, <span class="paramdef">out <span class="parameter">message</span> varchar</span>, <span class="paramdef">in <span class="parameter">params</span> any</span>, <span class="paramdef">in <span class="parameter">maxrows</span> integer</span>, <span class="paramdef">out <span class="parameter">metadata</span> vector</span>, <span class="paramdef">out <span class="parameter">rows</span> vector</span>, <span class="paramdef">out <span class="parameter">cursor_handle</span> long</span>); </div> <div class="refsect1"> <div class="refsect1title">Description</div> <p>This function provides dynamic SQL capabilities in Virtuoso PL. The first argument is an arbitrary SQL statement, which may contain parameter placeholders. The function returns as output parameters a SQL state, error message, column metadata and result set rows if the statement is a select.</p> <p>A stored procedure can be invoked by exec but a procedure's result set will not be received in the rows output parameter but rather sent to the client.</p> </div> <div class="refsect1"> <div class="refsect1title">Parameters</div> <div class="refsect2"> <span class="refsect2title">str – </span> A <span class="computeroutput">varchar</span> containing arbitrary SQL using ?'s for parameter markers. </div> <div class="refsect2"> <span class="refsect2title">state – </span> An output parameter of type <span class="computeroutput">varchar</span> set to the 5 character SQL state if the exec resulted an error. Not set if an error is not present. </div> <div class="refsect2"> <span class="refsect2title">message – </span> An output parameter of type <span class="computeroutput">varchar</span> set to SQL error message associated with the error. Not set if an error is not present. </div> <div class="refsect2"> <span class="refsect2title">params – </span> A <span class="computeroutput">vector</span> containing the parameters for the SQL being executed. Element 0 corresponding to first ?, etc. </div> <div class="refsect2"> <span class="refsect2title">maxrows – </span> The <span class="computeroutput">integer</span> maximum number of rows to retrieve in case of a statement returning a result set. </div> <div class="refsect2"> <span class="refsect2title">metadata – </span> An output parameter of type <span class="computeroutput">vector</span> returning the metadata of the statement and its result. <table class="data"> <caption>Table: 22.1. The stmt_meta array</caption> <tr> <th class="data">Element</th> <th class="data">Name</th> <th class="data">Description</th> </tr> <tr> <td class="data">0</td> <td class="data">COLS</td> <td class="data">An array containing description of each column in the result set (see table below for contents)</td> </tr> <tr> <td class="data">1</td> <td class="data">STMT_SELECT</td> <td class="data">An <span class="computeroutput">integer</span> 1 indicates that the statement is a select, otherwise it is a DML statement.</td> </tr> <tr> <td class="data">2..</td> <td class="data">N/A</td> <td class="data">Trailing elements may appear, but they should not be used</td> </tr> </table> <br /> <table class="data"> <caption>Table: 22.2. Columns array of metadata</caption> <tr> <th class="data">Element</th> <th class="data">Name</th> <th class="data">Description</th> </tr> <tr> <td class="data">0</td> <td class="data">name</td> <td class="data">Column name</td> </tr> <tr> <td class="data">1</td> <td class="data">type</td> <td class="data">Column type as an internal type code corresponding, but not equal to ODBC SQL type codes.</td> </tr> <tr> <td class="data">2</td> <td class="data">scale</td> <td class="data">column scale</td> </tr> <tr> <td class="data">3</td> <td class="data">precision</td> <td class="data">column precision</td> </tr> <tr> <td class="data">4</td> <td class="data">nullable</td> <td class="data">indicates nullable column</td> </tr> <tr> <td class="data">5</td> <td class="data">updatable</td> <td class="data">indicates updatable column</td> </tr> <tr> <td class="data">6</td> <td class="data">searchable</td> <td class="data">indicates searchable column</td> </tr> </table> <br /> </div> <div class="refsect2"> <span class="refsect2title">rows – </span> An output array with one element per result row containing an array with the leftmost column as element 0 and so forth. </div> <div class="refsect2"> <span class="refsect2title">cursor_handle – </span> The cursor handle for use with related functions. </div> </div> <div class="refsect1"> <div class="refsect1title">Examples</div> <a name="ex_exec" /> <div class="example"> <div class="exampletitle">Procedure Example</div> <p>This stored procedure returns 1 if a given table is empty. An error such as a timeout or deadlock would be reported back to the caller as an exception. Exec always returns, no matter the type of exception. Thus it is also useful as a universal error catcher.</p> <div> <pre class="screen"> create procedure tb_is_empty (in tb varchar) { declare state, msg, 1, descs, rows any; state := '00000'; exec (sprintf ('select 1 from %s', tb), state, msg, vector (), 1, descs, rows); if (state <> '00000') signal (state, msg); if (length (rows) = 0) return 1; else return 0; } </pre> </div> </div> </div> <div class="refsect1"> <div class="refsect1title">See Also</div> <p> <a href="fn_rexecute.html">rexecute()</a> </p> <p> <a href="fn_exec_next.html">exec_next()</a> </p> <p> <a href="fn_exec_close.html">exec_close()</a> </p> </div> </div> <div id="footer"> <div>Copyright© 1999 - 2009 OpenLink Software All rights reserved.</div> <div id="validation"> <a href="http://validator.w3.org/check/referer"> <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /> </a> <a href="http://jigsaw.w3.org/css-validator/"> <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" /> </a> </div> </div> </body> </html>