174 lines
12 KiB
HTML
174 lines
12 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="SELECT INTO statement, SELECT INTO,
|
||
|
SQL statements, single row select, in SELECT INTO statement, isolation-clause,
|
||
|
INTO clause, variable, assigned to variable, null value, SQL" />
|
||
|
<title>SELECT INTO</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="hselin"></a>
|
||
|
<h2 id="hselin"><a href="rbafzmst02.htm#ToC_1269">SELECT INTO</a></h2><a id="idx3107" name="idx3107"></a><a id="idx3108" name="idx3108"></a><a id="idx3109" name="idx3109"></a>
|
||
|
<a name="selct"></a>
|
||
|
<p id="selct">The SELECT INTO statement produces a result table consisting
|
||
|
of at most one row, and assigns the values in that row to variables. If the
|
||
|
table is empty, the statement assigns +100 to SQLCODE and '02000'
|
||
|
to SQLSTATE and does not assign values to the variables. If more than one
|
||
|
row satisfies the search condition, statement processing is terminated, and
|
||
|
an error occurs.</p>
|
||
|
<p></p>
|
||
|
<a name="wq1726"></a>
|
||
|
<h3 id="wq1726"><a href="rbafzmst02.htm#ToC_1270">Invocation</a></h3>
|
||
|
<p>This statement can only be embedded in an application program. It is an
|
||
|
executable statement that cannot be dynamically prepared. It must not be specified
|
||
|
in REXX.</p>
|
||
|
<a name="wq1727"></a>
|
||
|
<h3 id="wq1727"><a href="rbafzmst02.htm#ToC_1271">Authorization</a></h3>
|
||
|
<p>The privileges held by the authorization ID of the statement must include
|
||
|
at least one of the following: </p>
|
||
|
<ul>
|
||
|
<li>For each table or view identified in the statement,
|
||
|
<ul>
|
||
|
<li>The SELECT privilege on the table or view, and</li>
|
||
|
<li>The system authority *EXECUTE on the library containing the table or view</li></ul></li>
|
||
|
<li>Administrative authority</li></ul>
|
||
|
<p>For information on the system authorities corresponding to SQL privileges,
|
||
|
see <a href="rbafzmstgnt.htm#eqtablet">Corresponding System Authorities When Checking Privileges to a Table or View</a>.</p>
|
||
|
<a name="wq1728"></a>
|
||
|
<h3 id="wq1728"><a href="rbafzmst02.htm#ToC_1272">Syntax</a></h3>
|
||
|
<a href="rbafzmsthselin.htm#synselectinto"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn377.htm"
|
||
|
border="0" /></span><a href="#skipsyn-376"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-,--------.
|
||
|
V |
|
||
|
>>-<span class="italic">select-clause</span>--INTO----<span><span class="italic">variable</span></span>-+---------------------------->
|
||
|
|
||
|
>--<span class="italic">from-clause</span>--+--------------+--+-----------------+--+---------------+-->
|
||
|
'-<span class="italic">where-clause</span>-' '-<span class="italic">group-by-clause</span>-' '-<span class="italic">having-clause</span>-'
|
||
|
|
||
|
(1)
|
||
|
>--+-----------------+--+--------------------+-------+------------------+-><
|
||
|
'-<span class="italic">order-by-clause</span>-' '-<span class="italic">fetch-first-clause</span>-' '-<span class="italic">isolation-clause</span>-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-376" id="skipsyn-376"></a>
|
||
|
<a name="wq1729"></a>
|
||
|
<div class="notelisttitle" id="wq1729">Notes:</div>
|
||
|
<ol type="1">
|
||
|
<li>Only one row may be specified in the fetch-first-clause.</li>
|
||
|
</ol>
|
||
|
<a name="synselectinto"></a>
|
||
|
<h3 id="synselectinto"><a href="rbafzmst02.htm#ToC_1273">Description</a></h3>
|
||
|
<p>The result table is derived by evaluating the <span class="italic">isolation-clause</span>, <span class="italic">from-clause</span>, <span class="italic">where-clause</span>, <span class="italic">group-by-clause</span>, <span class="italic">having-clause</span>, <span class="italic">order-by-clause</span>, <span class="italic">fetch-first-clause</span>, and <span class="italic">select-clause</span>, in this
|
||
|
order.</p>
|
||
|
<p>See <a href="rbafzmstsubsel.htm#subsel">Queries</a> for a description of the <var class="pv">select-clause</var>, <var class="pv">from-clause</var>, <var class="pv">where-clause</var>, <var class="pv">group-by-clause</var>, <var class="pv">having-clause</var>, <var class="pv">order-by-clause</var>, <var class="pv">fetch-first-clause</var>, and <var class="pv">isolation-clause</var>.</p><a id="idx3110" name="idx3110"></a>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold">INTO <var class="pv">variable</var>,... </dt><a id="idx3111" name="idx3111"></a><a id="idx3112" name="idx3112"></a>
|
||
|
<dd>Identifies one or more host structures or variables that must be declared
|
||
|
in the program in accordance with the rules for declaring host structures
|
||
|
and variables. In the operational form of the INTO clause, a reference to
|
||
|
a host structure is replaced by a reference to each of its variables. The
|
||
|
first value in the result row is assigned to the first variable in the list,
|
||
|
the second value to the second variable, and so on. The data type of each
|
||
|
variable must be compatible with its corresponding column.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<a name="wq1731"></a>
|
||
|
<h3 id="wq1731"><a href="rbafzmst02.htm#ToC_1274">Note</a></h3><a id="idx3113" name="idx3113"></a><a id="idx3114" name="idx3114"></a>
|
||
|
<p><span class="bold">Variable assignment: </span> Each assignment
|
||
|
to a variable is performed according to the retrieval assignment rules described
|
||
|
in <a href="rbafzmstch2bas.htm#ch2bas">Assignments and comparisons</a>. If the number of variables is less than the number
|
||
|
of values in the row, an SQL warning (SQLSTATE 01503) is returned (and the
|
||
|
SQLWARN3 field of the SQLCA is set to 'W'). Note that there is no
|
||
|
warning if there are more variables than the number of result columns. If
|
||
|
a value is null, an indicator variable must be provided for that value.</p>
|
||
|
<p>If the specified variable is character and is not large enough to contain
|
||
|
the result, a warning (SQLSTATE 01004) is returned (and 'W' is assigned
|
||
|
to SQLWARN1 in the SQLCA). The actual length of the result may be returned
|
||
|
in the indicator variable associated with the variable, if an indicator variable
|
||
|
is provided. For further information, see <a href="rbafzmstch2refvar.htm#ch2refvar">References to variables</a>.</p>
|
||
|
<p>If an assignment error occurs, the value of that variable and any following
|
||
|
variables is unpredictable. Any values that have already been assigned to
|
||
|
variables remain assigned.</p>
|
||
|
<p><span class="bold">Empty result table: </span> If the result table is empty,
|
||
|
the statement assigns '02000' to the SQLSTATE variable and does not
|
||
|
assign values to the variables.</p>
|
||
|
<p><span class="bold">Result tables with more than one row: </span> If more than
|
||
|
one row satisfies the search condition, statement processing is terminated
|
||
|
and an error is returned (SQLSTATE 21000). If an error occurs because the
|
||
|
result table has more than one row, values may or may not be assigned to the
|
||
|
variables. If values are assigned to the variables, the row that is the source
|
||
|
of the values is undefined and not predictable.</p>
|
||
|
<p><span class="bold">Result column evaluation considerations: </span> When a
|
||
|
TIME value is selected, if the ISO, EUR, or JIS format is used, the length
|
||
|
of the variable must not be less than 5. If the length is 5, 6, or 7, the
|
||
|
seconds part of the time is omitted from the result, a warning (SQLSTATE 01004)
|
||
|
is returned (and 'W' is assigned to SQLWARN1 in the SQLCA). In this
|
||
|
case, the seconds part of the time is assigned to the indicator variable if
|
||
|
one is provided, and, if the length is 6 or 7, blank padding occurs so that
|
||
|
the value is a valid string representation of a time.</p>
|
||
|
<p>If an error occurs while evaluating a result column in the SELECT list
|
||
|
of a SELECT INTO statement, as the result of an arithmetic expression (such
|
||
|
as division by zero, or overflow) or a numeric or character conversion error,
|
||
|
the result is the null value. As in any other case of a null value, an indicator
|
||
|
variable must be provided. The value of the variable is undefined. In this
|
||
|
case, however, the indicator variable is set to the value of -2. Processing
|
||
|
of the statement continues and a warning is returned. If an indicator variable
|
||
|
is not provided, an error is returned and no more values are assigned to variables.
|
||
|
It is possible that some values have already been assigned to variables and
|
||
|
will remain assigned when the error is returned.</p>
|
||
|
<a name="wq1732"></a>
|
||
|
<h3 id="wq1732"><a href="rbafzmst02.htm#ToC_1275">Examples</a></h3>
|
||
|
<p><span class="italic">Example 1:</span> Using a COBOL program statement, put
|
||
|
the maximum salary (SALARY) from the EMPLOYEE table into the host variable
|
||
|
MAX-SALARY (DECIMAL(9,2)). </p>
|
||
|
<pre class="xmp"> EXEC SQL <span class="bold">SELECT MAX(</span>SALARY<span class="bold">)</span>
|
||
|
<span class="bold">INTO</span> :MAX-SALARY
|
||
|
<span class="bold">FROM</span> EMPLOYEE <span class="bold">WITH CS</span>
|
||
|
END-EXEC.</pre>
|
||
|
<p><span class="italic">Example 2:</span> Using a Java™ program statement, select the row
|
||
|
from the EMPLOYEE table on the connection context 'ctx' with a employee number
|
||
|
(EMPNO) value the same as that stored in the host variable HOST_EMP (java.lang.String).
|
||
|
Then put the last name (LASTNAME) and education level (EDLEVEL) from that
|
||
|
row into the host variables HOST_NAME (String) and HOST_EDUCATE (Integer). </p>
|
||
|
<pre class="xmp"> #sql [ctx] { <span class="bold">SELECT</span> LASTNAME, EDLEVEL
|
||
|
<span class="bold">INTO</span> :HOST_NAME, :HOST_EDUCATE
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> EMPNO = :HOST_EMP };</pre>
|
||
|
<p><span class="italic">Example 3:</span> Put the row for employee
|
||
|
528671, from the EMPLOYEE table, into the host structure EMPREC. Assume that
|
||
|
the row will be updated later and should be locked when the query executes. </p>
|
||
|
<pre class="xmp"> EXEC SQL <span class="bold">SELECT *</span>
|
||
|
<span class="bold">INTO</span> :EMPREC
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> EMPNO <span class="bold">=</span> '528671'
|
||
|
<span class="bold">WITH RS USE AND KEEP EXCLUSIVE LOCKS</span>
|
||
|
END-EXEC.</pre>
|
||
|
<p><a id="idx3115" name="idx3115"></a><a id="idx3116" name="idx3116"></a></p>
|
||
|
<hr /><br />
|
||
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmsthselectstmt.htm">Previous Page</a> | <a href="rbafzmstetconj.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>
|