ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzajp_5.4.0.1/rzajphostvar.htm

146 lines
9.0 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<?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 host variables in SQL statements" />
<meta name="abstract" content="When your program retrieves data, the values are put into data items defined by your program and specified with the INTO clause of a SELECT INTO or FETCH statement. The data items are called host variables." />
<meta name="description" content="When your program retrieves data, the values are put into data items defined by your program and specified with the INTO clause of a SELECT INTO or FETCH statement. The data items are called host variables." />
<meta name="DC.subject" content="host variable, general use in SQL statement, definition, SQL, statements, using host variable, host variable in SQL, using, examples, host variable in SQL statement, definitions, host structure, host structure, concept, host language, using SQL with, host variable" />
<meta name="keywords" content="host variable, general use in SQL statement, definition, SQL, statements, using host variable, host variable in SQL, using, examples, host variable in SQL statement, definitions, host structure, host structure, concept, host language, using SQL with, host variable" />
<meta name="DC.Relation" scheme="URI" content="rzajpsqlcom.htm" />
<meta name="DC.Relation" scheme="URI" content="rzajpasnrule.htm" />
<meta name="DC.Relation" scheme="URI" content="rzajpindvar.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmst02.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="rzajphostvar" />
<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 host variables in SQL statements</title>
</head>
<body id="rzajphostvar"><a name="rzajphostvar"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use host variables in SQL statements</h1>
<div><p>When your program retrieves data, the values are put into data
items defined by your program and specified with the INTO clause of a SELECT
INTO or FETCH statement. The data items are called <em>host variables</em>.</p>
<div class="section"><p>A host variable is a field in your program that is specified in
an SQL statement, usually as the source or target for the value of a column.
The host variable and column must be data type compatible. Host variables
may not be used to identify SQL objects, such as tables or views, except in
the DESCRIBE TABLE statement.</p>
</div>
<div class="section"><div class="p">A <strong>host structure</strong> is a group of host variables used as the
source or target for a set of selected values (for example, the set of values
for the columns of a row). A <strong>host structure array</strong> is an array of host
structures used in the multiple-row FETCH and blocked INSERT statements. <div class="note"><span class="notetitle">Note:</span> By
using a host variable instead of a literal value in an SQL statement, you
give the application program the flexibility it needs to process different
rows in a table or view.</div>
</div>
</div>
<div class="section"><p>For example, instead of coding an actual department number in
a WHERE clause, you can use a host variable set to the department number you
are currently interested in.</p>
</div>
<div class="section"><p>Host variables are commonly used in SQL statements in these ways: </p>
</div>
<div class="section"> <ul><li><strong>In a WHERE clause:</strong> You can use a host variable to specify a value
in the predicate of a search condition, or to replace a literal value in an
expression. For example, if you have defined a field called EMPID that contains
an employee number, you can retrieve the name of the employee whose number
is 000110 with: <pre> MOVE '000110' TO EMPID.
EXEC SQL
<strong>SELECT</strong> LASTNAME
<strong>INTO</strong> :PGM-LASTNAME
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> EMPNO = :EMPID
END-EXEC.</pre>
</li>
<li><strong>As a receiving area for column values (named in an INTO clause):</strong> You
can use a host variable to specify a program data area that is to contain
the column values of a retrieved row. The INTO clause names one or more host
variables that you want to contain column values returned by SQL. For example,
suppose you are retrieving the <em>EMPNO</em>, <em>LASTNAME</em>, and <em>WORKDEPT</em> column
values from rows in the CORPDATA.EMPLOYEE table. You could define a host variable
in your program to hold each column, then name the host variables with an
INTO clause. For example: <pre> EXEC SQL
<strong>SELECT</strong> EMPNO, LASTNAME, WORKDEPT
<strong>INTO</strong> :CBLEMPNO, :CBLNAME, :CBLDEPT
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> EMPNO = :EMPID
END-EXEC.</pre>
<p>In this example, the host variable CBLEMPNO receives
the value from EMPNO, CBLNAME receives the value from LASTNAME, and CBLDEPT
receives the value from WORKDEPT.</p>
</li>
<li><strong>As a value in a SELECT clause:</strong> When specifying a list of items
in the SELECT clause, you are not restricted to the column names of tables
and views. Your program can return a set of column values intermixed with
host variable values and literal constants. For example: <pre> MOVE '000220' TO PERSON.
EXEC SQL
<strong>SELECT</strong> "A", LASTNAME, SALARY, :RAISE,
SALARY + :RAISE
<strong>INTO</strong> :PROCESS, :PERSON-NAME, :EMP-SAL,
:EMP-RAISE, :EMP-TTL
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> EMPNO = :PERSON
END-EXEC.</pre>
<p>The results are: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="90%" frame="hsides" border="1" rules="all"><thead align="left"><tr><th align="left" valign="bottom" width="14.25925925925926%" id="d0e154">PROCESS</th>
<th align="left" valign="bottom" width="42.77777777777778%" id="d0e156">PERSON-NAME</th>
<th align="left" valign="bottom" width="14.25925925925926%" id="d0e158">EMP-SAL</th>
<th align="left" valign="bottom" width="14.25925925925926%" id="d0e160">EMP-RAISE</th>
<th align="left" valign="bottom" width="14.444444444444443%" id="d0e162">EMP-TTL</th>
</tr>
</thead>
<tbody><tr><td align="left" valign="top" width="14.25925925925926%" headers="d0e154 ">A</td>
<td align="left" valign="top" width="42.77777777777778%" headers="d0e156 ">LUTZ</td>
<td align="left" valign="top" width="14.25925925925926%" headers="d0e158 ">29840</td>
<td align="left" valign="top" width="14.25925925925926%" headers="d0e160 ">4476</td>
<td align="left" valign="top" width="14.444444444444443%" headers="d0e162 ">34316</td>
</tr>
</tbody>
</table>
</div>
</li>
<li><strong>As a value in other clauses of an SQL statement:</strong> <ul><li>The SET clause in an UPDATE statement</li>
<li>The VALUES clause in an INSERT statement</li>
<li>The CALL statement</li>
</ul>
</li>
</ul>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rzajpasnrule.htm">Assignment rules for host variables in SQL statements</a></strong><br />
SQL values are assigned to host variables during the running of FETCH, SELECT INTO, SET, and VALUES INTO statements. SQL values are assigned from host variables during the running of INSERT, UPDATE, and CALL statements.</li>
<li class="ulchildlink"><strong><a href="rzajpindvar.htm">Indicator variables in applications that use SQL</a></strong><br />
An <em>indicator variable</em> is a halfword integer variable used
to indicate whether its associated host variable has been assigned a null
value.</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzajpsqlcom.htm" title="This topic describes some concepts and rules that are common to using SQL statements in a host language.">Common concepts and rules for using embedded SQL</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmst02.htm">SQL reference</a></div>
</div>
</div>
</body>
</html>