430 lines
26 KiB
HTML
430 lines
26 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="INSERT, SQL statements, inserting, row,
|
|
INSERT statement, INTO keyword, in INSERT statement, table-name, view-name,
|
|
column-name, VALUES clause, expression, DEFAULT clause, NULL clause,
|
|
used in INSERT statement, select-statement, isolation-clause, UPDATE statement,
|
|
WITH clause, ROWS clause, variable, host-structure-array, effect on insert,
|
|
check constraint, insert rules" />
|
|
<title>INSERT</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="backup"></a>
|
|
<h2 id="backup"><a href="rbafzmst02.htm#ToC_1154">INSERT</a></h2><a id="idx2863" name="idx2863"></a><a id="idx2864" name="idx2864"></a><a id="idx2865" name="idx2865"></a>
|
|
<a name="insrt"></a>
|
|
<p id="insrt">The INSERT statement inserts rows into a table or
|
|
view. Inserting a row into a view also inserts the row into the table on which
|
|
the view is based if no INSTEAD OF INSERT trigger is defined on this view.
|
|
If such a trigger is defined, the trigger will be executed instead.</p>
|
|
<p>There are three forms of this statement: </p>
|
|
<ul>
|
|
<li>The <span class="italic">INSERT using VALUES</span> form is used to insert
|
|
one or more rows into the table or view using the values provided or referenced.</li>
|
|
<li>The <span class="italic">INSERT using SELECT</span> form is used to insert
|
|
one or more rows into the table or view using values from other tables or
|
|
views.</li>
|
|
<li>The <span class="italic">INSERT using n ROWS</span> form is used to insert
|
|
multiple rows into the table or view using the values provided in a host-structure-array.</li></ul>
|
|
<a name="wq1624"></a>
|
|
<h3 id="wq1624"><a href="rbafzmst02.htm#ToC_1155">Invocation</a></h3>
|
|
<p>This statement can be embedded in an application program or issued interactively.
|
|
It is an executable statement that can be dynamically prepared with the exception
|
|
of the n ROWS form, which must be a static statement embedded in an application
|
|
program. The n ROWS form is not allowed in a REXX procedure.</p>
|
|
<a name="wq1625"></a>
|
|
<h3 id="wq1625"><a href="rbafzmst02.htm#ToC_1156">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 the table or view identified in the statement:
|
|
<ul>
|
|
<li>The INSERT 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>If a <var class="pv">select-statement</var> is specified, the privileges
|
|
held by the authorization ID of the statement must also include one of the
|
|
following: </p>
|
|
<ul>
|
|
<li>For each table or view identified in the <var class="pv">select-statement</var>:
|
|
<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="wq1626"></a>
|
|
<h3 id="wq1626"><a href="rbafzmst02.htm#ToC_1157">Syntax</a></h3>
|
|
<a href="rbafzmstbackup.htm#synsinsert"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
|
<a name="wq1627"></a>
|
|
<div class="fignone" id="wq1627">
|
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn357.htm"
|
|
border="0" /></span><a href="#skipsyn-356"><img src="c.gif" alt="Skip visual syntax diagram"
|
|
border="0" /></a>>>-INSERT INTO--+-<span class="italic">table-name</span>-+--+-----------------------+------->
|
|
'-<span class="italic">view-name</span>--' | .-,-----------. |
|
|
| V | |
|
|
'-(----<span class="italic">column-name</span>-+--)-'
|
|
|
|
>--+-------------------------+---------------------------------->
|
|
+-OVERRIDING SYSTEM VALUE-+
|
|
'-OVERRIDING USER VALUE---'
|
|
|
|
.-,----------------------------.
|
|
V |
|
|
>--+-VALUES----+-+-<span class="italic">expression</span>-+-----------+-+--+------------------+-+-><
|
|
| | +-DEFAULT----+ | '-<span class="italic">isolation-clause</span>-' |
|
|
| | '-NULL-------' | |
|
|
| | .-,--------------. | |
|
|
| | V | | |
|
|
| '-(----+-<span class="italic">expression</span>-+-+--)-' |
|
|
| +-DEFAULT----+ |
|
|
| '-NULL-------' |
|
|
+-<span class="italic">insert-multiple-rows</span>--+------------------+---------------------+
|
|
| '-<span class="italic">isolation-clause</span>-' |
|
|
'-<span class="italic">select-statement</span>-----------------------------------------------'
|
|
|
|
insert-multiple-rows:
|
|
|
|
|--+-<span class="italic">integer</span>--+--ROWS--VALUES--(--<span class="italic">host-structure-array</span>--)-------|
|
|
'-<span><span class="italic">variable</span></span>-'
|
|
|
|
isolation–clause:
|
|
|
|
|--WITH--+-NC-+-------------------------------------------------|
|
|
+-UR-+
|
|
+-CS-+
|
|
+-RS-+
|
|
'-RR-'
|
|
|
|
</pre>
|
|
<a name="skipsyn-356" id="skipsyn-356"></a></div>
|
|
<a name="synsinsert"></a>
|
|
<h3 id="synsinsert"><a href="rbafzmst02.htm#ToC_1158">Description</a></h3>
|
|
<dl class="parml">
|
|
<dt class="bold">INTO <var class="pv">table-name</var> <span class="base">or</span> <var class="pv">view-name</var> </dt><a id="idx2866" name="idx2866"></a><a id="idx2867" name="idx2867"></a><a id="idx2868" name="idx2868"></a>
|
|
<dd>Identifies the object of the insert operation. The name must identify
|
|
a table or view that exists at the current server, but it must not identify
|
|
a catalog table, a view of a catalog table, or a view that is not insertable.
|
|
For an explanation of insertable views, see <a href="rbafzmsthcview.htm#hcview">CREATE VIEW</a>.
|
|
</dd>
|
|
<dt class="bold">(<var class="pv">column-name</var>,...) </dt><a id="idx2869" name="idx2869"></a>
|
|
<dd>Specifies the columns for which insert values are provided. Each name
|
|
must be a name that identifies a column of the table or view. The same column
|
|
must not be identified more than once. A view column that is not updatable
|
|
must not be identified. If the object of the insert operation is a view with
|
|
such columns, a list of column names must be specified and the list must not
|
|
identify those columns. For an explanation of updatable columns in views,
|
|
see <a href="rbafzmsthcview.htm#hcview">CREATE VIEW</a>.
|
|
<p>Omission of the column list is an implicit specification
|
|
of a list in which every column of the table or view is identified in left-to-right
|
|
order. This list is established when the statement is prepared and, therefore,
|
|
does not include columns that were added to a table after the statement was
|
|
prepared.</p>
|
|
<p>If the INSERT statement is embedded in an application and
|
|
the referenced table or view exists at create program time, the statement
|
|
is prepared at create program time. Otherwise, the statement is prepared at
|
|
the first successful execute of the INSERT statement.</p>
|
|
</dd>
|
|
<dt class="bold">OVERRIDING SYSTEM VALUE or OVERRIDING USER VALUE</dt>
|
|
<dd>Specifies whether system generated values or user-specified values for
|
|
a ROWID or identity column are used. If OVERRIDING SYSTEM VALUE is specified,
|
|
the implicit or explicit list of columns for the INSERT statement must contain
|
|
a column defined as GENERATED ALWAYS. If OVERRIDING USER VALUE is specified,
|
|
the implicit or explicit list of columns for the INSERT statement must contain
|
|
a column defined as either GENERATED ALWAYS or GENERATED BY DEFAULT.
|
|
<dl class="parml">
|
|
<dt class="bold">OVERRIDING SYSTEM VALUE</dt>
|
|
<dd>Specifies that the value specified in the VALUES clause or produced
|
|
by a fullselect for a column that is defined as GENERATED ALWAYS is used.
|
|
A system-generated value is not inserted.
|
|
</dd>
|
|
<dt class="bold">OVERRIDING USER VALUE</dt>
|
|
<dd>Specifies that the value specified in the VALUES clause or produced
|
|
by a fullselect for a column that is defined as either GENERATED ALWAYS or
|
|
GENERATED BY DEFAULT is ignored. Instead, a system-generated value is inserted,
|
|
overriding the user-specified value.
|
|
</dd>
|
|
</dl>
|
|
<p>If neither OVERRIDING SYSTEM VALUE nor OVERRIDING USER VALUE is
|
|
specified:</p>
|
|
<ul>
|
|
<li>A value cannot be specified for a ROWID or identity column that is defined
|
|
as GENERATED ALWAYS.</li>
|
|
<li>A value can be specified for a ROWID or identity column that is defined
|
|
as GENERATED BY DEFAULT. If a value is specified that value is assigned to
|
|
the column. However, a value can be inserted into a ROWID column defined BY
|
|
DEFAULT only if the specified value is a valid row ID value that was previously
|
|
generated by DB2 UDB for z/OS or DB2 UDB for iSeries. When a value is inserted into an identity
|
|
column defined BY DEFAULT, the database manager does not verify that the specified value
|
|
is a unique value for the column unless the identity column is the sole key
|
|
in a unique constraint or unique index. Without a unique constraint or unique
|
|
index, the database manager can guarantee unique values only among the set of system-generated
|
|
values as long as NO CYCLE is in effect.
|
|
<p>If a value is not specified the database manager generates
|
|
a new value.</p></li></ul>
|
|
</dd>
|
|
<dt class="bold">VALUES </dt><a id="idx2870" name="idx2870"></a>
|
|
<dd>Specifies one or more new rows to be inserted.
|
|
<p>Each variable in the
|
|
clause must identify a host structure or variable that is declared in accordance
|
|
with the rules for declaring host structures and variables. In the operational
|
|
form of the statement, a reference to a host structure is replaced by a reference
|
|
to each of its variables. For further information on variables and structures,
|
|
see <a href="rbafzmstch2refvar.htm#ch2host">References to host variables</a> and <a href="rbafzmstch2refvar.htm#hscpcj">Host structures</a>.</p>
|
|
<p>The number of
|
|
values for each row in the VALUES clause must equal the number of names in
|
|
the column list. The first value is inserted in the first column in the list,
|
|
the second value in the second column, and so on.</p>
|
|
<dl class="parml">
|
|
<dt class="bold"><var class="pv">expression</var></dt><a id="idx2871" name="idx2871"></a>
|
|
<dd>An <var class="pv">expression</var> of the type described in <a href="rbafzmstch2expr.htm#ch2expr">Expressions</a>, that does not include an aggregate function or column name.
|
|
If <var class="pv">expression</var> is a <var class="pv">variable</var>, the variable can
|
|
identify a structure.
|
|
</dd>
|
|
<dt class="bold">DEFAULT</dt><a id="idx2872" name="idx2872"></a>
|
|
<dd>Specifies that the default value is assigned to a column. The value
|
|
that is inserted depends on how the column was defined, as follows:
|
|
<ul>
|
|
<li>If the WITH DEFAULT clause is used, the default inserted is as defined
|
|
for the column (see <span class="italic">default-clause</span> in <span class="italic">column-definition</span> in <a href="rbafzmsthctabl.htm#hctabl">CREATE TABLE</a>).</li>
|
|
<li>If the WITH DEFAULT clause or the NOT NULL clause is not used, the value
|
|
inserted is NULL.</li>
|
|
<li>If the NOT NULL clause is used and the WITH DEFAULT clause is not used
|
|
or DEFAULT NULL is used, the DEFAULT keyword cannot be specified for that
|
|
column.</li>
|
|
<li>If the column is a ROWID or identity column, the database manager will generate a new
|
|
value.</li></ul>
|
|
<p>DEFAULT must be specified for a ROWID or an identity column
|
|
that was defined as GENERATED ALWAYS unless OVERRIDING USER VALUE is specified
|
|
to indicate that any user-specified value will be ignored and a unique system-generated
|
|
value will be inserted.</p>
|
|
</dd>
|
|
<dt class="bold">NULL<a id="idx2873" name="idx2873"></a></dt>
|
|
<dd>Specifies the value for a column is the null value. NULL should only
|
|
be specified for nullable columns.
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt class="bold"><var class="pv">select-statement</var></dt><a id="idx2874" name="idx2874"></a>
|
|
<dd>Specifies a set of new rows in the form of the result table of a <var class="pv">select-statement</var>. The FOR READ ONLY, FOR UPDATE, and OPTIMIZE clauses
|
|
are not valid for a <var class="pv">select-statement</var> used with insert. If an ORDER
|
|
BY clause is specified on the <var class="pv">select-statement</var>, the rows are inserted
|
|
according to the values of the columns identified in the ORDER BY clause.
|
|
For an explanation of <var class="pv">select-statement</var>, see <a href="rbafzmstintsel.htm#intsel">select-statement</a>.
|
|
<p>There can be one, more than one, or zero rows inserted when
|
|
using the <var class="pv">select-statement</var>. If no rows are inserted, SQLCODE is set
|
|
to +100 and SQLSTATE is set to '02000'.</p>
|
|
<p>When the base object
|
|
of the INSERT and a base object of any subselect in the <var class="pv">select-statement</var> are the same table, the select statement is completely evaluated before
|
|
any rows are inserted.</p>
|
|
<p>The number of columns in the result
|
|
table must equal the number of names implicitly or explicitly specified in
|
|
the <var class="pv">column-name</var> list. The value of the first column of the result
|
|
is inserted in the first column in the list, the second value in the second
|
|
column, and so on. <a id="idx2875" name="idx2875"></a></p>
|
|
</dd>
|
|
<dt class="bold"><var class="pv">isolation-clause</var></dt>
|
|
<dd>Specifies the isolation level to be used for this statement.
|
|
<dl class="parml">
|
|
<dt class="bold">WITH <a id="idx2876" name="idx2876"></a></dt>
|
|
<dd>
|
|
<p>Introduces the isolation level, which may be one of:</p>
|
|
<ul>
|
|
<li>
|
|
<span class="term">RR</span> Repeatable read</li>
|
|
<li>
|
|
<span class="term">RS</span> Read stability</li>
|
|
<li>
|
|
<span class="term">CS</span> Cursor stability</li>
|
|
<li>
|
|
<span class="term">UR</span> Uncommitted read</li>
|
|
<li>
|
|
<span class="term">NC</span> No commit</li></ul>
|
|
</dd>
|
|
</dl> If <var class="pv">isolation-clause</var> is not specified the default isolation
|
|
is used. See <a href="rbafzmstintsel.htm#isocl">isolation-clause</a> for a description of how the default is
|
|
determined.
|
|
</dd>
|
|
</dl>
|
|
<a name="wq1628"></a>
|
|
<h3 id="wq1628"><a href="rbafzmst02.htm#ToC_1159">insert-multiple-rows</a></h3>
|
|
<dl class="parml">
|
|
<dt class="bold"><var class="pv">integer</var><span class="base"> or </span> <var class="pv">variable</var> ROWS </dt><a id="idx2877" name="idx2877"></a><a id="idx2878" name="idx2878"></a>
|
|
<dd>Specifies the number of rows to be inserted. If a <var class="pv">variable</var> is specified, it must be numeric with zero scale and cannot include
|
|
an indicator variable.
|
|
</dd>
|
|
<dt class="bold">VALUES (<var class="pv">host-structure-array</var>) </dt><a id="idx2879" name="idx2879"></a><a id="idx2880" name="idx2880"></a>
|
|
<dd>Specifies a set of new rows in the form of an array of host structures.
|
|
The <var class="pv">host-structure-array</var> must be declared in the program in accordance
|
|
with the rules for declaring host structure arrays. A parameter marker may <span>not</span> be used in place of the <var class="pv">host-structure-array</var> name.
|
|
<p>The number of variables in the host structure must equal the number of names
|
|
in the column-list. The first host structure in the array corresponds to the
|
|
first row, the second host structure in the array corresponds to the second
|
|
row, and so on. In addition, the first variable in the host structure corresponds
|
|
with the first column of the row, the second variable in the host structure
|
|
corresponds with the second column of the row, and so on.</p>
|
|
<p>For an explanation
|
|
of arrays of host structures see <a href="rbafzmstch2refvar.htm#array">Host structure arrays</a>.</p>
|
|
</dd>
|
|
</dl>
|
|
<p><span class="italic">insert-multiple-rows</span> is not allowed if the current
|
|
connection is to a non-iSeries remote server.</p>
|
|
<a name="insert"></a>
|
|
<h3 id="insert"><a href="rbafzmst02.htm#ToC_1160">INSERT Rules</a></h3>
|
|
<p><span class="bold">Default Values:</span> The value inserted in
|
|
any column that is not in the column list is the default value of the column.
|
|
Columns without a default value must be included in the column list. Similarly,
|
|
if you insert into a view without an INSTEAD OF INSERT trigger, the default
|
|
value is inserted into any column of the base table that is not included in
|
|
the view. Hence, all columns of the base table that are not in the view must
|
|
have default values.</p>
|
|
<p><span class="bold">Assignment:</span> Insert values are assigned to columns
|
|
in accordance with the assignment rules described in <a href="rbafzmstsqlelem.htm#sqlelem">Language elements</a>.</p>
|
|
<p><span class="bold">Validity:</span> If the identified table or the base table
|
|
of the identified view has one or more unique indexes or unique constraints,
|
|
each row inserted into the table must conform to the constraints imposed by
|
|
those indexes.</p>
|
|
<p>The unique indexes and unique constraints are effectively checked at the
|
|
end of the statement unless COMMIT(*NONE) was specified. In the case of a
|
|
multiple-row INSERT, this would occur after all rows were inserted and any
|
|
associated triggers were activated. If COMMIT(*NONE) is specified, checking
|
|
is performed as each row is inserted.</p><a id="idx2881" name="idx2881"></a><a id="idx2882" name="idx2882"></a>
|
|
<p>If the identified table or the base table of the identified view has one
|
|
or more check constraints, each check constraint must be true or unknown for
|
|
each row inserted into the table.</p>
|
|
<p>The check constraints are effectively checked at the end of the statement.
|
|
In the case of a multiple-row INSERT, this would occur after all rows were
|
|
inserted.</p>
|
|
<p>If a view is identified, the inserted rows must conform to any applicable
|
|
WITH CHECK OPTION. For more information, see <a href="rbafzmsthcview.htm#hcview">CREATE VIEW</a>.</p>
|
|
<p><span class="bold">Triggers:</span> If the identified table or the base table
|
|
of the identified view has an insert trigger, the trigger is activated. A
|
|
trigger might cause other statements to be executed or raise error conditions
|
|
based on the insert values.</p>
|
|
<p><span class="bold">Referential Integrity:</span> Each nonnull insert value
|
|
of a foreign key must equal some value of the parent key of the parent table
|
|
in the relationship.</p>
|
|
<p>The referential constraints (other than a referential constraint with a
|
|
RESTRICT delete rule) are effectively checked at the end of the statement.
|
|
In the case of a multiple-row INSERT, this would occur after all rows were
|
|
inserted and any associated triggers were activated.</p>
|
|
<a name="wq1629"></a>
|
|
<h3 id="wq1629"><a href="rbafzmst02.htm#ToC_1161">Notes</a></h3>
|
|
<p><span class="bold">Insert operation errors:</span> If an insert value violates
|
|
any constraints, or if any other error occurs during the execution of an INSERT
|
|
statement and COMMIT(*NONE) was not specified, all changes made during the
|
|
execution of the statement are backed out. However, other changes in the unit
|
|
of work made prior to the error are not backed out. If COMMIT(*NONE) is specified,
|
|
changes are not backed out.</p>
|
|
<p><span class="bold">Number of rows inserted:</span> After executing an INSERT
|
|
statement, the ROW_COUNT statement information item in the SQL Diagnostics
|
|
Area (or SQLERRD(3) of the SQLCA) is the number of rows that the database
|
|
manager inserted. The ROW_COUNT item does not include the number of rows that
|
|
were inserted as a result of a trigger.</p>
|
|
<p><span class="bold">Locking:</span> If COMMIT(*RR), COMMIT(*ALL), COMMIT(*CS),
|
|
or COMMIT(*CHG) is specified, one or more exclusive locks are acquired during
|
|
the execution of a successful INSERT statement. Until the locks are released
|
|
by a commit or rollback operation, an inserted row can only be accessed by: </p>
|
|
<ul>
|
|
<li>The application process that performed the insert</li>
|
|
<li>Another application process using COMMIT(*NONE) or COMMIT(*CHG) through
|
|
a read-only cursor, SELECT INTO statement, or subquery</li></ul>
|
|
<p>The locks can prevent other application processes from performing operations
|
|
on the table. For further information about locking, see the description of
|
|
the <a href="rbafzmstc4comit.htm#descomj">COMMIT</a>, <a href="rbafzmstrollbac.htm#desrolj">ROLLBACK</a>, and <a href="rbafzmsth2lockt.htm#deslocj">LOCK TABLE</a> statements. Also, see <a href="rbafzmstisol.htm#isol">Isolation level</a> and
|
|
the <a href="../dbp/rbafokickoff.htm">Database Programming</a> book.</p>
|
|
<p>A maximum of 500 000 000 rows can be inserted or changed in any single
|
|
INSERT statement when COMMIT(*RR), COMMIT(*ALL), COMMIT(*CS), or COMMIT(*CHG)
|
|
was specified. The number of rows changed includes any rows inserted, updated,
|
|
or deleted under the same commitment definition as a result of a trigger.</p>
|
|
<p><span class="bold">REXX:</span> Variables cannot be used in the INSERT statement
|
|
within a REXX procedure. Instead, the INSERT must be the object of a PREPARE
|
|
and EXECUTE using parameter markers.</p>
|
|
<p><span class="bold">Syntax alternatives:</span> The following keywords are synonyms
|
|
supported for compatibility to prior releases. These keywords are non-standard
|
|
and should not be used:</p>
|
|
<ul>
|
|
<li>The keyword NONE can be used as a synonym for NC.</li>
|
|
<li>The keyword CHG can be used as a synonym for UR.</li>
|
|
<li>The keyword ALL can be used as a synonym for RS.</li></ul>
|
|
<a name="wq1630"></a>
|
|
<h3 id="wq1630"><a href="rbafzmst02.htm#ToC_1162">Examples</a></h3>
|
|
<p><span class="italic">Example 1:</span> Insert a new department with the following
|
|
specifications into the DEPARTMENT table: </p>
|
|
<ul>
|
|
<li>Department number (DEPTNO) is 'E31'</li>
|
|
<li>Department name (DEPTNAME) is 'ARCHITECTURE'</li>
|
|
<li>Managed by (MGRNO) a person with number '00390'</li>
|
|
<li>Reports to (ADMRDEPT) department 'E01'.</li></ul><p class="indatacontent"> </p>
|
|
<pre class="xmp"> <span class="bold">INSERT INTO</span> DEPARTMENT
|
|
<span class="bold">VALUES (</span>'E31', 'ARCHITECTURE', '00390', 'E01'<span class="bold">)</span></pre>
|
|
<p><span class="italic">Example 2:</span> Insert a new department into the DEPARTMENT
|
|
table as in example 1, but do not assign a manager to the new department. </p>
|
|
<pre class="xmp"> <span class="bold">INSERT INTO</span> DEPARTMENT <span class="bold">(</span>DEPTNO, DEPTNAME, ADMRDEPT<span class="bold">)
|
|
VALUES (</span>'E31', 'ARCHITECTURE', 'E01'<span class="bold">)</span></pre>
|
|
<p><span class="italic">Example 3:</span> <span>Create a table MA_EMPPROJACT
|
|
with the same columns as the EMPPROJACT table. Populate MA_EMPPROJACT with
|
|
the rows from the EMPPROJACT table with a project number (PROJNO) starting
|
|
with the letters 'MA'</span>. </p>
|
|
<pre class="xmp"> <span class="bold">CREATE TABLE</span> MA_EMPPROJACT <span class="bold">LIKE</span> EMPPROJACT
|
|
|
|
<span class="bold">INSERT INTO</span> MA_EMPPROJACT
|
|
<span class="bold">SELECT * FROM</span> EMPPROJACT
|
|
<span class="bold">WHERE SUBSTR(</span>PROJNO, 1, 2<span class="bold">)</span> = 'MA'</pre>
|
|
<p><span class="italic">Example 4:</span> Use a <span>Java™</span> program statement to add a skeleton
|
|
project to the PROJECT table <span>on the connection context 'ctx'</span>. Obtain
|
|
the project number (PROJNO), project name (PROJNAME), department number (DEPTNO),
|
|
and responsible employee (RESPEMP) from host variables. Use the current date
|
|
as the project start date (PRSTDATE). Assign a NULL value to the remaining
|
|
columns in the table. </p>
|
|
<pre class="xmp"> #sql [ctx] { <span class="bold">INSERT INTO</span> PROJECT <span class="bold">(</span>PROJNO, PROJNAME, DEPTNO, RESPEMP, PRSTDATE<span class="bold">)
|
|
VALUES (</span>:PRJNO, :PRJNM, :DPTNO, :REMP, <span class="bold">CURRENT DATE)</span> };</pre>
|
|
<p><span class="italic">Example 5:</span> Insert two new departments using one
|
|
statement into the DEPARTMENT table as in example 2, but do not assign a manager
|
|
to the new departments.</p>
|
|
<pre class="xmp"> <span class="bold">INSERT INTO</span> DEPARTMENT <span class="bold">(</span>DEPTNO, DEPTNAME, ADMRDEPT<span class="bold">)
|
|
VALUES (</span>'B11', 'PURCHASING', 'B01'<span class="bold">)</span>,
|
|
<span class="bold">(</span>'E41', 'DATABASE ADMINISTRATION', 'E01'<span class="bold">)</span></pre>
|
|
<p><span class="italic">Example 6:</span> In a PL/I program, use a multiple-row
|
|
INSERT to add 10 rows to table DEPARTMENT. The host structure array DEPT contains
|
|
the data to be inserted. </p>
|
|
<pre class="xmp"> DCL 1 DEPT(10),
|
|
3 DEPT CHAR(3),
|
|
3 LASTNAME CHAR(29) VARYING,
|
|
3 WORKDEPT CHAR(6),
|
|
3 JOB CHAR(3);
|
|
|
|
EXEC SQL <span class="bold">INSERT INTO</span> DEPARTMENT 10 <span class="bold">ROWS VALUES</span> (:DEPT);</pre>
|
|
<p><span class="italic">Example 7:</span> Insert a new project into the EMPPROJACT
|
|
table using the Read Uncommitted (UR, CHG) option: </p>
|
|
<pre class="xmp"> <span class="bold">INSERT INTO</span> EMPPROJACT
|
|
<span class="bold">VALUES</span> ('000140', 'PL2100', 30)
|
|
<span class="bold">WITH CHG</span></pre>
|
|
<p><a id="idx2883" name="idx2883"></a><a id="idx2884" name="idx2884"></a></p>
|
|
<hr /><br />
|
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstclucu.htm">Previous Page</a> | <a href="rbafzmstlabelon.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>
|