ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafyplepexc.htm

85 lines
5.6 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 lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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="DC.Type" content="reference" />
<meta name="DC.Title" content="Use the PREPARE and EXECUTE statements" />
<meta name="abstract" content="If non-SELECT statements contain no parameter markers, they can be run dynamically using the EXECUTE IMMEDIATE statement. However, if the non-SELECT statements have parameter markers, they must be run using PREPARE and EXECUTE." />
<meta name="description" content="If non-SELECT statements contain no parameter markers, they can be run dynamically using the EXECUTE IMMEDIATE statement. However, if the non-SELECT statements have parameter markers, they must be run using PREPARE and EXECUTE." />
<meta name="DC.subject" content="dynamic SQL, using PREPARE statement, using EXECUTE statement, statements, PREPARE, non-SELECT statement, EXECUTE, PREPARE statement, in dynamic SQL, EXECUTE statement, COMMIT, prepared statements, ROLLBACK statement, in dynamic SQL" />
<meta name="keywords" content="dynamic SQL, using PREPARE statement, using EXECUTE statement, statements, PREPARE, non-SELECT statement, EXECUTE, PREPARE statement, in dynamic SQL, EXECUTE statement, COMMIT, prepared statements, ROLLBACK statement, in dynamic SQL" />
<meta name="DC.Relation" scheme="URI" content="rbafynonsil.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafydynmic.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbafyplepexc" />
<meta name="DC.Language" 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. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Use the PREPARE and EXECUTE statements</title>
</head>
<body id="rbafyplepexc"><a name="rbafyplepexc"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use the PREPARE and EXECUTE statements</h1>
<div><p>If non-SELECT statements contain no parameter markers, they can
be run dynamically using the EXECUTE IMMEDIATE statement. However, if the
non-SELECT statements have parameter markers, they must be run using PREPARE
and EXECUTE.</p>
<div class="section"><p>The PREPARE statement prepares the non-SELECT statement (for example,
the DELETE statement) and gives it a statement name you choose. If DLYPRP
(*YES) is specified on the CRTSQLxxx command, the preparation is delayed until
the first time the statement is used in an EXECUTE or DESCRIBE statement,
unless the USING clause is specified on the PREPARE statement. After
the statement has been prepared, it can be run many times within the same
program, using different values for the parameter markers. The following example
is of a prepared statement being run multiple times:</p>
<pre> DSTRING = '<strong>DELETE FROM</strong> CORPDATA.EMPLOYEE <strong>WHERE</strong> EMPNO = ?';
/*The ? is a parameter marker which denotes
that this value is a host variable that is
to be substituted each time the statement is run.*/
EXEC SQL <strong>PREPARE</strong> S1 <strong>FROM</strong> :DSTRING;
/*DSTRING is the delete statement that the PREPARE statement is
naming S1.*/
DO UNTIL (EMP =0);
/*The application program reads a value for EMP from the
display station.*/
EXEC SQL
<strong>EXECUTE</strong> S1 <strong>USING</strong> :EMP;
END;</pre>
</div>
<div class="section"><p>In routines similar to the example above, you must know the number
of parameter markers and their data types, because the host variables that
provide the input data are declared when the program is being written.</p>
<div class="note"><span class="notetitle">Note:</span> All
prepared statements that are associated with an application server are destroyed
whenever the connection to the application server ends. Connections are ended
by a CONNECT (Type 1) statement, a DISCONNECT statement, or a RELEASE followed
by a successful COMMIT. </div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafynonsil.htm" title="To build a dynamic SQL non-SELECT statement, you need to verify that the SQL statement you want to build is one that can be run dynamically and then build the SQL statement.">Process non-SELECT statements</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="rbafydynmic.htm" title="Dynamic SQL allows an application to define and run SQL statements at program run time. An application that provides for dynamic SQL accepts as input (or builds) an SQL statement in the form of a character string. The application does not need to know what type of SQL statement it will run.">Dynamic SQL applications</a></div>
</div>
</div>
</body>
</html>