176 lines
8.9 KiB
HTML
176 lines
8.9 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="dc.language" scheme="rfc1766" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<meta name="dc.date" scheme="iso8601" content="2005-09-19" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow"/>
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="keywords" content="EXECUTE IMMEDIATE statement, EXECUTE IMMEDIATE,
|
|
SQL statements, execution, dynamic SQL, IMMEDIATE, variable, indicator,
|
|
in EXECUTE IMMEDIATE statement, string-expression, statement string" />
|
|
<title>EXECUTE IMMEDIATE</title>
|
|
<link rel="stylesheet" type="text/css" href="ibmidwb.css" />
|
|
<link rel="stylesheet" type="text/css" href="ic.css" />
|
|
</head>
|
|
<body>
|
|
<a id="Top_Of_Page" name="Top_Of_Page"></a><!-- Java sync-link -->
|
|
<script language = "Javascript" src = "../rzahg/synch.js" type="text/javascript"></script>
|
|
|
|
|
|
<a name="exim"></a>
|
|
<h2 id="exim"><a href="rbafzmst02.htm#ToC_1064">EXECUTE IMMEDIATE</a></h2><a id="idx2561" name="idx2561"></a><a id="idx2562" name="idx2562"></a><a id="idx2563" name="idx2563"></a><a id="idx2564" name="idx2564"></a>
|
|
<a name="exeqi"></a>
|
|
<p id="exeqi">The EXECUTE IMMEDIATE statement: </p>
|
|
<ul>
|
|
<li>Prepares an executable form of an SQL statement from a character string
|
|
form of the statement</li>
|
|
<li>Executes the SQL statement</li></ul>
|
|
<p>EXECUTE IMMEDIATE combines the basic functions of the PREPARE and EXECUTE
|
|
statements. It can be used to prepare and execute SQL statements that contain
|
|
neither variables nor parameter markers.</p>
|
|
<a name="wq1491"></a>
|
|
<h3 id="wq1491"><a href="rbafzmst02.htm#ToC_1065">Invocation</a></h3>
|
|
<p>This statement can only be embedded in an application program,
|
|
SQL function, SQL procedure, or trigger. It is an executable statement that
|
|
cannot be dynamically prepared. It must not be specified in Java™.</p>
|
|
<a name="wq1492"></a>
|
|
<h3 id="wq1492"><a href="rbafzmst02.htm#ToC_1066">Authorization</a></h3>
|
|
<p>The authorization rules are those defined for the SQL statement specified
|
|
by EXECUTE IMMEDIATE. For example, see <a href="rbafzmstbackup.htm#backup">INSERT</a> for the authorization
|
|
rules that apply when an INSERT statement is executed using EXECUTE IMMEDIATE.</p>
|
|
<p>The authorization ID of the statement is the run-time authorization ID
|
|
unless DYNUSRPRF(*OWNER) was specified on the CRTSQLxxx command when the program
|
|
was created. For more information, see <a href="rbafzmstch2auth.htm#ch2auth">Authorization IDs and authorization names</a>.</p>
|
|
<a name="wq1493"></a>
|
|
<h3 id="wq1493"><a href="rbafzmst02.htm#ToC_1067">Syntax</a></h3>
|
|
<a href="rbafzmstexim.htm#synsexeimm"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn339.htm"
|
|
border="0" /></span><a href="#skipsyn-338"><img src="c.gif" alt="Skip visual syntax diagram"
|
|
border="0" /></a>>>-EXECUTE IMMEDIATE--+-<span><span class="italic">variable</span></span>----------+--------------------><
|
|
'-<span class="italic">string-expression</span>-'
|
|
|
|
</pre>
|
|
<a name="skipsyn-338" id="skipsyn-338"></a>
|
|
<a name="synsexeimm"></a>
|
|
<h3 id="synsexeimm"><a href="rbafzmst02.htm#ToC_1068">Description</a></h3><a id="idx2565" name="idx2565"></a>
|
|
<dl class="parml">
|
|
<dt class="bold"><var class="pv">variable</var> </dt><a id="idx2566" name="idx2566"></a>
|
|
<dd>Identifies a variable that must be declared in accordance
|
|
with the rules for declaring character-string, UTF-16 graphic, or UCS-2 graphic
|
|
variables. An indicator variable must not be specified.
|
|
</dd>
|
|
<dt class="bold"><var class="pv">string-expression</var></dt><a id="idx2567" name="idx2567"></a>
|
|
<dd>A <var class="pv">string-expression</var> is any PL/I <var class="pv">string-expression</var> that
|
|
yields a character string. SQL expressions that yield a character string are
|
|
not allowed. A <var class="pv">string-expression</var> is only allowed in PL/I.
|
|
</dd>
|
|
</dl><a id="idx2568" name="idx2568"></a><a id="idx2569" name="idx2569"></a>
|
|
<p>The value of the identified variable or string expression is called a <var class="pv">statement string</var>.</p>
|
|
<p>The statement string must be one of the following SQL statements:<sup class="fn"><a id="wq1494" name="wq1494" href="rbafzmstexim.htm#wq1495">70</a></sup></p>
|
|
<a name="wq1496"></a>
|
|
<table id="wq1496" width="100%" summary="" border="0" frame="void" rules="none">
|
|
<tbody valign="top">
|
|
<tr>
|
|
<td width="33%" align="left" valign="top">ALTER</td>
|
|
<td width="33%" align="left" valign="top">INSERT</td>
|
|
<td width="33%" align="left" valign="top">SET CURRENT DEBUG MODE</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">CALL</td>
|
|
<td align="left" valign="top">LABEL</td>
|
|
<td align="left" valign="top">SET CURRENT DEGREE</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">COMMENT</td>
|
|
<td align="left" valign="top">LOCK TABLE</td>
|
|
<td align="left" valign="top">SET ENCRYPTION PASSWORD</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">COMMIT</td>
|
|
<td align="left" valign="top">REFRESH TABLE</td>
|
|
<td align="left" valign="top">SET PATH</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">CREATE</td>
|
|
<td align="left" valign="top">RELEASE SAVEPOINT</td>
|
|
<td align="left" valign="top">SET SCHEMA</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">DECLARE GLOBAL TEMPORARY TABLE</td>
|
|
<td align="left" valign="top">RENAME</td>
|
|
<td align="left" valign="top">SET SESSION AUTHORIZATION</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">DELETE</td>
|
|
<td align="left" valign="top">REVOKE</td>
|
|
<td align="left" valign="top">SET TRANSACTION</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left" valign="top">DROP</td>
|
|
<td align="left" valign="top">ROLLBACK</td>
|
|
<td align="left" valign="top">UPDATE</td>
|
|
</tr>
|
|
<tr>
|
|
<td>GRANT</td>
|
|
<td>SAVEPOINT</td>
|
|
<td></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>The statement string must not: </p>
|
|
<ul>
|
|
<li>Begin with EXEC SQL and end with END-EXEC or a semicolon (;).</li>
|
|
<li>Include references to variables.</li>
|
|
<li>Include parameter markers.</li></ul>
|
|
<p>When an EXECUTE IMMEDIATE statement is executed, the specified statement
|
|
string is parsed and checked for errors. If the SQL statement is not valid,
|
|
it is not executed and the error condition that prevents its execution is
|
|
reported in the stand-alone SQLSTATE and SQLCODE. If the SQL statement is
|
|
valid, but an error occurs during its execution, that error condition is reported
|
|
in the stand-alone SQLSTATE and SQLCODE. Additional information on the error
|
|
can be retrieved from the SQL Diagnostics Area (or the SQLCA).</p>
|
|
<a name="wq1497"></a>
|
|
<h3 id="wq1497"><a href="rbafzmst02.htm#ToC_1069">Note</a></h3>
|
|
<p>If the same SQL statement is to be executed more than once, it is more
|
|
efficient to use the PREPARE and EXECUTE statements rather than the EXECUTE
|
|
IMMEDIATE statement.</p>
|
|
<a name="wq1498"></a>
|
|
<h3 id="wq1498"><a href="rbafzmst02.htm#ToC_1070">Example</a></h3>
|
|
<p>Use C to execute the SQL statement in the variable Qstring. </p>
|
|
<pre class="xmp">void main ()
|
|
{
|
|
|
|
EXEC SQL <span class="bold">BEGIN DECLARE SECTION</span> END-EXEC.
|
|
|
|
char Qstring[100] = "<span class="bold">INSERT INTO</span> WORK_TABLE <span class="bold">SELECT * FROM</span> EMPPROJACT
|
|
<span class="bold">WHERE</span> ACTNO >= 100";
|
|
|
|
EXEC SQL <span class="bold">END DECLARE SECTION</span> END-EXEC.
|
|
EXEC SQL <span class="bold">INCLUDE SQLCA</span>;
|
|
.
|
|
.
|
|
.
|
|
EXEC SQL <span class="bold">EXECUTE IMMEDIATE</span> :Qstring;
|
|
|
|
return;
|
|
}</pre><a id="idx2570" name="idx2570"></a><a id="idx2571" name="idx2571"></a><a id="idx2572" name="idx2572"></a>
|
|
<hr /><div class="fnnum"><a id="wq1495" name="wq1495" href="rbafzmstexim.htm#wq1494">70</a>.</div>
|
|
<div class="fntext">A <var class="pv">select-statement</var> is not allowed. To dynamically process a <var class="pv">select-statement</var>, use the PREPARE, DECLARE CURSOR, and OPEN statements.</div>
|
|
<br />
|
|
<hr /><br />
|
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmsthexeqt.htm">Previous Page</a> | <a href="rbafzmstfets.htm">Next Page</a> | <a href="rbafzmst02.htm#wq1">Contents</a> |
|
|
<a href="rbafzmstindex.htm#index">Index</a> ]
|
|
|
|
<a id="Bot_Of_Page" name="Bot_Of_Page"></a>
|
|
</body>
|
|
</html>
|