81 lines
5.4 KiB
HTML
81 lines
5.4 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="Insert rows into a table using a select-statement" />
|
|
<meta name="abstract" content="You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement." />
|
|
<meta name="description" content="You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement." />
|
|
<meta name="DC.subject" content="INSERT statement, inserting multiple rows, using SELECT, row, inserting multiple using SELECT, into a table, table, inserting multiple rows into, using SELECT, examples" />
|
|
<meta name="keywords" content="INSERT statement, inserting multiple rows, using SELECT, row, inserting multiple using SELECT, into a table, table, inserting multiple rows into, using SELECT, examples" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyinsert.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="rbafymultrow" />
|
|
<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>Insert rows into a table using a select-statement</title>
|
|
</head>
|
|
<body id="rbafymultrow"><a name="rbafymultrow"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Insert rows into a table using a select-statement</h1>
|
|
<div><p>You can use a select-statement within an INSERT statement to insert
|
|
zero, one, or more rows into a table from the result table of the select-statement. </p>
|
|
<div class="section"><p>One use for this kind of INSERT statement is to move data into
|
|
a table you created for summary data. For example, suppose you want a table
|
|
that shows each employee's time commitments to projects. Create a table called
|
|
EMPTIME with the columns <em>EMPNUMBER</em>, <em>PROJNUMBER</em>, <em>STARTDATE</em>,
|
|
and <em>ENDDATE</em> and then use the following INSERT statement to fill the
|
|
table: </p>
|
|
<pre> <strong>INSERT INTO</strong> CORPDATA.EMPTIME
|
|
(EMPNUMBER, PROJNUMBER, STARTDATE, ENDDATE)
|
|
<strong>SELECT</strong> EMPNO, PROJNO, EMSTDATE, EMENDATE
|
|
<strong>FROM</strong> CORPDATA.EMPPROJACT</pre>
|
|
</div>
|
|
<div class="section"><p>The select-statement embedded in the INSERT statement
|
|
is no different from the select-statement you use to retrieve data. With the
|
|
exception of FOR READ ONLY, FOR UPDATE, or the OPTIMIZE clause, you can use
|
|
all the keywords, functions, and techniques used to retrieve data. SQL inserts
|
|
all the rows that meet the search conditions into the table you specify. Inserting
|
|
rows from one table into another table does not affect any existing rows in
|
|
either the source table or the target table.</p>
|
|
</div>
|
|
<div class="section"><p>You should consider the following when inserting multiple rows
|
|
into a table: </p>
|
|
</div>
|
|
<div class="section"> <div class="note"><span class="notetitle">Notes:</span> <ol><li>The number of columns implicitly or explicitly listed in the INSERT statement
|
|
must equal the number of columns listed in the select-statement.</li>
|
|
<li>The data in the columns you are selecting must be compatible with the
|
|
columns you are inserting into when using the INSERT with select-statement.</li>
|
|
<li>In the event the select-statement embedded in the INSERT returns no rows,
|
|
an SQLCODE of 100 is returned to alert you that no rows were inserted. If
|
|
you successfully insert rows, the SQLERRD(3) field of the SQLCA has an integer
|
|
representing the number of rows SQL actually inserted. This value is also
|
|
available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement.</li>
|
|
<li>If SQL finds an error while running the INSERT statement, SQL stops the
|
|
operation. If you specify COMMIT (*CHG), COMMIT(*CS), COMMIT (*ALL), or COMMIT(*RR),
|
|
nothing is inserted into the table and a negative SQLCODE is returned. If
|
|
you specify COMMIT(*NONE), any rows inserted before the error remain in the
|
|
table.</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyinsert.htm" title="This topic shows the basic SQL statements and clauses that insert data into tables and views. Examples using these SQL statements are supplied to help you develop SQL applications.">Insert rows using the INSERT statement</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |